#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.DTO.Common; using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary; using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; using Lumina.Plugins.Calibre.Common.Models.DTO.Opf; using Lumina.Plugins.Calibre.Core.Opf; using Lumina.Plugins.Contracts.Core.Metadata; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Plugins.Calibre.Core; /// /// Provides the cover of a book from the cover reference of the Calibre OPF file located next to the book file. /// internal sealed class CalibreArtworkProvider : IArtworkProvider { /// /// Gets the display name of the artwork provider. /// public string Name => "Calibre"; /// /// Gets the media library types this artwork provider supports. /// public IReadOnlyList SupportedLibraryTypes => [LibraryType.Book, LibraryType.EBook]; /// /// Gets a value indicating whether this artwork provider requires access to the web to retrieve artwork. /// public bool RequiresWebAccess => false; /// /// Gets the cover of the book described by . /// /// The lookup describing the book to get the cover for. /// Cancellation token that can be used to stop the execution. /// The cover of the book, or an empty collection when no cover was found. public Task> GetArtworkAsync(BookMetadataLookupDto lookup, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(lookup.Path)) return Task.FromResult>([]); string bookDirectory = Path.GetDirectoryName(lookup.Path) ?? string.Empty; string opfFilePath = Path.Combine(bookDirectory, "metadata.opf"); if (!File.Exists(opfFilePath)) return Task.FromResult>([]); OpfDocumentDto document = OpfReader.Read(opfFilePath); if (string.IsNullOrWhiteSpace(document.CoverHref)) return Task.FromResult>([]); // the cover reference must be a bare file name contained in the directory of the book, so that no file outside of it can be accessed string coverFileName = Path.GetFileName(document.CoverHref); if (coverFileName != document.CoverHref || Path.IsPathRooted(document.CoverHref)) return Task.FromResult>([]); string coverFilePath = Path.Combine(bookDirectory, coverFileName); if (!File.Exists(coverFilePath)) return Task.FromResult>([]); return Task.FromResult>([new ArtworkDto(ArtworkType.Cover, Ordinal: 0, LocalPath: coverFilePath, RemoteUrl: null)]); } }