| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Emby.Naming.Audio; |
| | | 7 | | using Emby.Naming.Common; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 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 Microsoft.Extensions.Logging; |
| | | 15 | | |
| | | 16 | | namespace Emby.Server.Implementations.Library.Resolvers.Audio |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// The music artist resolver. |
| | | 20 | | /// </summary> |
| | | 21 | | public class MusicArtistResolver : ItemResolver<MusicArtist> |
| | | 22 | | { |
| | | 23 | | private readonly ILogger<MusicAlbumResolver> _logger; |
| | | 24 | | private readonly NamingOptions _namingOptions; |
| | | 25 | | private readonly IDirectoryService _directoryService; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="MusicArtistResolver"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="logger">Instance of the <see cref="MusicAlbumResolver"/> interface.</param> |
| | | 31 | | /// <param name="namingOptions">The <see cref="NamingOptions"/>.</param> |
| | | 32 | | /// <param name="directoryService">The directory service.</param> |
| | 21 | 33 | | public MusicArtistResolver( |
| | 21 | 34 | | ILogger<MusicAlbumResolver> logger, |
| | 21 | 35 | | NamingOptions namingOptions, |
| | 21 | 36 | | IDirectoryService directoryService) |
| | | 37 | | { |
| | 21 | 38 | | _logger = logger; |
| | 21 | 39 | | _namingOptions = namingOptions; |
| | 21 | 40 | | _directoryService = directoryService; |
| | 21 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the priority. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <value>The priority.</value> |
| | 21 | 47 | | public override ResolverPriority Priority => ResolverPriority.Second; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Resolves the specified resolver arguments. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="args">The resolver arguments.</param> |
| | | 53 | | /// <returns>A <see cref="MusicArtist"/>.</returns> |
| | | 54 | | protected override MusicArtist Resolve(ItemResolveArgs args) |
| | | 55 | | { |
| | 10 | 56 | | if (!args.IsDirectory) |
| | | 57 | | { |
| | 10 | 58 | | return null; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | // Don't allow nested artists |
| | 0 | 62 | | if (args.HasParent<MusicArtist>() || args.HasParent<MusicAlbum>()) |
| | | 63 | | { |
| | 0 | 64 | | return null; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | var collectionType = args.GetCollectionType(); |
| | | 68 | | |
| | 0 | 69 | | var isMusicMediaFolder = collectionType == CollectionType.music; |
| | | 70 | | |
| | | 71 | | // If there's a collection type and it's not music, it can't be a music artist |
| | 0 | 72 | | if (!isMusicMediaFolder) |
| | | 73 | | { |
| | 0 | 74 | | return null; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (args.ContainsFileSystemEntryByName("artist.nfo")) |
| | | 78 | | { |
| | 0 | 79 | | return new MusicArtist(); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // Avoid mis-identifying top folders |
| | 0 | 83 | | if (args.Parent.IsRoot) |
| | | 84 | | { |
| | 0 | 85 | | return null; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | var albumResolver = new MusicAlbumResolver(_logger, _namingOptions, _directoryService); |
| | 0 | 89 | | var albumParser = new AlbumParser(_namingOptions); |
| | | 90 | | |
| | 0 | 91 | | var directories = args.FileSystemChildren.Where(i => i.IsDirectory); |
| | | 92 | | |
| | 0 | 93 | | var result = Parallel.ForEach(directories, (fileSystemInfo, state) => |
| | 0 | 94 | | { |
| | 0 | 95 | | // If we contain a artist subfolder assume we are an artist folder |
| | 0 | 96 | | foreach (var subfolder in _namingOptions.ArtistSubfolders) |
| | 0 | 97 | | { |
| | 0 | 98 | | if (fileSystemInfo.Name.Equals(subfolder, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 99 | | { |
| | 0 | 100 | | // Stop once we see an artist subfolder |
| | 0 | 101 | | state.Stop(); |
| | 0 | 102 | | } |
| | 0 | 103 | | } |
| | 0 | 104 | | |
| | 0 | 105 | | // If the folder is a multi-disc folder, then it is not an artist folder |
| | 0 | 106 | | if (albumParser.IsMultiPart(fileSystemInfo.FullName)) |
| | 0 | 107 | | { |
| | 0 | 108 | | return; |
| | 0 | 109 | | } |
| | 0 | 110 | | |
| | 0 | 111 | | // If we contain a music album assume we are an artist folder |
| | 0 | 112 | | if (albumResolver.IsMusicAlbum(fileSystemInfo.FullName, _directoryService)) |
| | 0 | 113 | | { |
| | 0 | 114 | | // Stop once we see a music album |
| | 0 | 115 | | state.Stop(); |
| | 0 | 116 | | } |
| | 0 | 117 | | }); |
| | | 118 | | |
| | 0 | 119 | | return !result.IsCompleted ? new MusicArtist() : null; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |