#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary; using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; using Lumina.Plugins.Contracts.Core.Metadata; using Lumina.Plugins.ID3.Common.Models.DTO.Tags; using Lumina.Plugins.ID3.Core.Mapping; using Lumina.Plugins.ID3.Core.Tags; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Plugins.ID3.Core; /// /// Provides album metadata from the embedded tags of one of its audio files, by resolving lookups into album metadata DTOs. /// internal sealed class Id3AlbumMetadataProvider : 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 Id3AlbumMetadataProvider(Id3TagReader tagReader) { _tagReader = tagReader; } /// /// Searches for the metadata of the album described by . /// /// The lookup describing the album to search for. /// Cancellation token that can be used to stop the execution. /// The collection of album metadata candidates found, which for embedded tags is at most the file of the lookup. public async Task> GetSearchResultsAsync(AlbumMetadataLookupDto lookup, CancellationToken cancellationToken) { AlbumMetadataDto? metadata = await ReadMetadataAsync(lookup.Path, cancellationToken).ConfigureAwait(false); IReadOnlyList results = metadata is null ? [] : [metadata]; return results; } /// /// Gets the metadata of the album described by . /// /// The lookup describing the album to get the metadata for. /// Cancellation token that can be used to stop the execution. /// The metadata of the album, or when the file carries none. public Task GetMetadataAsync(AlbumMetadataLookupDto lookup, CancellationToken cancellationToken) { return ReadMetadataAsync(lookup.Path, cancellationToken); } /// /// Reads the metadata of the album from the embedded tags of the track stored at . /// /// The file system path of a track of the album. /// Cancellation token that can be used to stop the execution. /// The metadata of the album, or when the tags carry no album title. private Task ReadMetadataAsync(string path, 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(path); if (data is null || string.IsNullOrWhiteSpace(data.Album)) return null; return Id3TagMapper.MapAlbum(data); }, cancellationToken); } }