ProviderIdsExtensions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if (instance == null)
  30. {
  31. throw new ArgumentNullException(nameof(instance));
  32. }
  33. return instance.TryGetProviderId(name, out _);
  34. }
  35. /// <summary>
  36. /// Checks if this instance has an id for the given provider.
  37. /// </summary>
  38. /// <param name="instance">The instance.</param>
  39. /// <param name="provider">The provider.</param>
  40. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  41. public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
  42. {
  43. return instance.HasProviderId(provider.ToString());
  44. }
  45. /// <summary>
  46. /// Gets a provider id.
  47. /// </summary>
  48. /// <param name="instance">The instance.</param>
  49. /// <param name="name">The name.</param>
  50. /// <param name="id">The provider id.</param>
  51. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  52. public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id)
  53. {
  54. if (instance == null)
  55. {
  56. throw new ArgumentNullException(nameof(instance));
  57. }
  58. if (instance.ProviderIds == null)
  59. {
  60. id = null;
  61. return false;
  62. }
  63. var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
  64. // This occurs when searching with Identify (and possibly in other places)
  65. if (string.IsNullOrEmpty(id))
  66. {
  67. id = null;
  68. foundProviderId = false;
  69. }
  70. return foundProviderId;
  71. }
  72. /// <summary>
  73. /// Gets a provider id.
  74. /// </summary>
  75. /// <param name="instance">The instance.</param>
  76. /// <param name="provider">The provider.</param>
  77. /// <param name="id">The provider id.</param>
  78. /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
  79. public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id)
  80. {
  81. return instance.TryGetProviderId(provider.ToString(), out id);
  82. }
  83. /// <summary>
  84. /// Gets a provider id.
  85. /// </summary>
  86. /// <param name="instance">The instance.</param>
  87. /// <param name="name">The name.</param>
  88. /// <returns>System.String.</returns>
  89. public static string? GetProviderId(this IHasProviderIds instance, string name)
  90. {
  91. instance.TryGetProviderId(name, out string? id);
  92. return id;
  93. }
  94. /// <summary>
  95. /// Gets a provider id.
  96. /// </summary>
  97. /// <param name="instance">The instance.</param>
  98. /// <param name="provider">The provider.</param>
  99. /// <returns>System.String.</returns>
  100. public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
  101. {
  102. return instance.GetProviderId(provider.ToString());
  103. }
  104. /// <summary>
  105. /// Sets a provider id.
  106. /// </summary>
  107. /// <param name="instance">The instance.</param>
  108. /// <param name="name">The name.</param>
  109. /// <param name="value">The value.</param>
  110. public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
  111. {
  112. if (instance == null)
  113. {
  114. throw new ArgumentNullException(nameof(instance));
  115. }
  116. // If it's null remove the key from the dictionary
  117. if (string.IsNullOrEmpty(value))
  118. {
  119. instance.ProviderIds?.Remove(name);
  120. }
  121. else
  122. {
  123. // Ensure it exists
  124. instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  125. // Match on internal MetadataProvider enum string values before adding arbitrary providers
  126. if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
  127. {
  128. instance.ProviderIds[enumValue] = value;
  129. }
  130. else
  131. {
  132. instance.ProviderIds[name] = value;
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// Sets a provider id.
  138. /// </summary>
  139. /// <param name="instance">The instance.</param>
  140. /// <param name="provider">The provider.</param>
  141. /// <param name="value">The value.</param>
  142. public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
  143. {
  144. instance.SetProviderId(provider.ToString(), value);
  145. }
  146. }
  147. }