MusicArtist.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Enums;
  8. using MediaBrowser.Controller.Extensions;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Configuration;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Users;
  13. using Microsoft.Extensions.Logging;
  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(Jellyfin.Data.Entities.User user)
  65. {
  66. if (IsAccessedByName)
  67. {
  68. return 0;
  69. }
  70. return base.GetChildCount(user);
  71. }
  72. public override bool IsSaveLocalMetadataEnabled()
  73. {
  74. if (IsAccessedByName)
  75. {
  76. return true;
  77. }
  78. return base.IsSaveLocalMetadataEnabled();
  79. }
  80. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  81. {
  82. if (IsAccessedByName)
  83. {
  84. // Should never get in here anyway
  85. return Task.CompletedTask;
  86. }
  87. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  88. }
  89. public override List<string> GetUserDataKeys()
  90. {
  91. var list = base.GetUserDataKeys();
  92. list.InsertRange(0, GetUserDataKeys(this));
  93. return list;
  94. }
  95. /// <summary>
  96. /// Returns the folder containing the item.
  97. /// If the item is a folder, it returns the folder itself
  98. /// </summary>
  99. /// <value>The containing folder path.</value>
  100. [JsonIgnore]
  101. public override string ContainingFolderPath => Path;
  102. /// <summary>
  103. /// Gets the user data key.
  104. /// </summary>
  105. /// <param name="item">The item.</param>
  106. /// <returns>System.String.</returns>
  107. private static List<string> GetUserDataKeys(MusicArtist item)
  108. {
  109. var list = new List<string>();
  110. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  111. if (!string.IsNullOrEmpty(id))
  112. {
  113. list.Add("Artist-Musicbrainz-" + id);
  114. }
  115. list.Add("Artist-" + (item.Name ?? string.Empty).RemoveDiacritics());
  116. return list;
  117. }
  118. public override string CreatePresentationUniqueKey()
  119. {
  120. return "Artist-" + (Name ?? string.Empty).RemoveDiacritics();
  121. }
  122. protected override bool GetBlockUnratedValue(Jellyfin.Data.Entities.User user)
  123. {
  124. return user.GetPreference(PreferenceKind.BlockUnratedItems).Contains(UnratedItem.Music.ToString());
  125. }
  126. public override UnratedItem GetBlockUnratedType()
  127. {
  128. return UnratedItem.Music;
  129. }
  130. public ArtistInfo GetLookupInfo()
  131. {
  132. var info = GetItemLookupInfo<ArtistInfo>();
  133. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  134. .Cast<Audio>()
  135. .Select(i => i.GetLookupInfo())
  136. .ToList();
  137. return info;
  138. }
  139. [JsonIgnore]
  140. public override bool SupportsPeople => false;
  141. public static string GetPath(string name)
  142. {
  143. return GetPath(name, true);
  144. }
  145. public static string GetPath(string name, bool normalizeName)
  146. {
  147. // Trim the period at the end because windows will have a hard time with that
  148. var validName = normalizeName ?
  149. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  150. name;
  151. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.ArtistsPath, validName);
  152. }
  153. private string GetRebasedPath()
  154. {
  155. return GetPath(System.IO.Path.GetFileName(Path), false);
  156. }
  157. public override bool RequiresRefresh()
  158. {
  159. if (IsAccessedByName)
  160. {
  161. var newPath = GetRebasedPath();
  162. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  163. {
  164. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  165. return true;
  166. }
  167. }
  168. return base.RequiresRefresh();
  169. }
  170. /// <summary>
  171. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  172. /// </summary>
  173. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  174. {
  175. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  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. }