MusicAlbum.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 MediaBrowser.Controller.Dto;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Configuration;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Users;
  13. namespace MediaBrowser.Controller.Entities.Audio
  14. {
  15. /// <summary>
  16. /// Class MusicAlbum
  17. /// </summary>
  18. public class MusicAlbum : Folder, IHasAlbumArtist, IHasArtist, IHasMusicGenres, IHasLookupInfo<AlbumInfo>, IMetadataContainer
  19. {
  20. /// <inheritdoc />
  21. public IReadOnlyList<string> AlbumArtists { get; set; }
  22. /// <inheritdoc />
  23. public IReadOnlyList<string> Artists { get; set; }
  24. public MusicAlbum()
  25. {
  26. Artists = Array.Empty<string>();
  27. AlbumArtists = Array.Empty<string>();
  28. }
  29. [JsonIgnore]
  30. public override bool SupportsAddingToPlaylist => true;
  31. [JsonIgnore]
  32. public override bool SupportsInheritedParentImages => true;
  33. [JsonIgnore]
  34. public MusicArtist MusicArtist => GetMusicArtist(new DtoOptions(true));
  35. public MusicArtist GetMusicArtist(DtoOptions options)
  36. {
  37. var parents = GetParents();
  38. foreach (var parent in parents)
  39. {
  40. if (parent is MusicArtist artist)
  41. {
  42. return artist;
  43. }
  44. }
  45. var name = AlbumArtist;
  46. if (!string.IsNullOrEmpty(name))
  47. {
  48. return LibraryManager.GetArtist(name, options);
  49. }
  50. return null;
  51. }
  52. [JsonIgnore]
  53. public override bool SupportsPlayedStatus => false;
  54. [JsonIgnore]
  55. public override bool SupportsCumulativeRunTimeTicks => true;
  56. [JsonIgnore]
  57. public string AlbumArtist => AlbumArtists.FirstOrDefault();
  58. [JsonIgnore]
  59. public override bool SupportsPeople => false;
  60. /// <summary>
  61. /// Gets the tracks.
  62. /// </summary>
  63. /// <value>The tracks.</value>
  64. [JsonIgnore]
  65. public IEnumerable<Audio> Tracks => GetRecursiveChildren(i => i is Audio).Cast<Audio>();
  66. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  67. {
  68. return Tracks;
  69. }
  70. public override double GetDefaultPrimaryImageAspectRatio()
  71. {
  72. return 1;
  73. }
  74. public override List<string> GetUserDataKeys()
  75. {
  76. var list = base.GetUserDataKeys();
  77. var albumArtist = AlbumArtist;
  78. if (!string.IsNullOrEmpty(albumArtist))
  79. {
  80. list.Insert(0, albumArtist + "-" + Name);
  81. }
  82. var id = this.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  83. if (!string.IsNullOrEmpty(id))
  84. {
  85. list.Insert(0, "MusicAlbum-Musicbrainz-" + id);
  86. }
  87. id = this.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  88. if (!string.IsNullOrEmpty(id))
  89. {
  90. list.Insert(0, "MusicAlbum-MusicBrainzReleaseGroup-" + id);
  91. }
  92. return list;
  93. }
  94. protected override bool GetBlockUnratedValue(UserPolicy config)
  95. {
  96. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  97. }
  98. public override UnratedItem GetBlockUnratedType()
  99. {
  100. return UnratedItem.Music;
  101. }
  102. public AlbumInfo GetLookupInfo()
  103. {
  104. var id = GetItemLookupInfo<AlbumInfo>();
  105. id.AlbumArtists = AlbumArtists;
  106. var artist = GetMusicArtist(new DtoOptions(false));
  107. if (artist != null)
  108. {
  109. id.ArtistProviderIds = artist.ProviderIds;
  110. }
  111. id.SongInfos = GetRecursiveChildren(i => i is Audio)
  112. .Cast<Audio>()
  113. .Select(i => i.GetLookupInfo())
  114. .ToList();
  115. var album = id.SongInfos
  116. .Select(i => i.Album)
  117. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  118. if (!string.IsNullOrEmpty(album))
  119. {
  120. id.Name = album;
  121. }
  122. return id;
  123. }
  124. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  125. {
  126. var items = GetRecursiveChildren();
  127. var totalItems = items.Count;
  128. var numComplete = 0;
  129. var childUpdateType = ItemUpdateType.None;
  130. // Refresh songs
  131. foreach (var item in items)
  132. {
  133. cancellationToken.ThrowIfCancellationRequested();
  134. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  135. childUpdateType = childUpdateType | updateType;
  136. numComplete++;
  137. double percent = numComplete;
  138. percent /= totalItems;
  139. progress.Report(percent * 95);
  140. }
  141. var parentRefreshOptions = refreshOptions;
  142. if (childUpdateType > ItemUpdateType.None)
  143. {
  144. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  145. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  146. }
  147. // Refresh current item
  148. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  149. if (!refreshOptions.IsAutomated)
  150. {
  151. await RefreshArtists(refreshOptions, cancellationToken).ConfigureAwait(false);
  152. }
  153. }
  154. private async Task RefreshArtists(MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
  155. {
  156. foreach (var i in this.GetAllArtists())
  157. {
  158. // This should not be necessary but we're seeing some cases of it
  159. if (string.IsNullOrEmpty(i))
  160. {
  161. continue;
  162. }
  163. var artist = LibraryManager.GetArtist(i);
  164. if (!artist.IsAccessedByName)
  165. {
  166. continue;
  167. }
  168. await artist.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  169. }
  170. }
  171. }
  172. }