MusicAlbumResolver.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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));
  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)) 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. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  83. private static bool ContainsMusic(IEnumerable<FileSystemInfo> list)
  84. {
  85. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  86. var foundAudio = 0;
  87. foreach (var fileSystemInfo in list)
  88. {
  89. // TODO: Support disc 1, disc 2, etc
  90. if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  91. {
  92. continue;
  93. }
  94. var fullName = fileSystemInfo.FullName;
  95. if (EntityResolutionHelper.IsAudioFile(fullName))
  96. {
  97. // Don't resolve these into audio files
  98. if (string.Equals(Path.GetFileNameWithoutExtension(fullName), BaseItem.ThemeSongFilename) && EntityResolutionHelper.IsAudioFile(fullName))
  99. {
  100. continue;
  101. }
  102. foundAudio++;
  103. }
  104. if (foundAudio >= 2)
  105. {
  106. return true;
  107. }
  108. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  109. if (EntityResolutionHelper.IsVideoPlaceHolder(fullName)) return false;
  110. }
  111. // or a single audio file and no video files
  112. return foundAudio > 0;
  113. }
  114. }
  115. }