MusicArtist.cs 6.8 KB

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