#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Plugins.Contracts.Core.Metadata;
using Lumina.Plugins.LocalMusicArtwork.Common.Models.DTO.Settings;
using Lumina.Plugins.LocalMusicArtwork.Core.Settings;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Plugins.LocalMusicArtwork.Core;
///
/// Provides the artwork of an album from the images stored in the folders of the local music library of the user.
/// When the album has no cover image on disk and the settings allow it, the cover embedded in the audio files is extracted as a fallback.
///
internal sealed class LocalMusicAlbumArtworkProvider : IArtworkProvider
{
private readonly LocalMusicArtworkSettingsProvider _settingsProvider;
///
/// Gets the display name of the artwork provider.
///
public string Name => "Local Music Artwork";
///
/// Gets the media library types this artwork provider supports.
///
public IReadOnlyList SupportedLibraryTypes => [LibraryType.Music];
///
/// Gets a value indicating whether this artwork provider requires access to the web to retrieve artwork.
///
public bool RequiresWebAccess => false;
///
/// Initializes a new instance of the class.
///
/// The provider of the runtime settings of the plugin.
public LocalMusicAlbumArtworkProvider(LocalMusicArtworkSettingsProvider settingsProvider)
{
_settingsProvider = settingsProvider;
}
///
/// Gets the artworks of the album described by .
///
/// The lookup describing the album to get the artwork for.
/// Cancellation token that can be used to stop the execution.
/// The artworks of the album, or an empty collection when no artwork was found.
public async Task> GetArtworkAsync(AlbumMetadataLookupDto lookup, CancellationToken cancellationToken)
{
IReadOnlyList artworks = LocalMusicArtworkScanner.ScanAlbumArtwork(lookup.Path);
// The cover stored on disk always wins; the embedded one only fills the gap when the album carries no cover on disk at all.
if (artworks.Any(artwork => artwork.Type == ArtworkType.Cover))
return artworks;
LocalMusicArtworkSettingsDto settings = await _settingsProvider.GetAsync(cancellationToken).ConfigureAwait(false);
if (!settings.ShouldExtractEmbeddedCover)
return artworks;
ArtworkDto? embeddedCover = LocalMusicArtworkEmbeddedCoverExtractor.Extract(lookup.Path);
if (embeddedCover is null)
return artworks;
return [.. artworks, embeddedCover];
}
}