MusicAlbum.cs 6.7 KB

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