MusicAlbumResolver.cs 4.6 KB

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