MusicAlbumResolver.cs 6.1 KB

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