MusicAlbumResolver.cs 6.2 KB

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