MusicAlbumResolver.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Emby.Naming.Audio;
  9. using Emby.Naming.Common;
  10. using Jellyfin.Data.Enums;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Controller.Resolvers;
  15. using MediaBrowser.Model.IO;
  16. using Microsoft.Extensions.Logging;
  17. namespace Emby.Server.Implementations.Library.Resolvers.Audio
  18. {
  19. /// <summary>
  20. /// The music album resolver.
  21. /// </summary>
  22. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  23. {
  24. private readonly ILogger<MusicAlbumResolver> _logger;
  25. private readonly NamingOptions _namingOptions;
  26. private readonly IDirectoryService _directoryService;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="MusicAlbumResolver"/> class.
  29. /// </summary>
  30. /// <param name="logger">The logger.</param>
  31. /// <param name="namingOptions">The naming options.</param>
  32. /// <param name="directoryService">The directory service.</param>
  33. public MusicAlbumResolver(ILogger<MusicAlbumResolver> logger, NamingOptions namingOptions, IDirectoryService directoryService)
  34. {
  35. _logger = logger;
  36. _namingOptions = namingOptions;
  37. _directoryService = directoryService;
  38. }
  39. /// <summary>
  40. /// Gets the priority.
  41. /// </summary>
  42. /// <value>The priority.</value>
  43. public override ResolverPriority Priority => ResolverPriority.Third;
  44. /// <summary>
  45. /// Resolves the specified args.
  46. /// </summary>
  47. /// <param name="args">The args.</param>
  48. /// <returns>MusicAlbum.</returns>
  49. protected override MusicAlbum Resolve(ItemResolveArgs args)
  50. {
  51. var collectionType = args.GetCollectionType();
  52. var isMusicMediaFolder = collectionType == CollectionType.music;
  53. // If there's a collection type and it's not music, don't allow it.
  54. if (!isMusicMediaFolder)
  55. {
  56. return null;
  57. }
  58. if (!args.IsDirectory)
  59. {
  60. return null;
  61. }
  62. // Avoid mis-identifying top folders
  63. if (args.HasParent<MusicAlbum>())
  64. {
  65. return null;
  66. }
  67. if (args.Parent.IsRoot)
  68. {
  69. return null;
  70. }
  71. return IsMusicAlbum(args) ? new MusicAlbum() : null;
  72. }
  73. /// <summary>
  74. /// Determine if the supplied file data points to a music album.
  75. /// </summary>
  76. /// <param name="path">The path to check.</param>
  77. /// <param name="directoryService">The directory service.</param>
  78. /// <returns><c>true</c> if the provided path points to a music album; otherwise, <c>false</c>.</returns>
  79. public bool IsMusicAlbum(string path, IDirectoryService directoryService)
  80. {
  81. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService);
  82. }
  83. /// <summary>
  84. /// Determine if the supplied resolve args should be considered a music album.
  85. /// </summary>
  86. /// <param name="args">The args.</param>
  87. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  88. private bool IsMusicAlbum(ItemResolveArgs args)
  89. {
  90. if (args.IsDirectory)
  91. {
  92. // If args is a artist subfolder it's not a music album
  93. foreach (var subfolder in _namingOptions.ArtistSubfolders)
  94. {
  95. if (Path.GetDirectoryName(args.Path.AsSpan()).Equals(subfolder, StringComparison.OrdinalIgnoreCase))
  96. {
  97. _logger.LogDebug("Found release folder: {Path}", args.Path);
  98. return false;
  99. }
  100. }
  101. // If args contains music it's a music album
  102. if (ContainsMusic(args.FileSystemChildren, true, _directoryService))
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. /// <summary>
  110. /// Determine if the supplied list contains what we should consider music.
  111. /// </summary>
  112. /// <returns><c>true</c> if the provided path list contains music; otherwise, <c>false</c>.</returns>
  113. private bool ContainsMusic(
  114. ICollection<FileSystemMetadata> list,
  115. bool allowSubfolders,
  116. IDirectoryService directoryService)
  117. {
  118. // Check for audio files before digging down into directories
  119. var foundAudioFile = list.Any(fileSystemInfo => !fileSystemInfo.IsDirectory && AudioFileParser.IsAudioFile(fileSystemInfo.FullName, _namingOptions));
  120. if (foundAudioFile)
  121. {
  122. // At least one audio file exists
  123. return true;
  124. }
  125. if (!allowSubfolders)
  126. {
  127. // Not music since no audio file exists and we're not looking into subfolders
  128. return false;
  129. }
  130. var discSubfolderCount = 0;
  131. var parser = new AlbumParser(_namingOptions);
  132. var directories = list.Where(fileSystemInfo => fileSystemInfo.IsDirectory);
  133. var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
  134. {
  135. var path = fileSystemInfo.FullName;
  136. var hasMusic = ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService);
  137. if (hasMusic)
  138. {
  139. if (parser.IsMultiPart(path))
  140. {
  141. _logger.LogDebug("Found multi-disc folder: {Path}", path);
  142. Interlocked.Increment(ref discSubfolderCount);
  143. }
  144. else
  145. {
  146. // If there are folders underneath with music that are not multidisc, then this can't be a multi-disc album
  147. state.Stop();
  148. }
  149. }
  150. });
  151. if (!result.IsCompleted)
  152. {
  153. return false;
  154. }
  155. return discSubfolderCount > 0;
  156. }
  157. }
  158. }