MusicAlbumResolver.cs 6.2 KB

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