Remove book size cache
This commit is contained in:
parent
fb355147a2
commit
b54b12e518
@ -5,7 +5,6 @@
|
|||||||
#include <ZipFile.h>
|
#include <ZipFile.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <ratio>
|
|
||||||
|
|
||||||
#include "Epub/FsHelpers.h"
|
#include "Epub/FsHelpers.h"
|
||||||
#include "Epub/parsers/ContainerParser.h"
|
#include "Epub/parsers/ContainerParser.h"
|
||||||
@ -127,7 +126,6 @@ bool Epub::parseTocNcxFile() {
|
|||||||
// load in the meta data for the epub file
|
// load in the meta data for the epub file
|
||||||
bool Epub::load() {
|
bool Epub::load() {
|
||||||
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
|
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
|
||||||
ZipFile zip("/sd" + filepath);
|
|
||||||
|
|
||||||
std::string contentOpfFilePath;
|
std::string contentOpfFilePath;
|
||||||
if (!findContentOpfFile(&contentOpfFilePath)) {
|
if (!findContentOpfFile(&contentOpfFilePath)) {
|
||||||
@ -155,85 +153,21 @@ bool Epub::load() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string normalisePath(const std::string& path) {
|
|
||||||
std::vector<std::string> 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() {
|
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;
|
size_t cumSpineItemSize = 0;
|
||||||
if (SD.exists((getCachePath() + "/spine_size.bin").c_str())) {
|
const ZipFile zip("/sd" + filepath);
|
||||||
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);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < spineItemsCount; i++) {
|
for (size_t i = 0; i < spineItemsCount; i++) {
|
||||||
std::string spineItem = getSpineItem(i);
|
std::string spineItem = getSpineItem(i);
|
||||||
size_t s = 0;
|
size_t s = 0;
|
||||||
unsigned long last = millis();
|
getItemSize(zip, spineItem, &s);
|
||||||
const std::string path = normalisePath(spineItem);
|
cumSpineItemSize += s;
|
||||||
zip.getInflatedFileSize(path.c_str(), &s);
|
cumulativeSpineItemSize.emplace_back(cumSpineItemSize);
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.printf("[%lu] [EBP] Book size: %lu\n", millis(), 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; }
|
const std::string& Epub::getCoverImageItem() const { return coverImageItem; }
|
||||||
|
|
||||||
|
std::string normalisePath(const std::string& path) {
|
||||||
|
std::vector<std::string> 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 {
|
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, bool trailingNullByte) const {
|
||||||
const ZipFile zip("/sd" + filepath);
|
const ZipFile zip("/sd" + filepath);
|
||||||
const std::string path = normalisePath(itemHref);
|
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 {
|
bool Epub::getItemSize(const std::string& itemHref, size_t* size) const {
|
||||||
const ZipFile zip("/sd" + filepath);
|
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);
|
return zip.getInflatedFileSize(path.c_str(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,7 @@ class Epub {
|
|||||||
bool parseContentOpf(const std::string& contentOpfFilePath);
|
bool parseContentOpf(const std::string& contentOpfFilePath);
|
||||||
bool parseTocNcxFile();
|
bool parseTocNcxFile();
|
||||||
void initializeSpineItemSizes();
|
void initializeSpineItemSizes();
|
||||||
|
static bool getItemSize(const ZipFile &zip, const std::string &itemHref, size_t *size);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user