MusicArtist.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Data.Entities;
  10. using Jellyfin.Data.Enums;
  11. using Jellyfin.Extensions;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Entities;
  14. using Microsoft.Extensions.Logging;
  15. using MetadataProvider = MediaBrowser.Model.Entities.MetadataProvider;
  16. namespace MediaBrowser.Controller.Entities.Audio
  17. {
  18. /// <summary>
  19. /// Class MusicArtist.
  20. /// </summary>
  21. [Common.RequiresSourceSerialisation]
  22. public class MusicArtist : Folder, IItemByName, IHasMusicGenres, IHasDualAccess, IHasLookupInfo<ArtistInfo>
  23. {
  24. [JsonIgnore]
  25. public bool IsAccessedByName => ParentId.IsEmpty();
  26. [JsonIgnore]
  27. public override bool IsFolder => !IsAccessedByName;
  28. [JsonIgnore]
  29. public override bool SupportsInheritedParentImages => false;
  30. [JsonIgnore]
  31. public override bool SupportsCumulativeRunTimeTicks => true;
  32. [JsonIgnore]
  33. public override bool IsDisplayedAsFolder => true;
  34. [JsonIgnore]
  35. public override bool SupportsAddingToPlaylist => true;
  36. [JsonIgnore]
  37. public override bool SupportsPlayedStatus => false;
  38. /// <summary>
  39. /// Gets the folder containing the item.
  40. /// If the item is a folder, it returns the folder itself.
  41. /// </summary>
  42. /// <value>The containing folder path.</value>
  43. [JsonIgnore]
  44. public override string ContainingFolderPath => Path;
  45. [JsonIgnore]
  46. public override IEnumerable<BaseItem> Children
  47. {
  48. get
  49. {
  50. if (IsAccessedByName)
  51. {
  52. return Enumerable.Empty<BaseItem>();
  53. }
  54. return base.Children;
  55. }
  56. }
  57. [JsonIgnore]
  58. public override bool SupportsPeople => false;
  59. public static string GetPath(string name)
  60. {
  61. return GetPath(name, true);
  62. }
  63. public override double GetDefaultPrimaryImageAspectRatio()
  64. {
  65. return 1;
  66. }
  67. public override bool CanDelete()
  68. {
  69. return !IsAccessedByName;
  70. }
  71. public IReadOnlyList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  72. {
  73. if (query.IncludeItemTypes.Length == 0)
  74. {
  75. query.IncludeItemTypes = new[] { BaseItemKind.Audio, BaseItemKind.MusicVideo, BaseItemKind.MusicAlbum };
  76. query.ArtistIds = new[] { Id };
  77. }
  78. return LibraryManager.GetItemList(query);
  79. }
  80. public override int GetChildCount(User user)
  81. {
  82. return IsAccessedByName ? 0 : base.GetChildCount(user);
  83. }
  84. public override bool IsSaveLocalMetadataEnabled()
  85. {
  86. if (IsAccessedByName)
  87. {
  88. return true;
  89. }
  90. return base.IsSaveLocalMetadataEnabled();
  91. }
  92. protected override async Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, bool allowRemoveRoot, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  93. {
  94. if (IsAccessedByName)
  95. {
  96. // Should never get in here anyway
  97. return;
  98. }
  99. await base.ValidateChildrenInternal(progress, recursive, refreshChildMetadata, false, refreshOptions, directoryService, cancellationToken).ConfigureAwait(false);
  100. }
  101. public override List<string> GetUserDataKeys()
  102. {
  103. var list = base.GetUserDataKeys();
  104. list.InsertRange(0, GetUserDataKeys(this));
  105. return list;
  106. }
  107. /// <summary>
  108. /// Gets the user data key.
  109. /// </summary>
  110. /// <param name="item">The item.</param>
  111. /// <returns>System.String.</returns>
  112. private static List<string> GetUserDataKeys(MusicArtist item)
  113. {
  114. var list = new List<string>();
  115. if (item.TryGetProviderId(MetadataProvider.MusicBrainzArtist, out var externalId))
  116. {
  117. list.Add("Artist-Musicbrainz-" + externalId);
  118. }
  119. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  120. return list;
  121. }
  122. public override string CreatePresentationUniqueKey()
  123. {
  124. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  125. }
  126. protected override bool GetBlockUnratedValue(User user)
  127. {
  128. return user.GetPreferenceValues<UnratedItem>(PreferenceKind.BlockUnratedItems).Contains(UnratedItem.Music);
  129. }
  130. public override UnratedItem GetBlockUnratedType()
  131. {
  132. return UnratedItem.Music;
  133. }
  134. public ArtistInfo GetLookupInfo()
  135. {
  136. var info = GetItemLookupInfo<ArtistInfo>();
  137. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  138. .Cast<Audio>()
  139. .Select(i => i.GetLookupInfo())
  140. .ToList();
  141. return info;
  142. }
  143. public static string GetPath(string name, bool normalizeName)
  144. {
  145. // Trim the period at the end because windows will have a hard time with that
  146. var validName = normalizeName ?
  147. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  148. name;
  149. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.ArtistsPath, validName);
  150. }
  151. private string GetRebasedPath()
  152. {
  153. return GetPath(System.IO.Path.GetFileName(Path), false);
  154. }
  155. public override bool RequiresRefresh()
  156. {
  157. if (IsAccessedByName)
  158. {
  159. var newPath = GetRebasedPath();
  160. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  161. {
  162. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  163. return true;
  164. }
  165. }
  166. return base.RequiresRefresh();
  167. }
  168. /// <summary>
  169. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  170. /// </summary>
  171. /// <param name="replaceAllMetadata">Option to replace metadata.</param>
  172. /// <returns>True if metadata changed.</returns>
  173. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  174. {
  175. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  176. if (IsAccessedByName)
  177. {
  178. var newPath = GetRebasedPath();
  179. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  180. {
  181. Path = newPath;
  182. hasChanges = true;
  183. }
  184. }
  185. return hasChanges;
  186. }
  187. }
  188. }