MusicAlbumResolver.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio
  15. {
  16. /// <summary>
  17. /// Class MusicAlbumResolver
  18. /// </summary>
  19. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  20. {
  21. private readonly ILogger _logger;
  22. private readonly IFileSystem _fileSystem;
  23. public MusicAlbumResolver(ILogger logger, IFileSystem fileSystem)
  24. {
  25. _logger = logger;
  26. _fileSystem = fileSystem;
  27. }
  28. /// <summary>
  29. /// Gets the priority.
  30. /// </summary>
  31. /// <value>The priority.</value>
  32. public override ResolverPriority Priority
  33. {
  34. get { return ResolverPriority.Third; } // we need to be ahead of the generic folder resolver but behind the movie one
  35. }
  36. /// <summary>
  37. /// Resolves the specified args.
  38. /// </summary>
  39. /// <param name="args">The args.</param>
  40. /// <returns>MusicAlbum.</returns>
  41. protected override MusicAlbum Resolve(ItemResolveArgs args)
  42. {
  43. if (!args.IsDirectory) return null;
  44. //Avoid mis-identifying top folders
  45. if (args.Parent == null) return null;
  46. if (args.Parent.IsRoot) return null;
  47. if (args.Parent is MusicAlbum) return null;
  48. // Optimization
  49. if (args.Parent is BoxSet || args.Parent is Series || args.Parent is Season)
  50. {
  51. return null;
  52. }
  53. var collectionType = args.GetCollectionType();
  54. // If there's a collection type and it's not music, don't allow it.
  55. if (!string.IsNullOrEmpty(collectionType) &&
  56. !string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
  57. {
  58. return null;
  59. }
  60. return IsMusicAlbum(args) ? new MusicAlbum() : null;
  61. }
  62. /// <summary>
  63. /// Determine if the supplied file data points to a music album
  64. /// </summary>
  65. /// <param name="path">The path.</param>
  66. /// <param name="directoryService">The directory service.</param>
  67. /// <param name="logger">The logger.</param>
  68. /// <param name="fileSystem">The file system.</param>
  69. /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
  70. public static bool IsMusicAlbum(string path, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem)
  71. {
  72. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService, logger, fileSystem);
  73. }
  74. /// <summary>
  75. /// Determine if the supplied resolve args should be considered a music album
  76. /// </summary>
  77. /// <param name="args">The args.</param>
  78. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  79. private bool IsMusicAlbum(ItemResolveArgs args)
  80. {
  81. // Args points to an album if parent is an Artist folder or it directly contains music
  82. if (args.IsDirectory)
  83. {
  84. //if (args.Parent is MusicArtist) return true; //saves us from testing children twice
  85. if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService, _logger, _fileSystem)) return true;
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// Determine if the supplied list contains what we should consider music
  91. /// </summary>
  92. /// <param name="list">The list.</param>
  93. /// <param name="allowSubfolders">if set to <c>true</c> [allow subfolders].</param>
  94. /// <param name="directoryService">The directory service.</param>
  95. /// <param name="logger">The logger.</param>
  96. /// <param name="fileSystem">The file system.</param>
  97. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  98. private static bool ContainsMusic(IEnumerable<FileSystemInfo> list, bool allowSubfolders, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem)
  99. {
  100. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  101. var foundAudio = 0;
  102. var discSubfolderCount = 0;
  103. foreach (var fileSystemInfo in list)
  104. {
  105. if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  106. {
  107. if (allowSubfolders && IsAlbumSubfolder(fileSystemInfo, directoryService, logger, fileSystem))
  108. {
  109. discSubfolderCount++;
  110. }
  111. if (!IsAdditionalSubfolderAllowed(fileSystemInfo))
  112. {
  113. return false;
  114. }
  115. }
  116. var fullName = fileSystemInfo.FullName;
  117. if (EntityResolutionHelper.IsAudioFile(fullName))
  118. {
  119. // Don't resolve these into audio files
  120. if (string.Equals(fileSystem.GetFileNameWithoutExtension(fullName), BaseItem.ThemeSongFilename) && EntityResolutionHelper.IsAudioFile(fullName))
  121. {
  122. continue;
  123. }
  124. foundAudio++;
  125. }
  126. if (foundAudio >= 2)
  127. {
  128. return true;
  129. }
  130. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  131. if (EntityResolutionHelper.IsVideoPlaceHolder(fullName)) return false;
  132. }
  133. // or a single audio file and no video files
  134. return foundAudio > 0 || discSubfolderCount > 0;
  135. }
  136. private static bool IsAlbumSubfolder(FileSystemInfo directory, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem)
  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);
  143. }
  144. return false;
  145. }
  146. private static bool IsMultiDiscFolder(string path)
  147. {
  148. return EntityResolutionHelper.IsMultiPartFolder(path);
  149. }
  150. private static bool IsAdditionalSubfolderAllowed(FileSystemInfo directory)
  151. {
  152. // Resolver will ignore them based on rules engine
  153. return true;
  154. }
  155. }
  156. }