MusicArtist.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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(Guid.Empty);
  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. public override double GetDefaultPrimaryImageAspectRatio()
  38. {
  39. return 1;
  40. }
  41. public override bool CanDelete()
  42. {
  43. return !IsAccessedByName;
  44. }
  45. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  46. {
  47. if (query.IncludeItemTypes.Length == 0)
  48. {
  49. query.IncludeItemTypes = new[] { nameof(Audio), nameof(MusicVideo), nameof(MusicAlbum) };
  50. query.ArtistIds = new[] { Id };
  51. }
  52. return LibraryManager.GetItemList(query);
  53. }
  54. [JsonIgnore]
  55. public override IEnumerable<BaseItem> Children
  56. {
  57. get
  58. {
  59. if (IsAccessedByName)
  60. {
  61. return new List<BaseItem>();
  62. }
  63. return base.Children;
  64. }
  65. }
  66. public override int GetChildCount(User user)
  67. {
  68. return IsAccessedByName ? 0 : base.GetChildCount(user);
  69. }
  70. public override bool IsSaveLocalMetadataEnabled()
  71. {
  72. if (IsAccessedByName)
  73. {
  74. return true;
  75. }
  76. return base.IsSaveLocalMetadataEnabled();
  77. }
  78. protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  79. {
  80. if (IsAccessedByName)
  81. {
  82. // Should never get in here anyway
  83. return Task.CompletedTask;
  84. }
  85. return base.ValidateChildrenInternal(progress, recursive, refreshChildMetadata, refreshOptions, directoryService, cancellationToken);
  86. }
  87. public override List<string> GetUserDataKeys()
  88. {
  89. var list = base.GetUserDataKeys();
  90. list.InsertRange(0, GetUserDataKeys(this));
  91. return list;
  92. }
  93. /// <summary>
  94. /// Gets the folder containing the item.
  95. /// If the item is a folder, it returns the folder itself.
  96. /// </summary>
  97. /// <value>The containing folder path.</value>
  98. [JsonIgnore]
  99. public override string ContainingFolderPath => Path;
  100. /// <summary>
  101. /// Gets the user data key.
  102. /// </summary>
  103. /// <param name="item">The item.</param>
  104. /// <returns>System.String.</returns>
  105. private static List<string> GetUserDataKeys(MusicArtist item)
  106. {
  107. var list = new List<string>();
  108. var id = item.GetProviderId(MetadataProvider.MusicBrainzArtist);
  109. if (!string.IsNullOrEmpty(id))
  110. {
  111. list.Add("Artist-Musicbrainz-" + id);
  112. }
  113. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  114. return list;
  115. }
  116. public override string CreatePresentationUniqueKey()
  117. {
  118. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  119. }
  120. protected override bool GetBlockUnratedValue(User user)
  121. {
  122. return user.GetPreferenceValues<UnratedItem>(PreferenceKind.BlockUnratedItems).Contains(UnratedItem.Music);
  123. }
  124. public override UnratedItem GetBlockUnratedType()
  125. {
  126. return UnratedItem.Music;
  127. }
  128. public ArtistInfo GetLookupInfo()
  129. {
  130. var info = GetItemLookupInfo<ArtistInfo>();
  131. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  132. .Cast<Audio>()
  133. .Select(i => i.GetLookupInfo())
  134. .ToList();
  135. return info;
  136. }
  137. [JsonIgnore]
  138. public override bool SupportsPeople => false;
  139. public static string GetPath(string name)
  140. {
  141. return GetPath(name, true);
  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. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  172. {
  173. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  174. if (IsAccessedByName)
  175. {
  176. var newPath = GetRebasedPath();
  177. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  178. {
  179. Path = newPath;
  180. hasChanges = true;
  181. }
  182. }
  183. return hasChanges;
  184. }
  185. }
  186. }