MusicAlbumResolver.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Emby.Naming.Audio;
  8. using Emby.Naming.Common;
  9. using MediaBrowser.Controller.Entities.Audio;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Controller.Resolvers;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.IO;
  15. using Microsoft.Extensions.Logging;
  16. namespace Emby.Server.Implementations.Library.Resolvers.Audio
  17. {
  18. /// <summary>
  19. /// Class MusicAlbumResolver.
  20. /// </summary>
  21. public class MusicAlbumResolver : ItemResolver<MusicAlbum>
  22. {
  23. private readonly ILogger<MusicAlbumResolver> _logger;
  24. private readonly NamingOptions _namingOptions;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="MusicAlbumResolver"/> class.
  27. /// </summary>
  28. /// <param name="logger">The logger.</param>
  29. /// <param name="namingOptions">The naming options.</param>
  30. public MusicAlbumResolver(ILogger<MusicAlbumResolver> logger, NamingOptions namingOptions)
  31. {
  32. _logger = logger;
  33. _namingOptions = namingOptions;
  34. }
  35. /// <summary>
  36. /// Gets the priority.
  37. /// </summary>
  38. /// <value>The priority.</value>
  39. public override ResolverPriority Priority => ResolverPriority.Third;
  40. /// <summary>
  41. /// Resolves the specified args.
  42. /// </summary>
  43. /// <param name="args">The args.</param>
  44. /// <returns>MusicAlbum.</returns>
  45. protected override MusicAlbum Resolve(ItemResolveArgs args)
  46. {
  47. var collectionType = args.GetCollectionType();
  48. var isMusicMediaFolder = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
  49. // If there's a collection type and it's not music, don't allow it.
  50. if (!isMusicMediaFolder)
  51. {
  52. return null;
  53. }
  54. if (!args.IsDirectory)
  55. {
  56. return null;
  57. }
  58. // Avoid mis-identifying top folders
  59. if (args.HasParent<MusicAlbum>())
  60. {
  61. return null;
  62. }
  63. if (args.Parent.IsRoot)
  64. {
  65. return null;
  66. }
  67. return IsMusicAlbum(args) ? new MusicAlbum() : null;
  68. }
  69. /// <summary>
  70. /// Determine if the supplied file data points to a music album.
  71. /// </summary>
  72. /// <param name="path">The path to check.</param>
  73. /// <param name="directoryService">The directory service.</param>
  74. /// <returns><c>true</c> if the provided path points to a music album, <c>false</c> otherwise.</returns>
  75. public bool IsMusicAlbum(string path, IDirectoryService directoryService)
  76. {
  77. return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService);
  78. }
  79. /// <summary>
  80. /// Determine if the supplied resolve args should be considered a music album.
  81. /// </summary>
  82. /// <param name="args">The args.</param>
  83. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  84. private bool IsMusicAlbum(ItemResolveArgs args)
  85. {
  86. // Args points to an album if parent is an Artist folder or it directly contains music
  87. if (args.IsDirectory)
  88. {
  89. // if (args.Parent is MusicArtist) return true; // saves us from testing children twice
  90. if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService))
  91. {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. /// <summary>
  98. /// Determine if the supplied list contains what we should consider music.
  99. /// </summary>
  100. private bool ContainsMusic(
  101. ICollection<FileSystemMetadata> list,
  102. bool allowSubfolders,
  103. IDirectoryService directoryService)
  104. {
  105. // check for audio files before digging down into directories
  106. var foundAudioFile = list.Any(fileSystemInfo => !fileSystemInfo.IsDirectory && AudioFileParser.IsAudioFile(fileSystemInfo.FullName, _namingOptions));
  107. if (foundAudioFile)
  108. {
  109. // at least one audio file exists
  110. return true;
  111. }
  112. if (!allowSubfolders)
  113. {
  114. // not music since no audio file exists and we're not looking into subfolders
  115. return false;
  116. }
  117. var discSubfolderCount = 0;
  118. var parser = new AlbumParser(_namingOptions);
  119. var directories = list.Where(fileSystemInfo => fileSystemInfo.IsDirectory);
  120. var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
  121. {
  122. var path = fileSystemInfo.FullName;
  123. var hasMusic = ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService);
  124. if (hasMusic)
  125. {
  126. if (parser.IsMultiPart(path))
  127. {
  128. _logger.LogDebug("Found multi-disc folder: {Path}", path);
  129. Interlocked.Increment(ref discSubfolderCount);
  130. }
  131. else
  132. {
  133. // If there are folders underneath with music that are not multidisc, then this can't be a multi-disc album
  134. state.Stop();
  135. }
  136. }
  137. });
  138. if (!result.IsCompleted)
  139. {
  140. return false;
  141. }
  142. return discSubfolderCount > 0;
  143. }
  144. }
  145. }