MusicArtist.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Diacritics.Extensions;
  10. using Jellyfin.Data.Entities;
  11. using Jellyfin.Data.Enums;
  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. public class MusicArtist : Folder, IItemByName, IHasMusicGenres, IHasDualAccess, IHasLookupInfo<ArtistInfo>
  22. {
  23. [JsonIgnore]
  24. public bool IsAccessedByName => ParentId.Equals(default);
  25. [JsonIgnore]
  26. public override bool IsFolder => !IsAccessedByName;
  27. [JsonIgnore]
  28. public override bool SupportsInheritedParentImages => false;
  29. [JsonIgnore]
  30. public override bool SupportsCumulativeRunTimeTicks => true;
  31. [JsonIgnore]
  32. public override bool IsDisplayedAsFolder => true;
  33. [JsonIgnore]
  34. public override bool SupportsAddingToPlaylist => true;
  35. [JsonIgnore]
  36. public override bool SupportsPlayedStatus => false;
  37. /// <summary>
  38. /// Gets the folder containing the item.
  39. /// If the item is a folder, it returns the folder itself.
  40. /// </summary>
  41. /// <value>The containing folder path.</value>
  42. [JsonIgnore]
  43. public override string ContainingFolderPath => Path;
  44. [JsonIgnore]
  45. public override IEnumerable<BaseItem> Children
  46. {
  47. get
  48. {
  49. if (IsAccessedByName)
  50. {
  51. return new List<BaseItem>();
  52. }
  53. return base.Children;
  54. }
  55. }
  56. [JsonIgnore]
  57. public override bool SupportsPeople => false;
  58. public static string GetPath(string name)
  59. {
  60. return GetPath(name, true);
  61. }
  62. public override double GetDefaultPrimaryImageAspectRatio()
  63. {
  64. return 1;
  65. }
  66. public override bool CanDelete()
  67. {
  68. return !IsAccessedByName;
  69. }
  70. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  71. {
  72. if (query.IncludeItemTypes.Length == 0)
  73. {
  74. query.IncludeItemTypes = new[] { BaseItemKind.Audio, BaseItemKind.MusicVideo, BaseItemKind.MusicAlbum };
  75. query.ArtistIds = new[] { Id };
  76. }
  77. return LibraryManager.GetItemList(query);
  78. }
  79. public override int GetChildCount(User user)
  80. {
  81. return IsAccessedByName ? 0 : base.GetChildCount(user);
  82. }
  83. public override bool IsSaveLocalMetadataEnabled()
  84. {
  85. if (IsAccessedByName)
  86. {
  87. return true;
  88. }
  89. return base.IsSaveLocalMetadataEnabled();
  90. }
  91. protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  92. {
  93. if (IsAccessedByName)
  94. {
  95. // Should never get in here anyway
  96. return Task.CompletedTask;
  97. }
  98. return base.ValidateChildrenInternal(progress, recursive, refreshChildMetadata, refreshOptions, directoryService, cancellationToken);
  99. }
  100. public override List<string> GetUserDataKeys()
  101. {
  102. var list = base.GetUserDataKeys();
  103. list.InsertRange(0, GetUserDataKeys(this));
  104. return list;
  105. }
  106. /// <summary>
  107. /// Gets the user data key.
  108. /// </summary>
  109. /// <param name="item">The item.</param>
  110. /// <returns>System.String.</returns>
  111. private static List<string> GetUserDataKeys(MusicArtist item)
  112. {
  113. var list = new List<string>();
  114. var id = item.GetProviderId(MetadataProvider.MusicBrainzArtist);
  115. if (!string.IsNullOrEmpty(id))
  116. {
  117. list.Add("Artist-Musicbrainz-" + id);
  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. }