MusicAlbum.cs 6.7 KB

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