MusicAlbumResolver.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using MediaBrowser.Controller.Entities.Audio;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Resolvers;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio
  8. {
  9. /// <summary>
  10. /// Class MusicAlbumResolver
  11. /// </summary>
  12. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  13. {
  14. /// <summary>
  15. /// Gets the priority.
  16. /// </summary>
  17. /// <value>The priority.</value>
  18. public override ResolverPriority Priority
  19. {
  20. get { return ResolverPriority.Third; } // we need to be ahead of the generic folder resolver but behind the movie one
  21. }
  22. /// <summary>
  23. /// Resolves the specified args.
  24. /// </summary>
  25. /// <param name="args">The args.</param>
  26. /// <returns>MusicAlbum.</returns>
  27. protected override MusicAlbum Resolve(ItemResolveArgs args)
  28. {
  29. if (!args.IsDirectory) return null;
  30. //Avoid mis-identifying top folders
  31. if (args.Parent == null) return null;
  32. if (args.Parent.IsRoot) return null;
  33. if (args.Parent is MusicAlbum) return null;
  34. return IsMusicAlbum(args) ? new MusicAlbum
  35. {
  36. DisplayMediaType = "Album"
  37. } : null;
  38. }
  39. /// <summary>
  40. /// Determine if the supplied file data points to a music album
  41. /// </summary>
  42. /// <param name="path">The path.</param>
  43. /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
  44. public static bool IsMusicAlbum(string path)
  45. {
  46. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  47. var foundAudio = 0;
  48. foreach (var fullName in Directory.EnumerateFiles(path))
  49. {
  50. if (EntityResolutionHelper.IsAudioFile(fullName)) foundAudio++;
  51. if (foundAudio >= 2)
  52. {
  53. return true;
  54. }
  55. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  56. }
  57. // or a single audio file and no video files
  58. return foundAudio > 0;
  59. }
  60. /// <summary>
  61. /// Determine if the supplied resolve args should be considered a music album
  62. /// </summary>
  63. /// <param name="args">The args.</param>
  64. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  65. public static bool IsMusicAlbum(ItemResolveArgs args)
  66. {
  67. // Args points to an album if parent is an Artist folder or it directly contains music
  68. if (args.IsDirectory)
  69. {
  70. //if (args.Parent is MusicArtist) return true; //saves us from testing children twice
  71. if (ContainsMusic(args.FileSystemChildren)) return true;
  72. }
  73. return false;
  74. }
  75. /// <summary>
  76. /// Determine if the supplied list contains what we should consider music
  77. /// </summary>
  78. /// <param name="list">The list.</param>
  79. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  80. public static bool ContainsMusic(IEnumerable<FileSystemInfo> list)
  81. {
  82. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  83. var foundAudio = 0;
  84. foreach (var fullName in list.Select(file => file.FullName))
  85. {
  86. if (EntityResolutionHelper.IsAudioFile(fullName)) foundAudio++;
  87. if (foundAudio >= 2)
  88. {
  89. return true;
  90. }
  91. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  92. }
  93. // or a single audio file and no video files
  94. return foundAudio > 0;
  95. }
  96. }
  97. }