MusicAlbum.cs 6.6 KB

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