MusicAlbumResolver.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Naming.Audio;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Controller.Resolvers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.IO;
  13. using Microsoft.Extensions.Logging;
  14. namespace Emby.Server.Implementations.Library.Resolvers.Audio
  15. {
  16. /// <summary>
  17. /// Class MusicAlbumResolver.
  18. /// </summary>
  19. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  20. {
  21. private readonly ILogger<MusicAlbumResolver> _logger;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly ILibraryManager _libraryManager;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="MusicAlbumResolver"/> class.
  26. /// </summary>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="fileSystem">The file system.</param>
  29. /// <param name="libraryManager">The library manager.</param>
  30. public MusicAlbumResolver(ILogger<MusicAlbumResolver> logger, IFileSystem fileSystem, ILibraryManager libraryManager)
  31. {
  32. _logger = logger;
  33. _fileSystem = fileSystem;
  34. _libraryManager = libraryManager;
  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. public bool IsMusicAlbum(string path, IDirectoryService directoryService)
  74. {
  75. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService, _logger, _fileSystem, _libraryManager);
  76. }
  77. /// <summary>
  78. /// Determine if the supplied resolve args should be considered a music album.
  79. /// </summary>
  80. /// <param name="args">The args.</param>
  81. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  82. private bool IsMusicAlbum(ItemResolveArgs args)
  83. {
  84. // Args points to an album if parent is an Artist folder or it directly contains music
  85. if (args.IsDirectory)
  86. {
  87. // if (args.Parent is MusicArtist) return true; // saves us from testing children twice
  88. if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService, _logger, _fileSystem, _libraryManager))
  89. {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. /// Determine if the supplied list contains what we should consider music.
  97. /// </summary>
  98. private bool ContainsMusic(
  99. IEnumerable<FileSystemMetadata> list,
  100. bool allowSubfolders,
  101. IDirectoryService directoryService,
  102. ILogger<MusicAlbumResolver> logger,
  103. IFileSystem fileSystem,
  104. ILibraryManager libraryManager)
  105. {
  106. // check for audio files before digging down into directories
  107. var foundAudioFile = list.Any(fileSystemInfo => !fileSystemInfo.IsDirectory && libraryManager.IsAudioFile(fileSystemInfo.FullName));
  108. if (foundAudioFile)
  109. {
  110. // at least one audio file exists
  111. return true;
  112. }
  113. if (!allowSubfolders)
  114. {
  115. // not music since no audio file exists and we're not looking into subfolders
  116. return false;
  117. }
  118. var discSubfolderCount = 0;
  119. var namingOptions = ((LibraryManager)_libraryManager).GetNamingOptions();
  120. var parser = new AlbumParser(namingOptions);
  121. var directories = list.Where(fileSystemInfo => fileSystemInfo.IsDirectory);
  122. var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
  123. {
  124. var path = fileSystemInfo.FullName;
  125. var hasMusic = ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService, logger, fileSystem, libraryManager);
  126. if (hasMusic)
  127. {
  128. if (parser.IsMultiPart(path))
  129. {
  130. logger.LogDebug("Found multi-disc folder: " + path);
  131. Interlocked.Increment(ref discSubfolderCount);
  132. }
  133. else
  134. {
  135. // If there are folders underneath with music that are not multidisc, then this can't be a multi-disc album
  136. state.Stop();
  137. }
  138. }
  139. });
  140. if (!result.IsCompleted)
  141. {
  142. return false;
  143. }
  144. return discSubfolderCount > 0;
  145. }
  146. }
  147. }