MusicAlbumResolver.cs 6.2 KB

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