diff --git a/src/network/CrossPointWebServer.cpp b/src/network/CrossPointWebServer.cpp index a135c9f0..70aaeec3 100644 --- a/src/network/CrossPointWebServer.cpp +++ b/src/network/CrossPointWebServer.cpp @@ -1,6 +1,7 @@ #include "CrossPointWebServer.h" #include +#include "util/BookCacheManager.h" #include #include #include @@ -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,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 void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { if (wsInstance) { diff --git a/src/network/CrossPointWebServer.h b/src/network/CrossPointWebServer.h index 36030292..a2a5279a 100644 --- a/src/network/CrossPointWebServer.h +++ b/src/network/CrossPointWebServer.h @@ -78,4 +78,7 @@ class CrossPointWebServer { void handleUploadPost() const; void handleCreateFolder() const; void handleDelete() const; + void handleMove() const; + void handleRename() const; + }; diff --git a/src/network/html/FilesPage.html b/src/network/html/FilesPage.html index bfdbe3cc..41715a3a 100644 --- a/src/network/html/FilesPage.html +++ b/src/network/html/FilesPage.html @@ -1,5 +1,6 @@ + @@ -7,21 +8,24 @@ + - - -