using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VersOne.Epub.Environment;
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
namespace VersOne.Epub
{
///
/// Represents a EPUB book with its metadata. An instance of this class holds a reference to the EPUB file to load book's content on demand.
///
public class EpubBookRef : IDisposable
{
private bool isDisposed;
///
/// Initializes a new instance of the class.
///
/// Reference to the EPUB file.
/// The path to the EPUB file or null if the EPUB file is being loaded from a stream.
/// The title of the book.
/// The comma separated list of the book's authors.
/// The list of book's authors names.
/// The book's description or null if the description is not present in the book.
/// The parsed EPUB schema of the book.
/// The collection of references to the book's content files within the EPUB archive.
/// Various options to configure the behavior of the EPUB reader.
/// The parameter is null.
/// The parameter is null.
/// The parameter is null.
/// The parameter is null.
/// The parameter is null.
public EpubBookRef(
IZipFile epubFile, string? filePath, string title, string author, List? authorList, string? description, EpubSchema schema,
EpubContentRef content, EpubReaderOptions? epubReaderOptions = null)
{
EpubFile = epubFile ?? throw new ArgumentNullException(nameof(epubFile));
FilePath = filePath;
Title = title ?? throw new ArgumentNullException(nameof(title));
Author = author ?? throw new ArgumentNullException(nameof(author));
AuthorList = authorList ?? new List();
Description = description;
Schema = schema ?? throw new ArgumentNullException(nameof(schema));
Content = content ?? throw new ArgumentNullException(nameof(content));
EpubReaderOptions = epubReaderOptions ?? new EpubReaderOptions();
isDisposed = false;
}
///
/// Finalizes an instance of the class.
///
~EpubBookRef()
{
Dispose(false);
}
///
/// Gets the path to the EPUB file or null if the EPUB file was loaded from a stream.
///
public string? FilePath { get; }
///
/// Gets the title of the book.
///
public string Title { get; }
///
/// Gets the comma separated list of the book's authors.
///
public string Author { get; }
///
/// Gets the list of book's authors names.
///
public List AuthorList { get; }
///
/// Gets the book's description or null if the description is not present in the book.
///
public string? Description { get; }
///
/// Gets the parsed EPUB schema of the book.
///
public EpubSchema Schema { get; }
///
/// Gets the collection of references to the book's content files within the EPUB archive.
///
public EpubContentRef Content { get; }
///
/// Gets the reference to the EPUB file.
///
public IZipFile EpubFile { get; }
///
/// Gets the options that configure the behavior of the EPUB reader.
///
public EpubReaderOptions EpubReaderOptions { get; }
///
/// Loads the book's cover image from the EPUB file.
///
/// Book's cover image or null if there is no cover.
public byte[]? ReadCover()
{
return ReadCoverAsync().Result;
}
///
/// Asynchronously loads the book's cover image from the EPUB file.
///
///
/// A task that represents the asynchronous load operation. The value of the TResult parameter contains the book's cover image or null if there is no cover.
///
public async Task ReadCoverAsync()
{
if (Content.Cover == null)
{
return null;
}
return await Content.Cover.ReadContentAsBytesAsync().ConfigureAwait(false);
}
///
/// Processes the reading order of the EPUB book and returns a list of text content file references in the order of reading intended by the author.
///
/// A list of text content file references in the order of reading.
public List GetReadingOrder()
{
return GetReadingOrderAsync().Result;
}
///
/// Asynchronously processes the reading order of the EPUB book and returns a list of text content file references in the order of reading intended by the author.
///
///
/// A task that represents the asynchronous processing operation.
/// The value of the TResult parameter contains a list of text content file references in the order of reading.
///
public async Task> GetReadingOrderAsync()
{
return await Task.Run(() => SpineReader.GetReadingOrder(Schema, Content, EpubReaderOptions.SpineReaderOptions)).ConfigureAwait(false);
}
///
/// Processes the navigational information of the EPUB book and returns a list of its navigation elements (typically the table of contents).
///
/// A list of navigation elements of the book or null if the book doesn't have navigation information.
public List? GetNavigation()
{
return GetNavigationAsync().Result;
}
///
/// Asynchronously processes the navigational information of the EPUB book and returns a list of its navigation elements (typically the table of contents).
///
///
/// A task that represents the asynchronous processing operation.
/// The value of the TResult parameter contains a list of navigation elements of the book or null if the book doesn't have navigation information.
///
public async Task?> GetNavigationAsync()
{
return await Task.Run(() => NavigationReader.GetNavigationItems(Schema, Content)).ConfigureAwait(false);
}
///
/// Releases the managed resources used by the .
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases the resources used by the .
///
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
/// This class has no unmanaged resources, so the value of false causes this method to not release any resources.
///
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
EpubFile.Dispose();
}
isDisposed = true;
}
}
}
}