MusicArtistResolver.cs 3.2 KB

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