mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
Add burger menu for file renaming and relocation
This commit is contained in:
parent
f2d6d9bb78
commit
391f297dcd
@ -1 +1 @@
|
|||||||
Subproject commit bd4e6707503ab9c97d13ee0d8f8c69e9ff03cd12
|
Subproject commit b502a268d65b1ab787e71826d8502126db71e6d4
|
||||||
@ -104,3 +104,21 @@ bool RecentBooksStore::loadFromFile() {
|
|||||||
Serial.printf("[%lu] [RBS] Recent books loaded from file (%d entries)\n", millis(), recentBooks.size());
|
Serial.printf("[%lu] [RBS] Recent books loaded from file (%d entries)\n", millis(), recentBooks.size());
|
||||||
return true;
|
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,8 +32,17 @@ class RecentBooksStore {
|
|||||||
int getCount() const { return static_cast<int>(recentBooks.size()); }
|
int getCount() const { return static_cast<int>(recentBooks.size()); }
|
||||||
|
|
||||||
bool saveToFile() const;
|
bool saveToFile() const;
|
||||||
|
|
||||||
bool loadFromFile();
|
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
|
// Helper macro to access recent books store
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "CrossPointWebServer.h"
|
#include "CrossPointWebServer.h"
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "util/BookCacheManager.h"
|
||||||
#include <Epub.h>
|
#include <Epub.h>
|
||||||
#include <FsHelpers.h>
|
#include <FsHelpers.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
@ -112,6 +113,10 @@ void CrossPointWebServer::begin() {
|
|||||||
// Delete file/folder endpoint
|
// Delete file/folder endpoint
|
||||||
server->on("/delete", HTTP_POST, [this] { handleDelete(); });
|
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(); });
|
server->onNotFound([this] { handleNotFound(); });
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
@ -787,6 +792,91 @@ 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
|
// WebSocket callback trampoline
|
||||||
void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
||||||
if (wsInstance) {
|
if (wsInstance) {
|
||||||
|
|||||||
@ -78,4 +78,7 @@ class CrossPointWebServer {
|
|||||||
void handleUploadPost() const;
|
void handleUploadPost() const;
|
||||||
void handleCreateFolder() const;
|
void handleCreateFolder() const;
|
||||||
void handleDelete() 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 "StringUtils.h"
|
||||||
|
#include <HardwareSerial.h>
|
||||||
|
#include "../RecentBooksStore.h"
|
||||||
|
#include "../CrossPointState.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
|
||||||
|
}
|
||||||
30
src/util/BookCacheManager.h
Normal file
30
src/util/BookCacheManager.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#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