#nullable disable
using System;
using System.Linq;
using System.Threading.Tasks;
using Emby.Naming.Common;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Library.Resolvers.Audio
{
///
/// Class MusicArtistResolver.
///
public class MusicArtistResolver : ItemResolver
{
private readonly ILogger _logger;
private NamingOptions _namingOptions;
///
/// Initializes a new instance of the class.
///
/// The logger for the created instances.
/// The naming options.
public MusicArtistResolver(
ILogger logger,
NamingOptions namingOptions)
{
_logger = logger;
_namingOptions = namingOptions;
}
///
/// Gets the priority.
///
/// The priority.
public override ResolverPriority Priority => ResolverPriority.Second;
///
/// Resolves the specified args.
///
/// The args.
/// MusicArtist.
protected override MusicArtist Resolve(ItemResolveArgs args)
{
if (!args.IsDirectory)
{
return null;
}
// Don't allow nested artists
if (args.HasParent() || args.HasParent())
{
return null;
}
var collectionType = args.GetCollectionType();
var isMusicMediaFolder = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
// If there's a collection type and it's not music, it can't be a series
if (!isMusicMediaFolder)
{
return null;
}
if (args.ContainsFileSystemEntryByName("artist.nfo"))
{
return new MusicArtist();
}
// Avoid mis-identifying top folders
if (args.Parent.IsRoot)
{
return null;
}
var directoryService = args.DirectoryService;
var albumResolver = new MusicAlbumResolver(_logger, _namingOptions);
// If we contain an album assume we are an artist folder
var directories = args.FileSystemChildren.Where(i => i.IsDirectory);
var result = Parallel.ForEach(directories, (fileSystemInfo, state) =>
{
if (albumResolver.IsMusicAlbum(fileSystemInfo.FullName, directoryService))
{
// stop once we see a music album
state.Stop();
}
});
return !result.IsCompleted ? new MusicArtist() : null;
}
}
}