ProviderIdsExtensions.cs 8.1 KB

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