MusicAlbumResolver.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public bool IsMusicAlbum(string path, IDirectoryService directoryService)
  75. {
  76. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService, _logger, _fileSystem, _libraryManager);
  77. }
  78. /// <summary>
  79. /// Determine if the supplied resolve args should be considered a music album.
  80. /// </summary>
  81. /// <param name="args">The args.</param>
  82. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  83. private bool IsMusicAlbum(ItemResolveArgs args)
  84. {
  85. // Args points to an album if parent is an Artist folder or it directly contains music
  86. if (args.IsDirectory)
  87. {
  88. // if (args.Parent is MusicArtist) return true; // saves us from testing children twice
  89. if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService, _logger, _fileSystem, _libraryManager))
  90. {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /// <summary>
  97. /// Determine if the supplied list contains what we should consider music.
  98. /// </summary>
  99. private bool ContainsMusic(
  100. IEnumerable<FileSystemMetadata> list,
  101. bool allowSubfolders,
  102. IDirectoryService directoryService,
  103. ILogger<MusicAlbumResolver> logger,
  104. IFileSystem fileSystem,
  105. ILibraryManager libraryManager)
  106. {
  107. // check for audio files before digging down into directories
  108. var foundAudioFile = list.Any(fileSystemInfo => !fileSystemInfo.IsDirectory && libraryManager.IsAudioFile(fileSystemInfo.FullName));
  109. if (foundAudioFile)
  110. {
  111. // at least one audio file exists
  112. return true;
  113. }
  114. if (!allowSubfolders)
  115. {
  116. // not music since no audio file exists and we're not looking into subfolders
  117. return false;
  118. }
  119. var discSubfolderCount = 0;
  120. var namingOptions = ((LibraryManager)_libraryManager).GetNamingOptions();
  121. var parser = new AlbumParser(namingOptions);
  122. var directories = list.Where(fileSystemInfo => fileSystemInfo.IsDirectory);
  123. var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
  124. {
  125. var path = fileSystemInfo.FullName;
  126. var hasMusic = ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService, logger, fileSystem, libraryManager);
  127. if (hasMusic)
  128. {
  129. if (parser.IsMultiPart(path))
  130. {
  131. logger.LogDebug("Found multi-disc folder: " + path);
  132. Interlocked.Increment(ref discSubfolderCount);
  133. }
  134. else
  135. {
  136. // If there are folders underneath with music that are not multidisc, then this can't be a multi-disc album
  137. state.Stop();
  138. }
  139. }
  140. });
  141. if (!result.IsCompleted)
  142. {
  143. return false;
  144. }
  145. return discSubfolderCount > 0;
  146. }
  147. }
  148. }