MusicAlbumResolver.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using MediaBrowser.Controller.Entities.Audio;
  2. using MediaBrowser.Controller.Entities.Movies;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Resolvers;
  6. using MediaBrowser.Model.Entities;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio
  11. {
  12. /// <summary>
  13. /// Class MusicAlbumResolver
  14. /// </summary>
  15. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  16. {
  17. /// <summary>
  18. /// Gets the priority.
  19. /// </summary>
  20. /// <value>The priority.</value>
  21. public override ResolverPriority Priority
  22. {
  23. get { return ResolverPriority.Third; } // we need to be ahead of the generic folder resolver but behind the movie one
  24. }
  25. /// <summary>
  26. /// Resolves the specified args.
  27. /// </summary>
  28. /// <param name="args">The args.</param>
  29. /// <returns>MusicAlbum.</returns>
  30. protected override MusicAlbum Resolve(ItemResolveArgs args)
  31. {
  32. if (!args.IsDirectory) return null;
  33. //Avoid mis-identifying top folders
  34. if (args.Parent == null) return null;
  35. if (args.Parent.IsRoot) return null;
  36. if (args.Parent is MusicAlbum) return null;
  37. // Optimization
  38. if (args.Parent is BoxSet || args.Parent is Series || args.Parent is Season)
  39. {
  40. return null;
  41. }
  42. var collectionType = args.GetCollectionType();
  43. // If there's a collection type and it's not music, it can't be a series
  44. if (!string.IsNullOrEmpty(collectionType) &&
  45. !string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
  46. {
  47. return null;
  48. }
  49. return IsMusicAlbum(args) ? new MusicAlbum() : null;
  50. }
  51. /// <summary>
  52. /// Determine if the supplied file data points to a music album
  53. /// </summary>
  54. /// <param name="path">The path.</param>
  55. /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
  56. public static bool IsMusicAlbum(string path)
  57. {
  58. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  59. var foundAudio = 0;
  60. foreach (var fullName in Directory.EnumerateFiles(path))
  61. {
  62. if (EntityResolutionHelper.IsAudioFile(fullName)) foundAudio++;
  63. if (foundAudio >= 2)
  64. {
  65. return true;
  66. }
  67. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  68. }
  69. // or a single audio file and no video files
  70. return foundAudio > 0;
  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. public static 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)) return true;
  84. }
  85. return false;
  86. }
  87. /// <summary>
  88. /// Determine if the supplied list contains what we should consider music
  89. /// </summary>
  90. /// <param name="list">The list.</param>
  91. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  92. public static bool ContainsMusic(IEnumerable<FileSystemInfo> list)
  93. {
  94. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  95. var foundAudio = 0;
  96. foreach (var file in list)
  97. {
  98. var fullName = file.FullName;
  99. if (EntityResolutionHelper.IsAudioFile(fullName)) foundAudio++;
  100. if (foundAudio >= 2)
  101. {
  102. return true;
  103. }
  104. if (EntityResolutionHelper.IsVideoFile(fullName)) return false;
  105. }
  106. // or a single audio file and no video files
  107. return foundAudio > 0;
  108. }
  109. }
  110. }