jellyfin/Emby.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs

114 lines
3.9 KiB
C#
Raw Normal View History

#nullable disable
using System;
using System.Linq;
using System.Threading.Tasks;
using Emby.Naming.Common;
using MediaBrowser.Controller.Entities.Audio;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
2013-02-21 02:33:05 +01:00
namespace Emby.Server.Implementations.Library.Resolvers.Audio
2013-02-21 02:33:05 +01:00
{
2013-02-23 08:57:11 +01:00
/// <summary>
2022-03-31 16:17:37 +02:00
/// The music artist resolver.
2013-02-23 08:57:11 +01:00
/// </summary>
public class MusicArtistResolver : ItemResolver<MusicArtist>
2013-02-21 02:33:05 +01:00
{
private readonly ILogger<MusicAlbumResolver> _logger;
private readonly NamingOptions _namingOptions;
private readonly IDirectoryService _directoryService;
2014-07-26 19:30:15 +02:00
2019-11-01 18:38:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="MusicArtistResolver"/> class.
/// </summary>
2022-03-31 16:17:37 +02:00
/// <param name="logger">Instance of the <see cref="MusicAlbumResolver"/> interface.</param>
/// <param name="namingOptions">The <see cref="NamingOptions"/>.</param>
/// <param name="directoryService">The directory service.</param>
public MusicArtistResolver(
ILogger<MusicAlbumResolver> logger,
NamingOptions namingOptions,
IDirectoryService directoryService)
2014-07-26 19:30:15 +02:00
{
_logger = logger;
_namingOptions = namingOptions;
_directoryService = directoryService;
2014-07-26 19:30:15 +02:00
}
2013-02-23 08:57:11 +01:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority => ResolverPriority.Second;
2013-02-21 02:33:05 +01:00
2013-02-23 08:57:11 +01:00
/// <summary>
2022-03-31 16:17:37 +02:00
/// Resolves the specified resolver arguments.
2013-02-23 08:57:11 +01:00
/// </summary>
2022-03-31 16:17:37 +02:00
/// <param name="args">The resolver arguments.</param>
/// <returns>A <see cref="MusicArtist"/>.</returns>
2013-02-21 02:33:05 +01:00
protected override MusicArtist Resolve(ItemResolveArgs args)
{
2019-11-01 18:38:54 +01:00
if (!args.IsDirectory)
{
return null;
}
2013-02-21 02:33:05 +01:00
2013-06-02 14:43:53 +02:00
// Don't allow nested artists
2014-12-15 06:16:23 +01:00
if (args.HasParent<MusicArtist>() || args.HasParent<MusicAlbum>())
2013-06-02 14:43:53 +02:00
{
return null;
}
var collectionType = args.GetCollectionType();
2014-12-20 07:06:27 +01:00
var isMusicMediaFolder = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
2014-07-30 05:31:35 +02:00
// If there's a collection type and it's not music, it can't be a music artist
2014-07-30 05:31:35 +02:00
if (!isMusicMediaFolder)
{
return null;
}
2014-02-13 06:11:54 +01:00
2016-10-09 09:18:43 +02:00
if (args.ContainsFileSystemEntryByName("artist.nfo"))
2016-10-07 17:08:13 +02:00
{
2016-10-09 09:18:43 +02:00
return new MusicArtist();
2016-10-07 17:08:13 +02:00
}
2017-02-08 19:50:33 +01:00
// Avoid mis-identifying top folders
2020-02-19 21:56:35 +01:00
if (args.Parent.IsRoot)
{
return null;
}
2016-10-09 09:18:43 +02:00
var albumResolver = new MusicAlbumResolver(_logger, _namingOptions, _directoryService);
2015-01-10 06:53:35 +01:00
var directories = args.FileSystemChildren.Where(i => i.IsDirectory);
var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
{
2022-03-31 16:17:37 +02:00
// If we contain a artist subfolder assume we are an artist folder
foreach (var subfolder in _namingOptions.ArtistSubfolders)
{
if (fileSystemInfo.Name.Equals(subfolder, StringComparison.OrdinalIgnoreCase))
{
2022-03-31 16:17:37 +02:00
// Stop once we see an artist subfolder
state.Stop();
}
}
2022-03-31 16:17:37 +02:00
// If we contain a music album assume we are an artist folder
if (albumResolver.IsMusicAlbum(fileSystemInfo.FullName, _directoryService))
{
2022-03-31 16:17:37 +02:00
// Stop once we see a music album
state.Stop();
}
});
return !result.IsCompleted ? new MusicArtist() : null;
2013-02-21 02:33:05 +01:00
}
}
}