ProviderIdsExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.</param>
  103. /// <param name="value">The value.</param>
  104. public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
  105. {
  106. ArgumentNullException.ThrowIfNull(instance);
  107. // If it's null remove the key from the dictionary
  108. if (string.IsNullOrEmpty(value))
  109. {
  110. instance.ProviderIds?.Remove(name);
  111. }
  112. else
  113. {
  114. // Ensure it exists
  115. instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  116. // Match on internal MetadataProvider enum string values before adding arbitrary providers
  117. if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
  118. {
  119. instance.ProviderIds[enumValue] = value;
  120. }
  121. else
  122. {
  123. instance.ProviderIds[name] = value;
  124. }
  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. }
  138. }