MusicArtistResolver.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #nullable disable
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Emby.Naming.Common;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Resolvers;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.IO;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.Library.Resolvers.Audio
  14. {
  15. /// <summary>
  16. /// Class MusicArtistResolver.
  17. /// </summary>
  18. public class MusicArtistResolver : ItemResolver<MusicArtist>
  19. {
  20. private readonly ILogger<MusicAlbumResolver> _logger;
  21. private NamingOptions _namingOptions;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="MusicArtistResolver"/> class.
  24. /// </summary>
  25. /// <param name="logger">The logger for the created <see cref="MusicAlbumResolver"/> instances.</param>
  26. /// <param name="namingOptions">The naming options.</param>
  27. public MusicArtistResolver(
  28. ILogger<MusicAlbumResolver> logger,
  29. NamingOptions namingOptions)
  30. {
  31. _logger = logger;
  32. _namingOptions = namingOptions;
  33. }
  34. /// <summary>
  35. /// Gets the priority.
  36. /// </summary>
  37. /// <value>The priority.</value>
  38. public override ResolverPriority Priority => ResolverPriority.Second;
  39. /// <summary>
  40. /// Resolves the specified args.
  41. /// </summary>
  42. /// <param name="args">The args.</param>
  43. /// <returns>MusicArtist.</returns>
  44. protected override MusicArtist Resolve(ItemResolveArgs args)
  45. {
  46. if (!args.IsDirectory)
  47. {
  48. return null;
  49. }
  50. // Don't allow nested artists
  51. if (args.HasParent<MusicArtist>() || args.HasParent<MusicAlbum>())
  52. {
  53. return null;
  54. }
  55. var collectionType = args.GetCollectionType();
  56. var isMusicMediaFolder = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
  57. // If there's a collection type and it's not music, it can't be a series
  58. if (!isMusicMediaFolder)
  59. {
  60. return null;
  61. }
  62. if (args.ContainsFileSystemEntryByName("artist.nfo"))
  63. {
  64. return new MusicArtist();
  65. }
  66. // Avoid mis-identifying top folders
  67. if (args.Parent.IsRoot)
  68. {
  69. return null;
  70. }
  71. var directoryService = args.DirectoryService;
  72. var albumResolver = new MusicAlbumResolver(_logger, _namingOptions);
  73. // If we contain an album assume we are an artist folder
  74. var directories = args.FileSystemChildren.Where(i => i.IsDirectory);
  75. var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
  76. {
  77. if (albumResolver.IsMusicAlbum(fileSystemInfo.FullName, directoryService))
  78. {
  79. // stop once we see a music album
  80. state.Stop();
  81. }
  82. });
  83. return !result.IsCompleted ? new MusicArtist() : null;
  84. }
  85. }
  86. }