mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
Merge 8d791b6e07 into da4d3b5ea5
This commit is contained in:
commit
6fd5d7b398
@ -1 +1 @@
|
||||
Subproject commit bd4e6707503ab9c97d13ee0d8f8c69e9ff03cd12
|
||||
Subproject commit c39f253a7dabbc193a8d7d310fb8777dca0ab8f1
|
||||
@ -104,3 +104,21 @@ bool RecentBooksStore::loadFromFile() {
|
||||
Serial.printf("[%lu] [RBS] Recent books loaded from file (%d entries)\n", millis(), recentBooks.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
void RecentBooksStore::updatePath(const std::string& oldPath, const std::string& newPath) {
|
||||
bool changed = false;
|
||||
for (auto& book : recentBooks) {
|
||||
if (book.path == oldPath) {
|
||||
book.path = newPath;
|
||||
changed = true;
|
||||
} else if (book.path.find(oldPath + "/") == 0) {
|
||||
// It's a directory move/rename
|
||||
book.path = newPath + book.path.substr(oldPath.length());
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
saveToFile();
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,9 +32,18 @@ class RecentBooksStore {
|
||||
int getCount() const { return static_cast<int>(recentBooks.size()); }
|
||||
|
||||
bool saveToFile() const;
|
||||
|
||||
bool loadFromFile();
|
||||
|
||||
/**
|
||||
* Update the path of a book in the recent list.
|
||||
* Useful when moving/renaming files or entire directories.
|
||||
* If oldPath is a directory, all books within will have their paths updated.
|
||||
*
|
||||
* @param oldPath Original absolute path
|
||||
* @param newPath New absolute path
|
||||
*/
|
||||
void updatePath(const std::string& oldPath, const std::string& newPath);
|
||||
};
|
||||
|
||||
// Helper macro to access recent books store
|
||||
#define RECENT_BOOKS RecentBooksStore::getInstance()
|
||||
#define RECENT_BOOKS RecentBooksStore::getInstance()
|
||||
@ -11,6 +11,7 @@
|
||||
|
||||
#include "html/FilesPageHtml.generated.h"
|
||||
#include "html/HomePageHtml.generated.h"
|
||||
#include "util/BookCacheManager.h"
|
||||
#include "util/StringUtils.h"
|
||||
|
||||
namespace {
|
||||
@ -112,6 +113,10 @@ void CrossPointWebServer::begin() {
|
||||
// Delete file/folder endpoint
|
||||
server->on("/delete", HTTP_POST, [this] { handleDelete(); });
|
||||
|
||||
// Move and Rename endpoints (stubs)
|
||||
server->on("/move", HTTP_POST, [this] { handleMove(); });
|
||||
server->on("/rename", HTTP_POST, [this] { handleRename(); });
|
||||
|
||||
server->onNotFound([this] { handleNotFound(); });
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
@ -787,6 +792,89 @@ void CrossPointWebServer::handleDelete() const {
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleMove() const {
|
||||
if (!server->hasArg("oldPath") || !server->hasArg("newPath")) {
|
||||
server->send(400, "text/plain", "Missing oldPath or newPath");
|
||||
return;
|
||||
}
|
||||
|
||||
String oldPath = server->arg("oldPath");
|
||||
String newPath = server->arg("newPath");
|
||||
|
||||
if (oldPath.isEmpty() || newPath.isEmpty() || oldPath == "/" || newPath == "/") {
|
||||
server->send(400, "text/plain", "Invalid paths");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!oldPath.startsWith("/")) oldPath = "/" + oldPath;
|
||||
if (!newPath.startsWith("/")) newPath = "/" + newPath;
|
||||
|
||||
if (!SdMan.exists(oldPath.c_str())) {
|
||||
server->send(404, "text/plain", "Source not found");
|
||||
return;
|
||||
}
|
||||
|
||||
if (SdMan.exists(newPath.c_str())) {
|
||||
server->send(400, "text/plain", "Destination already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
// Migrate cache first (or parts of it if it's a directory)
|
||||
BookCacheManager::migrateCache(oldPath, newPath);
|
||||
|
||||
if (SdMan.rename(oldPath.c_str(), newPath.c_str())) {
|
||||
Serial.printf("[%lu] [WEB] Moved %s to %s\n", millis(), oldPath.c_str(), newPath.c_str());
|
||||
server->send(200, "text/plain", "Moved successfully");
|
||||
} else {
|
||||
server->send(500, "text/plain", "Move failed");
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleRename() const {
|
||||
if (!server->hasArg("oldPath") || !server->hasArg("newPath")) {
|
||||
server->send(400, "text/plain", "Missing oldPath or newPath");
|
||||
return;
|
||||
}
|
||||
|
||||
String oldPath = server->arg("oldPath");
|
||||
String newPath = server->arg("newPath");
|
||||
|
||||
if (oldPath.isEmpty() || newPath.isEmpty() || oldPath == "/" || newPath == "/") {
|
||||
server->send(400, "text/plain", "Invalid paths");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!oldPath.startsWith("/")) oldPath = "/" + oldPath;
|
||||
if (!newPath.startsWith("/")) newPath = "/" + newPath;
|
||||
|
||||
// Security check: prevent renaming system files
|
||||
if (oldPath.substring(oldPath.lastIndexOf('/') + 1).startsWith(".") ||
|
||||
newPath.substring(newPath.lastIndexOf('/') + 1).startsWith(".")) {
|
||||
server->send(403, "text/plain", "Cannot rename system files");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!SdMan.exists(oldPath.c_str())) {
|
||||
server->send(404, "text/plain", "Source not found");
|
||||
return;
|
||||
}
|
||||
|
||||
if (SdMan.exists(newPath.c_str())) {
|
||||
server->send(400, "text/plain", "Destination already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
// Migrate cache
|
||||
BookCacheManager::migrateCache(oldPath, newPath);
|
||||
|
||||
if (SdMan.rename(oldPath.c_str(), newPath.c_str())) {
|
||||
Serial.printf("[%lu] [WEB] Renamed %s to %s\n", millis(), oldPath.c_str(), newPath.c_str());
|
||||
server->send(200, "text/plain", "Renamed successfully");
|
||||
} else {
|
||||
server->send(500, "text/plain", "Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
// WebSocket callback trampoline
|
||||
void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
||||
if (wsInstance) {
|
||||
|
||||
@ -78,4 +78,6 @@ class CrossPointWebServer {
|
||||
void handleUploadPost() const;
|
||||
void handleCreateFolder() const;
|
||||
void handleDelete() const;
|
||||
void handleMove() const;
|
||||
void handleRename() const;
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
103
src/util/BookCacheManager.cpp
Normal file
103
src/util/BookCacheManager.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include "BookCacheManager.h"
|
||||
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
#include "../CrossPointState.h"
|
||||
#include "../RecentBooksStore.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
bool BookCacheManager::migrateCache(const String& oldPath, const String& newPath) {
|
||||
if (oldPath == newPath) return true;
|
||||
|
||||
// Update Recent Books list
|
||||
RECENT_BOOKS.updatePath(oldPath.c_str(), newPath.c_str());
|
||||
|
||||
// Update last opened book state if matches
|
||||
if (CrossPointState::getInstance().openEpubPath == oldPath.c_str()) {
|
||||
CrossPointState::getInstance().openEpubPath = newPath.c_str();
|
||||
CrossPointState::getInstance().saveToFile();
|
||||
}
|
||||
|
||||
if (!SdMan.exists(oldPath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FsFile item = SdMan.open(oldPath.c_str());
|
||||
if (!item) return false;
|
||||
|
||||
bool isDir = item.isDirectory();
|
||||
item.close();
|
||||
|
||||
if (isDir) {
|
||||
// Recursively migrate contents of the directory
|
||||
FsFile dir = SdMan.open(oldPath.c_str());
|
||||
FsFile entry = dir.openNextFile();
|
||||
char nameBuf[512];
|
||||
bool success = true;
|
||||
|
||||
while (entry) {
|
||||
entry.getName(nameBuf, sizeof(nameBuf));
|
||||
String fileName = String(nameBuf);
|
||||
entry.close();
|
||||
|
||||
String subOldPath = oldPath + "/" + fileName;
|
||||
String subNewPath = newPath + "/" + fileName;
|
||||
|
||||
if (!migrateCache(subOldPath, subNewPath)) {
|
||||
success = false;
|
||||
}
|
||||
entry = dir.openNextFile();
|
||||
}
|
||||
dir.close();
|
||||
return success;
|
||||
}
|
||||
|
||||
// It's a file. check if it's a supported book type
|
||||
if (!isSupportedFile(oldPath)) {
|
||||
return true; // Not a book, nothing to migrate
|
||||
}
|
||||
|
||||
String oldCache = getCachePath(oldPath);
|
||||
String newCache = getCachePath(newPath);
|
||||
|
||||
if (oldCache.isEmpty() || newCache.isEmpty() || oldCache == newCache) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (SdMan.exists(oldCache.c_str())) {
|
||||
if (SdMan.exists(newCache.c_str())) {
|
||||
Serial.printf("[%lu] [BCM] New cache already exists for %s, removing old cache\n", millis(), newPath.c_str());
|
||||
SdMan.removeDir(oldCache.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [BCM] Migrating cache: %s -> %s\n", millis(), oldCache.c_str(), newCache.c_str());
|
||||
if (SdMan.rename(oldCache.c_str(), newCache.c_str())) {
|
||||
return true;
|
||||
} else {
|
||||
Serial.printf("[%lu] [BCM] Failed to rename cache directory\n", millis());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true; // No old cache to migrate
|
||||
}
|
||||
|
||||
String BookCacheManager::getCachePath(const String& path) {
|
||||
if (!isSupportedFile(path)) return "";
|
||||
|
||||
auto hash = std::hash<std::string>{}(path.c_str());
|
||||
return String("/.crosspoint/") + getCachePrefix(path) + "_" + String(hash);
|
||||
}
|
||||
|
||||
bool BookCacheManager::isSupportedFile(const String& path) {
|
||||
return StringUtils::checkFileExtension(path, ".epub") || StringUtils::checkFileExtension(path, ".txt") ||
|
||||
StringUtils::checkFileExtension(path, ".xtc") || StringUtils::checkFileExtension(path, ".xtg") ||
|
||||
StringUtils::checkFileExtension(path, ".xth");
|
||||
}
|
||||
|
||||
String BookCacheManager::getCachePrefix(const String& path) {
|
||||
if (StringUtils::checkFileExtension(path, ".epub")) return "epub";
|
||||
if (StringUtils::checkFileExtension(path, ".txt")) return "txt";
|
||||
return "xtc"; // .xtc, .xtg, .xth
|
||||
}
|
||||
31
src/util/BookCacheManager.h
Normal file
31
src/util/BookCacheManager.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDCardManager.h>
|
||||
#include <WString.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
class BookCacheManager {
|
||||
public:
|
||||
/**
|
||||
* Migrate cache data for a file or directory.
|
||||
* If path is a directory, it recursively migrates all files within.
|
||||
*
|
||||
* @param oldPath Original absolute path
|
||||
* @param newPath New absolute path
|
||||
* @return true if migration was successful or no cache existed
|
||||
*/
|
||||
static bool migrateCache(const String& oldPath, const String& newPath);
|
||||
|
||||
/**
|
||||
* Get the cache directory path for a given book file.
|
||||
*
|
||||
* @param path Absolute path to the book file
|
||||
* @return Full path to the cache directory, or empty string if not a supported book type
|
||||
*/
|
||||
static String getCachePath(const String& path);
|
||||
|
||||
private:
|
||||
static bool isSupportedFile(const String& path);
|
||||
static String getCachePrefix(const String& path);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user