MusicArtist.cs 6.7 KB

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