2
0

MusicAlbumResolver.cs 7.0 KB

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