2
0

MusicArtist.cs 6.8 KB

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