readBook function
Reads an EPUB book from the provided path.
The function first checks if the file at the path is valid, then reads its bytes and decodes them into an archive.
It then retrieves the root file path and root file from the archive, and parses the root file into a package.
It also retrieves the navigation of the EPUB from the package and archive, and extracts the files from the archive and package.
Finally, it retrieves the cover of the book from the package's manifest
items and the extracted images, and creates an EpubBook
from the
retrieved data.
path
is the path to the EPUB file to read.
Returns a Future
that completes with the EpubBook
representing the
read book.
Throws an Exception
if the file at the path is not valid, the root file
path could not be found, the root file could not be found, or the TOC ID is
empty when getting the navigation.
Implementation
Future<EpubBook> readBook(final String path) async {
final file = _getFileIfValid(path);
final bytes = file.readAsBytesSync();
final archive = ZipDecoder().decodeBytes(bytes);
final rootFilePath = await getEpubRootFilePath(archive);
final rootFile = _getRootFile(archive, rootFilePath);
final package = parsePackage(
convert.utf8.decode(rootFile.content as List<int>),
);
final navigation = getEpubNavigation(package, archive, rootFilePath);
final files = extractFiles(archive.files, package.manifest.items);
final cover = getBookCover(package.manifest.items, files.images);
return EpubBook(
navigation: navigation,
files: files,
cover: cover,
package: package,
);
}