From b54b12e518632d842261854c1374380fd4c066dc Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Sun, 21 Dec 2025 14:31:10 +1100 Subject: [PATCH] Remove book size cache --- lib/Epub/Epub.cpp | 127 ++++++++++++++++++---------------------------- lib/Epub/Epub.h | 1 + 2 files changed, 51 insertions(+), 77 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index 33ee6178..25bff2e4 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -5,7 +5,6 @@ #include #include -#include #include "Epub/FsHelpers.h" #include "Epub/parsers/ContainerParser.h" @@ -127,7 +126,6 @@ bool Epub::parseTocNcxFile() { // load in the meta data for the epub file bool Epub::load() { Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str()); - ZipFile zip("/sd" + filepath); std::string contentOpfFilePath; if (!findContentOpfFile(&contentOpfFilePath)) { @@ -155,85 +153,21 @@ bool Epub::load() { return true; } -std::string normalisePath(const std::string& path) { - std::vector components; - std::string component; - - for (const auto c : path) { - if (c == '/') { - if (!component.empty()) { - if (component == "..") { - if (!components.empty()) { - components.pop_back(); - } - } else { - components.push_back(component); - } - component.clear(); - } - } else { - component += c; - } - } - - if (!component.empty()) { - components.push_back(component); - } - - std::string result; - for (const auto& c : components) { - if (!result.empty()) { - result += "/"; - } - result += c; - } - - return result; -} void Epub::initializeSpineItemSizes() { - setupCacheDir(); + Serial.printf("[%lu] [EBP] Calculating book size\n", millis()); - size_t spineItemsCount = getSpineItemsCount(); + const size_t spineItemsCount = getSpineItemsCount(); size_t cumSpineItemSize = 0; - if (SD.exists((getCachePath() + "/spine_size.bin").c_str())) { - File f = SD.open((getCachePath() + "/spine_size.bin").c_str()); - uint8_t data[4]; - for (size_t i = 0; i < spineItemsCount; i++) { - f.read(data, 4); - cumSpineItemSize = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); - cumulativeSpineItemSize.emplace_back(cumSpineItemSize); - // Serial.printf("[%lu] [EBP] Loading item %d size %u to %u %u\n", millis(), - // i, cumSpineItemSize, data[1], data[0]); - } - f.close(); - } else { - File f = SD.open((getCachePath() + "/spine_size.bin").c_str(), FILE_WRITE); - uint8_t data[4]; - // determine size of spine items - const ZipFile zip("/sd" + filepath); + const ZipFile zip("/sd" + filepath); - for (size_t i = 0; i < spineItemsCount; i++) { - std::string spineItem = getSpineItem(i); - size_t s = 0; - unsigned long last = millis(); - const std::string path = normalisePath(spineItem); - zip.getInflatedFileSize(path.c_str(), &s); - // getItemSize(spineItem, &s); - Serial.printf("[%lu] [EBP] Determining size %d of item %d took %lu ms\n", millis(), cumSpineItemSize, i, - millis() - last); - cumSpineItemSize += s; - cumulativeSpineItemSize.emplace_back(cumSpineItemSize); - - // and persist to cache - data[0] = cumSpineItemSize & 0xFF; - data[1] = (cumSpineItemSize >> 8) & 0xFF; - data[2] = (cumSpineItemSize >> 16) & 0xFF; - data[3] = (cumSpineItemSize >> 24) & 0xFF; - f.write(data, 4); - } - - f.close(); + for (size_t i = 0; i < spineItemsCount; i++) { + std::string spineItem = getSpineItem(i); + size_t s = 0; + getItemSize(zip, spineItem, &s); + cumSpineItemSize += s; + cumulativeSpineItemSize.emplace_back(cumSpineItemSize); } + Serial.printf("[%lu] [EBP] Book size: %lu\n", millis(), cumSpineItemSize); } @@ -274,6 +208,42 @@ const std::string& Epub::getTitle() const { return title; } const std::string& Epub::getCoverImageItem() const { return coverImageItem; } +std::string normalisePath(const std::string& path) { + std::vector components; + std::string component; + + for (const auto c : path) { + if (c == '/') { + if (!component.empty()) { + if (component == "..") { + if (!components.empty()) { + components.pop_back(); + } + } else { + components.push_back(component); + } + component.clear(); + } + } else { + component += c; + } + } + + if (!component.empty()) { + components.push_back(component); + } + + std::string result; + for (const auto& c : components) { + if (!result.empty()) { + result += "/"; + } + result += c; + } + + return result; +} + uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, bool trailingNullByte) const { const ZipFile zip("/sd" + filepath); const std::string path = normalisePath(itemHref); @@ -296,8 +266,11 @@ bool Epub::readItemContentsToStream(const std::string& itemHref, Print& out, con bool Epub::getItemSize(const std::string& itemHref, size_t* size) const { const ZipFile zip("/sd" + filepath); - const std::string path = normalisePath(itemHref); + return getItemSize(zip, itemHref, size); +} +bool Epub::getItemSize(const ZipFile& zip, const std::string& itemHref, size_t* size) { + const std::string path = normalisePath(itemHref); return zip.getInflatedFileSize(path.c_str(), size); } diff --git a/lib/Epub/Epub.h b/lib/Epub/Epub.h index f22b630c..77c564d6 100644 --- a/lib/Epub/Epub.h +++ b/lib/Epub/Epub.h @@ -33,6 +33,7 @@ class Epub { bool parseContentOpf(const std::string& contentOpfFilePath); bool parseTocNcxFile(); void initializeSpineItemSizes(); + static bool getItemSize(const ZipFile &zip, const std::string &itemHref, size_t *size); public: explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {