#region ========================================================================= USING =====================================================================================
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Nemera.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Nemera.Plugins.Contracts.Core.Reading;
using Nemera.Plugins.Epub.Core.Epub;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Nemera.Plugins.Epub.Core;
///
/// Book reader for the EPUB format.
///
///
/// See for what an EPUB is, and how it is decoded; this class only declares the supported format and delegates the decoding to the parser.
///
internal sealed class EpubReader : IBookReader
{
///
/// Gets the file extensions supported by the reader.
///
public IReadOnlyList SupportedExtensions => [".epub"];
///
/// Gets the media library types the reader supports.
///
public IReadOnlyList SupportedLibraryTypes => [LibraryType.EBook, LibraryType.Book];
///
/// Opens the EPUB stored at , extracting its sections and resources into
/// , and returns its normalized reading document.
///
/// The file system path of the EPUB.
/// The directory into which the sections and resources of the EPUB are extracted.
/// Whether the book, when it is a PDF, is rendered as page images instead of extracting its text layer. An EPUB always has a text layer, so this is ignored.
/// Cancellation token that can be used to stop the execution.
/// The normalized reading document of the EPUB.
public Task OpenAsync(string path, string workingDirectory, bool shouldRenderPdfAsImages, CancellationToken cancellationToken)
{
return Task.FromResult(EpubDocumentParser.Parse(path, workingDirectory, cancellationToken));
}
///
/// Produces the resource identified by of the EPUB stored at .
/// Every resource of an EPUB is extracted to the working directory when the book is opened, so this reads the
/// already extracted file. The resource key is the safe file name the parser derives from the manifest href, and the
/// file is contained-checked by the host before it is served, so the key cannot escape the resources directory.
///
/// The file system path of the EPUB.
/// The directory into which the sections and resources of the EPUB are extracted.
/// The opaque resource key of the resource to produce.
/// Cancellation token that can be used to stop the execution.
/// The binary data of the extracted resource.
public Task GetResourceAsync(string path, string workingDirectory, string resourceKey, CancellationToken cancellationToken)
{
byte[] data = File.ReadAllBytes(Path.Combine(workingDirectory, "resources", resourceKey));
return Task.FromResult(data);
}
}