MusicAlbumResolver.cs 6.1 KB

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