MusicAlbumResolver.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Controller.Resolvers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Naming.Audio;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using MediaBrowser.Naming.Common;
  16. namespace MediaBrowser.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 { return ResolverPriority.Third; } // we need to be ahead of the generic folder resolver but behind the movie one
  39. }
  40. /// <summary>
  41. /// Resolves the specified args.
  42. /// </summary>
  43. /// <param name="args">The args.</param>
  44. /// <returns>MusicAlbum.</returns>
  45. protected override MusicAlbum Resolve(ItemResolveArgs args)
  46. {
  47. if (!args.IsDirectory) return null;
  48. //Avoid mis-identifying top folders
  49. if (args.Parent == null) return null;
  50. if (args.Parent.IsRoot) return null;
  51. if (args.Parent is MusicAlbum) return null;
  52. // Optimization
  53. if (args.Parent is BoxSet || args.Parent is Series || args.Parent is Season)
  54. {
  55. return null;
  56. }
  57. var collectionType = args.GetCollectionType();
  58. var isMusicMediaFolder = string.Equals(collectionType, CollectionType.Music,
  59. StringComparison.OrdinalIgnoreCase);
  60. // If there's a collection type and it's not music, don't allow it.
  61. if (!isMusicMediaFolder)
  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. /// <param name="path">The path.</param>
  71. /// <param name="directoryService">The directory service.</param>
  72. /// <param name="logger">The logger.</param>
  73. /// <param name="fileSystem">The file system.</param>
  74. /// <param name="libraryManager">The library manager.</param>
  75. /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
  76. public static bool IsMusicAlbum(string path, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem,
  77. ILibraryManager libraryManager)
  78. {
  79. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService, logger, fileSystem, libraryManager);
  80. }
  81. /// <summary>
  82. /// Determine if the supplied resolve args should be considered a music album
  83. /// </summary>
  84. /// <param name="args">The args.</param>
  85. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  86. private bool IsMusicAlbum(ItemResolveArgs args)
  87. {
  88. // Args points to an album if parent is an Artist folder or it directly contains music
  89. if (args.IsDirectory)
  90. {
  91. //if (args.Parent is MusicArtist) return true; //saves us from testing children twice
  92. if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService, _logger, _fileSystem, _libraryManager)) return true;
  93. }
  94. return false;
  95. }
  96. /// <summary>
  97. /// Determine if the supplied list contains what we should consider music
  98. /// </summary>
  99. /// <param name="list">The list.</param>
  100. /// <param name="allowSubfolders">if set to <c>true</c> [allow subfolders].</param>
  101. /// <param name="directoryService">The directory service.</param>
  102. /// <param name="logger">The logger.</param>
  103. /// <param name="fileSystem">The file system.</param>
  104. /// <param name="libraryManager">The library manager.</param>
  105. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  106. private static bool ContainsMusic(IEnumerable<FileSystemInfo> list,
  107. bool allowSubfolders,
  108. IDirectoryService directoryService,
  109. ILogger logger,
  110. IFileSystem fileSystem,
  111. ILibraryManager libraryManager)
  112. {
  113. var discSubfolderCount = 0;
  114. foreach (var fileSystemInfo in list)
  115. {
  116. if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  117. {
  118. if (allowSubfolders && IsAlbumSubfolder(fileSystemInfo, directoryService, logger, fileSystem, libraryManager))
  119. {
  120. discSubfolderCount++;
  121. }
  122. }
  123. var fullName = fileSystemInfo.FullName;
  124. if (libraryManager.IsAudioFile(fullName))
  125. {
  126. // Don't resolve these into audio files
  127. if (string.Equals(fileSystem.GetFileNameWithoutExtension(fullName), BaseItem.ThemeSongFilename))
  128. {
  129. continue;
  130. }
  131. return true;
  132. }
  133. }
  134. return discSubfolderCount > 0;
  135. }
  136. private static bool IsAlbumSubfolder(FileSystemInfo directory, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem, ILibraryManager libraryManager)
  137. {
  138. var path = directory.FullName;
  139. if (IsMultiDiscFolder(path))
  140. {
  141. logger.Debug("Found multi-disc folder: " + path);
  142. return ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService, logger, fileSystem, libraryManager);
  143. }
  144. return false;
  145. }
  146. public static bool IsMultiDiscFolder(string path)
  147. {
  148. return IsMultiDiscAlbumFolder(path);
  149. }
  150. /// <summary>
  151. /// Determines whether [is multi disc album folder] [the specified path].
  152. /// </summary>
  153. /// <param name="path">The path.</param>
  154. /// <returns><c>true</c> if [is multi disc album folder] [the specified path]; otherwise, <c>false</c>.</returns>
  155. private static bool IsMultiDiscAlbumFolder(string path)
  156. {
  157. var parser = new AlbumParser(new ExtendedNamingOptions(), new Naming.Logging.NullLogger());
  158. var result = parser.ParseMultiPart(path);
  159. return result.IsMultiPart;
  160. }
  161. }
  162. }