2
0

MusicAlbumResolver.cs 5.7 KB

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