ProviderIdsExtensions.cs 7.0 KB

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