MusicAlbum.cs 6.6 KB

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