#region ========================================================================= USING ===================================================================================== using Nemera.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary; using Nemera.Domain.SharedKernel.Common.Enums.MediaLibrary; using Nemera.Plugins.Contracts.Core.Metadata; using Nemera.Plugins.ID3.Common.Models.DTO.Tags; using Nemera.Plugins.ID3.Core.Mapping; using Nemera.Plugins.ID3.Core.Tags; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; #endregion namespace Nemera.Plugins.ID3.Core; /// /// Provides music artist metadata from the embedded tags of one of its audio files, by resolving lookups into artist metadata DTOs. /// internal sealed class Id3ArtistMetadataProvider : IMetadataProvider { private readonly Id3TagReader _tagReader; /// /// Gets the display name of the metadata provider. /// public string Name => "ID3"; /// /// Gets the media library types this metadata provider supports. /// public IReadOnlyList SupportedLibraryTypes => [LibraryType.Music]; /// /// Gets a value indicating whether this metadata provider requires access to the web to retrieve metadata. /// public bool RequiresWebAccess => false; /// /// Initializes a new instance of the class. /// /// The reader of the embedded tags of the audio files. public Id3ArtistMetadataProvider(Id3TagReader tagReader) { _tagReader = tagReader; } /// /// Searches for the metadata of the artist described by . /// /// The lookup describing the artist to search for. /// Cancellation token that can be used to stop the execution. /// The collection of artist metadata candidates found, which for embedded tags is at most the file of the lookup. public async Task> GetSearchResultsAsync(ArtistMetadataLookupDto lookup, CancellationToken cancellationToken) { ArtistMetadataDto? metadata = await ReadMetadataAsync(lookup, cancellationToken).ConfigureAwait(false); IReadOnlyList results = metadata is null ? [] : [metadata]; return results; } /// /// Gets the metadata of the artist described by . /// /// The lookup describing the artist to get the metadata for. /// Cancellation token that can be used to stop the execution. /// The metadata of the artist, or when the file carries none. public Task GetMetadataAsync(ArtistMetadataLookupDto lookup, CancellationToken cancellationToken) { return ReadMetadataAsync(lookup, cancellationToken); } /// /// Reads the metadata of the artist described by from the embedded tags of the track stored at its path. /// /// The lookup describing the artist. /// Cancellation token that can be used to stop the execution. /// The metadata of the artist, or when the tags carry no artist name. private Task ReadMetadataAsync(ArtistMetadataLookupDto lookup, CancellationToken cancellationToken) { // The tagging library exposes no asynchronous API, so the blocking read of the file is offloaded so that a scan does not block on the tags of each file. return Task.Run(() => { Id3TagDto? data = _tagReader.Read(lookup.Path); if (data is null || (data.AlbumArtists.Count == 0 && data.TrackArtists.Count == 0)) return null; return Id3TagMapper.MapArtist(data, lookup.Name); }, cancellationToken); } }