refactor: unify book cache clearing for epub, txt, and xtc files (#1875)

This commit is contained in:
WuTofu
2026-05-21 14:44:10 +03:00
committed by GitHub
parent dc404b41c7
commit 2dd491b62e
9 changed files with 77 additions and 32 deletions
+7 -16
View File
@@ -1,7 +1,6 @@
#include "CrossPointWebServer.h"
#include <ArduinoJson.h>
#include <Epub.h>
#include <FsHelpers.h>
#include <HalGPIO.h>
#include <HalStorage.h>
@@ -23,6 +22,7 @@
#include "html/HomePageHtml.generated.h"
#include "html/SettingsPageHtml.generated.h"
#include "html/js/jszip_minJs.generated.h"
#include "util/BookCacheUtils.h"
namespace {
// Folders/files to hide from the web interface file browser
@@ -48,15 +48,6 @@ String wsLastCompleteName;
size_t wsLastCompleteSize = 0;
unsigned long wsLastCompleteAt = 0;
// Helper function to clear epub cache after upload
void clearEpubCacheIfNeeded(const String& filePath) {
// Only clear cache for .epub files
if (FsHelpers::hasEpubExtension(filePath)) {
Epub(filePath.c_str(), "/.crosspoint").clearCache();
LOG_DBG("WEB", "Cleared epub cache for: %s", filePath.c_str());
}
}
String normalizeWebPath(const String& inputPath) {
if (inputPath.isEmpty() || inputPath == "/") {
return "/";
@@ -732,7 +723,7 @@ void CrossPointWebServer::handleUpload(UploadState& state) const {
String filePath = state.path;
if (!filePath.endsWith("/")) filePath += "/";
filePath += state.fileName;
clearEpubCacheIfNeeded(filePath);
clearBookCache(filePath.c_str());
}
}
} else if (upload.status == UPLOAD_FILE_ABORTED) {
@@ -878,7 +869,7 @@ void CrossPointWebServer::handleRename() const {
return;
}
clearEpubCacheIfNeeded(itemPath);
clearBookCache(itemPath.c_str());
const bool success = file.rename(newPath.c_str());
file.close();
@@ -971,7 +962,7 @@ void CrossPointWebServer::handleMove() const {
return;
}
clearEpubCacheIfNeeded(itemPath);
clearBookCache(itemPath.c_str());
const bool success = file.rename(newPath.c_str());
file.close();
@@ -1090,7 +1081,7 @@ void CrossPointWebServer::handleDelete() const {
// It's a file (or couldn't open as dir) — remove file
if (f) f.close();
success = Storage.remove(itemPath.c_str());
clearEpubCacheIfNeeded(itemPath);
clearBookCache(itemPath.c_str());
}
if (!success) {
@@ -1636,7 +1627,7 @@ void CrossPointWebServer::onWebSocketEvent(uint8_t num, WStype_t type, uint8_t*
wsLastCompleteSize = 0;
wsLastCompleteAt = millis();
LOG_DBG("WS", "Zero-byte upload complete: %s", filePath.c_str());
clearEpubCacheIfNeeded(filePath);
clearBookCache(filePath.c_str());
wsServer->sendTXT(num, "DONE");
wsLastProgressSent = 0;
break;
@@ -1705,7 +1696,7 @@ void CrossPointWebServer::onWebSocketEvent(uint8_t num, WStype_t type, uint8_t*
String filePath = wsUploadPath;
if (!filePath.endsWith("/")) filePath += "/";
filePath += wsUploadFileName;
clearEpubCacheIfNeeded(filePath);
clearBookCache(filePath.c_str());
wsServer->sendTXT(num, "DONE");
wsLastProgressSent = 0;
+5 -11
View File
@@ -1,11 +1,12 @@
#include "WebDAVHandler.h"
#include <Epub.h>
#include <FsHelpers.h>
#include <HalStorage.h>
#include <Logging.h>
#include <esp_task_wdt.h>
#include "util/BookCacheUtils.h"
namespace {
constexpr const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
@@ -384,7 +385,7 @@ void WebDAVHandler::handlePut(WebServer& s) {
return;
}
clearEpubCacheIfNeeded(path);
clearBookCache(path.c_str());
s.send(_putExisted ? 204 : 201);
LOG_DBG("DAV", "PUT complete: %s", path.c_str());
}
@@ -433,7 +434,7 @@ void WebDAVHandler::handleDelete(WebServer& s) {
}
} else {
file.close();
clearEpubCacheIfNeeded(path);
clearBookCache(path.c_str());
if (Storage.remove(path.c_str())) {
s.send(204);
} else {
@@ -542,7 +543,7 @@ void WebDAVHandler::handleMove(WebServer& s) {
return;
}
clearEpubCacheIfNeeded(srcPath);
clearBookCache(srcPath.c_str());
bool success = file.rename(dstPath.c_str());
file.close();
@@ -797,13 +798,6 @@ bool WebDAVHandler::getOverwrite(WebServer& s) const {
return true; // Default is T
}
void WebDAVHandler::clearEpubCacheIfNeeded(const String& path) const {
if (FsHelpers::hasEpubExtension(path)) {
Epub(path.c_str(), "/.crosspoint").clearCache();
LOG_DBG("DAV", "Cleared epub cache for: %s", path.c_str());
}
}
String WebDAVHandler::getMimeType(const String& path) const {
if (FsHelpers::hasEpubExtension(path)) return "application/epub+zip";
if (FsHelpers::checkFileExtension(path, ".pdf")) return "application/pdf";
-1
View File
@@ -38,7 +38,6 @@ class WebDAVHandler : public RequestHandler {
bool isProtectedPath(const String& path) const;
int getDepth(WebServer& s) const;
bool getOverwrite(WebServer& s) const;
void clearEpubCacheIfNeeded(const String& path) const;
void sendPropEntry(WebServer& s, const String& href, bool isDir, size_t size, const String& lastModified) const;
String getMimeType(const String& path) const;
};