File size: 8,787 Bytes
fab29d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 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
{
/// <summary>
/// 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.
/// </summary>
public class EpubBookRef : IDisposable
{
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="EpubBookRef" /> class.
/// </summary>
/// <param name="epubFile">Reference to the EPUB file.</param>
/// <param name="filePath">The path to the EPUB file or <c>null</c> if the EPUB file is being loaded from a stream.</param>
/// <param name="title">The title of the book.</param>
/// <param name="author">The comma separated list of the book's authors.</param>
/// <param name="authorList">The list of book's authors names.</param>
/// <param name="description">The book's description or <c>null</c> if the description is not present in the book.</param>
/// <param name="schema">The parsed EPUB schema of the book.</param>
/// <param name="content">The collection of references to the book's content files within the EPUB archive.</param>
/// <param name="epubReaderOptions">Various options to configure the behavior of the EPUB reader.</param>
/// <exception cref="ArgumentNullException">The <paramref name="epubFile" /> parameter is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="title" /> parameter is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="author" /> parameter is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="schema" /> parameter is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="content" /> parameter is <c>null</c>.</exception>
public EpubBookRef(
IZipFile epubFile, string? filePath, string title, string author, List<string>? 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<string>();
Description = description;
Schema = schema ?? throw new ArgumentNullException(nameof(schema));
Content = content ?? throw new ArgumentNullException(nameof(content));
EpubReaderOptions = epubReaderOptions ?? new EpubReaderOptions();
isDisposed = false;
}
/// <summary>
/// Finalizes an instance of the <see cref="EpubBookRef" /> class.
/// </summary>
~EpubBookRef()
{
Dispose(false);
}
/// <summary>
/// Gets the path to the EPUB file or <c>null</c> if the EPUB file was loaded from a stream.
/// </summary>
public string? FilePath { get; }
/// <summary>
/// Gets the title of the book.
/// </summary>
public string Title { get; }
/// <summary>
/// Gets the comma separated list of the book's authors.
/// </summary>
public string Author { get; }
/// <summary>
/// Gets the list of book's authors names.
/// </summary>
public List<string> AuthorList { get; }
/// <summary>
/// Gets the book's description or <c>null</c> if the description is not present in the book.
/// </summary>
public string? Description { get; }
/// <summary>
/// Gets the parsed EPUB schema of the book.
/// </summary>
public EpubSchema Schema { get; }
/// <summary>
/// Gets the collection of references to the book's content files within the EPUB archive.
/// </summary>
public EpubContentRef Content { get; }
/// <summary>
/// Gets the reference to the EPUB file.
/// </summary>
public IZipFile EpubFile { get; }
/// <summary>
/// Gets the options that configure the behavior of the EPUB reader.
/// </summary>
public EpubReaderOptions EpubReaderOptions { get; }
/// <summary>
/// Loads the book's cover image from the EPUB file.
/// </summary>
/// <returns>Book's cover image or <c>null</c> if there is no cover.</returns>
public byte[]? ReadCover()
{
return ReadCoverAsync().Result;
}
/// <summary>
/// Asynchronously loads the book's cover image from the EPUB file.
/// </summary>
/// <returns>
/// A task that represents the asynchronous load operation. The value of the TResult parameter contains the book's cover image or <c>null</c> if there is no cover.
/// </returns>
public async Task<byte[]?> ReadCoverAsync()
{
if (Content.Cover == null)
{
return null;
}
return await Content.Cover.ReadContentAsBytesAsync().ConfigureAwait(false);
}
/// <summary>
/// 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.
/// </summary>
/// <returns>A list of text content file references in the order of reading.</returns>
public List<EpubLocalTextContentFileRef> GetReadingOrder()
{
return GetReadingOrderAsync().Result;
}
/// <summary>
/// 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.
/// </summary>
/// <returns>
/// 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.
/// </returns>
public async Task<List<EpubLocalTextContentFileRef>> GetReadingOrderAsync()
{
return await Task.Run(() => SpineReader.GetReadingOrder(Schema, Content, EpubReaderOptions.SpineReaderOptions)).ConfigureAwait(false);
}
/// <summary>
/// Processes the navigational information of the EPUB book and returns a list of its navigation elements (typically the table of contents).
/// </summary>
/// <returns>A list of navigation elements of the book or <c>null</c> if the book doesn't have navigation information.</returns>
public List<EpubNavigationItemRef>? GetNavigation()
{
return GetNavigationAsync().Result;
}
/// <summary>
/// Asynchronously processes the navigational information of the EPUB book and returns a list of its navigation elements (typically the table of contents).
/// </summary>
/// <returns>
/// A task that represents the asynchronous processing operation.
/// The value of the TResult parameter contains a list of navigation elements of the book or <c>null</c> if the book doesn't have navigation information.
/// </returns>
public async Task<List<EpubNavigationItemRef>?> GetNavigationAsync()
{
return await Task.Run(() => NavigationReader.GetNavigationItems(Schema, Content)).ConfigureAwait(false);
}
/// <summary>
/// Releases the managed resources used by the <see cref="EpubBookRef" />.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases the resources used by the <see cref="EpubBookRef" />.
/// </summary>
/// <param name="disposing">
/// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.
/// This class has no unmanaged resources, so the value of <c>false</c> causes this method to not release any resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
EpubFile.Dispose();
}
isDisposed = true;
}
}
}
}
|