MusicArtist.cs 7.5 KB

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