MusicAlbumResolver.cs 6.2 KB

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