MusicAlbumResolver.cs 6.4 KB

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