MusicArtist.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. var id = item.GetProviderId(MetadataProvider.MusicBrainzArtist);
  116. if (!string.IsNullOrEmpty(id))
  117. {
  118. list.Add("Artist-Musicbrainz-" + id);
  119. }
  120. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  121. return list;
  122. }
  123. public override string CreatePresentationUniqueKey()
  124. {
  125. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  126. }
  127. protected override bool GetBlockUnratedValue(User user)
  128. {
  129. return user.GetPreferenceValues<UnratedItem>(PreferenceKind.BlockUnratedItems).Contains(UnratedItem.Music);
  130. }
  131. public override UnratedItem GetBlockUnratedType()
  132. {
  133. return UnratedItem.Music;
  134. }
  135. public ArtistInfo GetLookupInfo()
  136. {
  137. var info = GetItemLookupInfo<ArtistInfo>();
  138. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  139. .Cast<Audio>()
  140. .Select(i => i.GetLookupInfo())
  141. .ToList();
  142. return info;
  143. }
  144. public static string GetPath(string name, bool normalizeName)
  145. {
  146. // Trim the period at the end because windows will have a hard time with that
  147. var validName = normalizeName ?
  148. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  149. name;
  150. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.ArtistsPath, validName);
  151. }
  152. private string GetRebasedPath()
  153. {
  154. return GetPath(System.IO.Path.GetFileName(Path), false);
  155. }
  156. public override bool RequiresRefresh()
  157. {
  158. if (IsAccessedByName)
  159. {
  160. var newPath = GetRebasedPath();
  161. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  162. {
  163. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  164. return true;
  165. }
  166. }
  167. return base.RequiresRefresh();
  168. }
  169. /// <summary>
  170. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  171. /// </summary>
  172. /// <param name="replaceAllMetadata">Option to replace metadata.</param>
  173. /// <returns>True if metadata changed.</returns>
  174. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  175. {
  176. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  177. if (IsAccessedByName)
  178. {
  179. var newPath = GetRebasedPath();
  180. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  181. {
  182. Path = newPath;
  183. hasChanges = true;
  184. }
  185. }
  186. return hasChanges;
  187. }
  188. }
  189. }