MusicAlbumResolver.cs 4.4 KB

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