MusicAlbumResolver.cs 6.0 KB

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