MusicAlbumResolver.cs 5.8 KB

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