MusicArtist.cs 6.8 KB

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