MusicAlbumResolver.cs 5.9 KB

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