| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace MediaBrowser.Model.Entities; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Class ProviderIdsExtensions. |
| | 10 | | /// </summary> |
| | 11 | | public static class ProviderIdsExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Case-insensitive dictionary of <see cref="MetadataProvider"/> string representation. |
| | 15 | | /// </summary> |
| 3 | 16 | | private static readonly Dictionary<string, string> _metadataProviderEnumDictionary = |
| 3 | 17 | | Enum.GetValues<MetadataProvider>() |
| 3 | 18 | | .ToDictionary( |
| 3 | 19 | | enumValue => enumValue.ToString(), |
| 3 | 20 | | enumValue => enumValue.ToString(), |
| 3 | 21 | | StringComparer.OrdinalIgnoreCase); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Checks if this instance has an id for the given provider. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="instance">The instance.</param> |
| | 27 | | /// <param name="name">The of the provider name.</param> |
| | 28 | | /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns> |
| | 29 | | public static bool HasProviderId(this IHasProviderIds instance, string name) |
| 6 | 30 | | => instance.TryGetProviderId(name, out _); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Checks if this instance has an id for the given provider. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="instance">The instance.</param> |
| | 36 | | /// <param name="provider">The provider.</param> |
| | 37 | | /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns> |
| | 38 | | public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider) |
| 5 | 39 | | => instance.HasProviderId(provider.ToString()); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets a provider id. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="instance">The instance.</param> |
| | 45 | | /// <param name="name">The name.</param> |
| | 46 | | /// <param name="id">The provider id.</param> |
| | 47 | | /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns> |
| | 48 | | public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id) |
| | 49 | | { |
| 45 | 50 | | ArgumentNullException.ThrowIfNull(instance); |
| | 51 | |
|
| 43 | 52 | | if (instance.ProviderIds is null) |
| | 53 | | { |
| 3 | 54 | | id = null; |
| 3 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| 40 | 58 | | var foundProviderId = instance.ProviderIds.TryGetValue(name, out id); |
| | 59 | | // This occurs when searching with Identify (and possibly in other places) |
| 38 | 60 | | if (string.IsNullOrEmpty(id)) |
| | 61 | | { |
| 35 | 62 | | id = null; |
| 35 | 63 | | foundProviderId = false; |
| | 64 | | } |
| | 65 | |
|
| 38 | 66 | | return foundProviderId; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Gets a provider id. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="instance">The instance.</param> |
| | 73 | | /// <param name="provider">The provider.</param> |
| | 74 | | /// <param name="id">The provider id.</param> |
| | 75 | | /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns> |
| | 76 | | public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] ou |
| | 77 | | { |
| 34 | 78 | | return instance.TryGetProviderId(provider.ToString(), out id); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets a provider id. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="instance">The instance.</param> |
| | 85 | | /// <param name="name">The name.</param> |
| | 86 | | /// <returns>System.String.</returns> |
| | 87 | | public static string? GetProviderId(this IHasProviderIds instance, string name) |
| | 88 | | { |
| 5 | 89 | | instance.TryGetProviderId(name, out string? id); |
| 3 | 90 | | return id; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Gets a provider id. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="instance">The instance.</param> |
| | 97 | | /// <param name="provider">The provider.</param> |
| | 98 | | /// <returns>System.String.</returns> |
| | 99 | | public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider) |
| | 100 | | { |
| 4 | 101 | | return instance.GetProviderId(provider.ToString()); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Sets a provider id. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="instance">The instance.</param> |
| | 108 | | /// <param name="name">The name, this should not contain a '=' character.</param> |
| | 109 | | /// <param name="value">The value.</param> |
| | 110 | | /// <remarks>Due to how deserialization from the database works the name cannot contain '='.</remarks> |
| | 111 | | /// <returns><c>true</c> if the provider id got set successfully; otherwise, <c>false</c>.</returns> |
| | 112 | | public static bool TrySetProviderId(this IHasProviderIds instance, string? name, string? value) |
| | 113 | | { |
| 30 | 114 | | ArgumentNullException.ThrowIfNull(instance); |
| | 115 | |
|
| | 116 | | // When name contains a '=' it can't be deserialized from the database |
| 30 | 117 | | if (string.IsNullOrWhiteSpace(name) |
| 30 | 118 | | || string.IsNullOrWhiteSpace(value) |
| 30 | 119 | | || name.Contains('=', StringComparison.Ordinal)) |
| | 120 | | { |
| 7 | 121 | | return false; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | // Ensure it exists |
| 23 | 125 | | instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 126 | |
|
| | 127 | | // Match on internal MetadataProvider enum string values before adding arbitrary providers |
| 23 | 128 | | if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue)) |
| | 129 | | { |
| 22 | 130 | | instance.ProviderIds[enumValue] = value; |
| | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 1 | 134 | | instance.ProviderIds[name] = value; |
| | 135 | | } |
| | 136 | |
|
| 23 | 137 | | return true; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Sets a provider id. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="instance">The instance.</param> |
| | 144 | | /// <param name="provider">The provider.</param> |
| | 145 | | /// <param name="value">The value.</param> |
| | 146 | | /// <returns><c>true</c> if the provider id got set successfully; otherwise, <c>false</c>.</returns> |
| | 147 | | public static bool TrySetProviderId(this IHasProviderIds instance, MetadataProvider provider, string? value) |
| 21 | 148 | | => instance.TrySetProviderId(provider.ToString(), value); |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Sets a provider id. |
| | 152 | | /// </summary> |
| | 153 | | /// <param name="instance">The instance.</param> |
| | 154 | | /// <param name="name">The name, this should not contain a '=' character.</param> |
| | 155 | | /// <param name="value">The value.</param> |
| | 156 | | /// <remarks>Due to how deserialization from the database works the name cannot contain '='.</remarks> |
| | 157 | | public static void SetProviderId(this IHasProviderIds instance, string name, string value) |
| | 158 | | { |
| 11 | 159 | | ArgumentNullException.ThrowIfNull(instance); |
| 10 | 160 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| 10 | 161 | | ArgumentException.ThrowIfNullOrWhiteSpace(value); |
| | 162 | |
|
| | 163 | | // When name contains a '=' it can't be deserialized from the database |
| 7 | 164 | | if (name.Contains('=', StringComparison.Ordinal)) |
| | 165 | | { |
| 0 | 166 | | throw new ArgumentException("Provider id name cannot contain '='", nameof(name)); |
| | 167 | | } |
| | 168 | |
|
| | 169 | | // Ensure it exists |
| 7 | 170 | | instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 171 | |
|
| | 172 | | // Match on internal MetadataProvider enum string values before adding arbitrary providers |
| 7 | 173 | | if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue)) |
| | 174 | | { |
| 7 | 175 | | instance.ProviderIds[enumValue] = value; |
| | 176 | | } |
| | 177 | | else |
| | 178 | | { |
| 0 | 179 | | instance.ProviderIds[name] = value; |
| | 180 | | } |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// Sets a provider id. |
| | 185 | | /// </summary> |
| | 186 | | /// <param name="instance">The instance.</param> |
| | 187 | | /// <param name="provider">The provider.</param> |
| | 188 | | /// <param name="value">The value.</param> |
| | 189 | | public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value) |
| 11 | 190 | | => instance.SetProviderId(provider.ToString(), value); |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Removes a provider id. |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="instance">The instance.</param> |
| | 196 | | /// <param name="name">The name.</param> |
| | 197 | | public static void RemoveProviderId(this IHasProviderIds instance, string name) |
| | 198 | | { |
| 0 | 199 | | ArgumentNullException.ThrowIfNull(instance); |
| 0 | 200 | | ArgumentException.ThrowIfNullOrEmpty(name); |
| | 201 | |
|
| 0 | 202 | | instance.ProviderIds?.Remove(name); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Removes a provider id. |
| | 207 | | /// </summary> |
| | 208 | | /// <param name="instance">The instance.</param> |
| | 209 | | /// <param name="provider">The provider.</param> |
| | 210 | | public static void RemoveProviderId(this IHasProviderIds instance, MetadataProvider provider) |
| | 211 | | { |
| 1 | 212 | | ArgumentNullException.ThrowIfNull(instance); |
| | 213 | |
|
| 1 | 214 | | instance.ProviderIds?.Remove(provider.ToString()); |
| 1 | 215 | | } |
| | 216 | | } |