MusicArtist.cs 7.7 KB

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