MusicAlbumResolver.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  62. var foundAudio = 0;
  63. foreach (var file in directoryService.GetFiles(path))
  64. {
  65. var fullName = file.FullName;
  66. if (EntityResolutionHelper.IsAudioFile(fullName))
  67. {
  68. // Don't resolve these into audio files
  69. if (string.Equals(Path.GetFileNameWithoutExtension(fullName), BaseItem.ThemeSongFilename) && EntityResolutionHelper.IsAudioFile(fullName))
  70. {
  71. continue;
  72. }
  73. foundAudio++;
  74. }
  75. if (foundAudio >= 2)
  76. {
  77. return true;
  78. }
  79. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  80. }
  81. // or a single audio file and no video files
  82. return foundAudio > 0;
  83. }
  84. /// <summary>
  85. /// Determine if the supplied resolve args should be considered a music album
  86. /// </summary>
  87. /// <param name="args">The args.</param>
  88. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  89. public static bool IsMusicAlbum(ItemResolveArgs args)
  90. {
  91. // Args points to an album if parent is an Artist folder or it directly contains music
  92. if (args.IsDirectory)
  93. {
  94. //if (args.Parent is MusicArtist) return true; //saves us from testing children twice
  95. if (ContainsMusic(args.FileSystemChildren)) return true;
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// Determine if the supplied list contains what we should consider music
  101. /// </summary>
  102. /// <param name="list">The list.</param>
  103. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  104. public static bool ContainsMusic(IEnumerable<FileSystemInfo> list)
  105. {
  106. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  107. var foundAudio = 0;
  108. foreach (var file in list)
  109. {
  110. var fullName = file.FullName;
  111. if (EntityResolutionHelper.IsAudioFile(fullName)) foundAudio++;
  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. }
  123. }