#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 track metadata from the embedded tags of the audio file, by resolving lookups into audio metadata DTOs. /// internal sealed class Id3TrackMetadataProvider : 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 Id3TrackMetadataProvider(Id3TagReader tagReader) { _tagReader = tagReader; } /// /// Searches for the metadata of the track described by . /// /// The lookup describing the track to search for. /// Cancellation token that can be used to stop the execution. /// The collection of track metadata candidates found, which for embedded tags is at most the file of the lookup. public async Task> GetSearchResultsAsync(TrackMetadataLookupDto lookup, CancellationToken cancellationToken) { AudioMetadataDto? metadata = await ReadMetadataAsync(lookup.Path, cancellationToken).ConfigureAwait(false); IReadOnlyList results = metadata is null ? [] : [metadata]; return results; } /// /// Gets the metadata of the track described by . /// /// The lookup describing the track to get the metadata for. /// Cancellation token that can be used to stop the execution. /// The metadata of the track, or when the file carries none. public Task GetMetadataAsync(TrackMetadataLookupDto lookup, CancellationToken cancellationToken) { return ReadMetadataAsync(lookup.Path, cancellationToken); } /// /// Reads the metadata of the track stored at from its embedded tags. /// /// The file system path of the track. /// Cancellation token that can be used to stop the execution. /// The metadata of the track, or when the tags carry no 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.Title)) return null; return Id3TagMapper.MapTrack(data); }, cancellationToken); } }