MusicAlbumResolver.cs 5.0 KB

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