This commit is contained in:
Zando 2026-02-01 19:23:26 +11:00 committed by GitHub
commit 116338b1aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1256 additions and 587 deletions

@ -1 +1 @@
Subproject commit bd4e6707503ab9c97d13ee0d8f8c69e9ff03cd12
Subproject commit c39f253a7dabbc193a8d7d310fb8777dca0ab8f1

View File

@ -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();
}
}

View File

@ -32,8 +32,17 @@ 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

View File

@ -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) {

View File

@ -78,4 +78,6 @@ class CrossPointWebServer {
void handleUploadPost() const;
void handleCreateFolder() const;
void handleDelete() const;
void handleMove() const;
void handleRename() const;
};

View File

@ -1,5 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@ -14,14 +15,17 @@
background-color: #f5f5f5;
color: #333;
}
h1 {
color: #2c3e50;
margin-bottom: 5px;
}
h2 {
color: #34495e;
margin-top: 0;
}
.card {
background: white;
border-radius: 8px;
@ -29,6 +33,7 @@
margin: 15px 0;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.page-header {
display: flex;
justify-content: space-between;
@ -39,34 +44,42 @@
padding-bottom: 15px;
border-bottom: 2px solid #3498db;
}
.page-header-left {
display: flex;
align-items: baseline;
gap: 12px;
flex-wrap: wrap;
}
.breadcrumb-inline {
color: #7f8c8d;
font-size: 1.1em;
}
.breadcrumb-inline a {
color: #3498db;
text-decoration: none;
}
.breadcrumb-inline a:hover {
text-decoration: underline;
}
.breadcrumb-inline .sep {
margin: 0 6px;
color: #bdc3c7;
}
.breadcrumb-inline .current {
color: #2c3e50;
font-weight: 500;
}
.nav-links {
margin: 20px 0;
}
.nav-links a {
display: inline-block;
padding: 10px 20px;
@ -76,14 +89,17 @@
border-radius: 4px;
margin-right: 10px;
}
.nav-links a:hover {
background-color: #2980b9;
}
/* Action buttons */
.action-buttons {
display: flex;
gap: 10px;
}
.action-btn {
color: white;
padding: 10px 16px;
@ -96,18 +112,23 @@
align-items: center;
gap: 6px;
}
.upload-action-btn {
background-color: #27ae60;
}
.upload-action-btn:hover {
background-color: #219a52;
}
.folder-action-btn {
background-color: #f39c12;
}
.folder-action-btn:hover {
background-color: #d68910;
}
/* Upload modal */
.modal-overlay {
display: none;
@ -121,9 +142,11 @@
justify-content: center;
align-items: center;
}
.modal-overlay.open {
display: flex;
}
.modal {
background: white;
border-radius: 8px;
@ -132,10 +155,12 @@
width: 90%;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}
.modal h3 {
margin: 0 0 15px 0;
color: #2c3e50;
}
.modal-close {
float: right;
background: none;
@ -145,39 +170,49 @@
color: #7f8c8d;
line-height: 1;
}
.modal-close:hover {
color: #2c3e50;
}
.file-table {
width: 100%;
border-collapse: collapse;
}
.file-table th,
.file-table td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #eee;
}
.file-table th {
background-color: #f8f9fa;
font-weight: 600;
color: #7f8c8d;
}
.file-table tr:hover {
background-color: #f8f9fa;
}
.epub-file {
background-color: #e8f6e9 !important;
}
.epub-file:hover {
background-color: #d4edda !important;
}
.folder-row {
background-color: #fff9e6 !important;
}
.folder-row:hover {
background-color: #fff3cd !important;
}
.epub-badge {
display: inline-block;
padding: 2px 8px;
@ -187,6 +222,7 @@
font-size: 0.75em;
margin-left: 8px;
}
.folder-badge {
display: inline-block;
padding: 2px 8px;
@ -196,25 +232,31 @@
font-size: 0.75em;
margin-left: 8px;
}
.file-icon {
margin-right: 8px;
}
.folder-link {
color: #2c3e50;
text-decoration: none;
cursor: pointer;
}
.folder-link:hover {
color: #3498db;
text-decoration: underline;
}
.upload-form {
margin-top: 10px;
}
.upload-form input[type="file"] {
margin: 10px 0;
width: 100%;
}
.upload-btn {
background-color: #27ae60;
color: white;
@ -225,59 +267,71 @@
font-size: 1em;
width: 100%;
}
.upload-btn:hover {
background-color: #219a52;
}
.upload-btn:disabled {
background-color: #95a5a6;
cursor: not-allowed;
}
.file-info {
color: #7f8c8d;
font-size: 0.85em;
margin: 8px 0;
}
.no-files {
text-align: center;
color: #95a5a6;
padding: 40px;
font-style: italic;
}
.message {
padding: 15px;
border-radius: 4px;
margin: 15px 0;
}
.message.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.message.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.contents-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.contents-title {
font-size: 1.1em;
font-weight: 600;
color: #34495e;
margin: 0;
}
.summary-inline {
color: #7f8c8d;
font-size: 0.9em;
}
#progress-container {
display: none;
margin-top: 10px;
}
#progress-bar {
width: 100%;
height: 20px;
@ -285,21 +339,25 @@
border-radius: 10px;
overflow: hidden;
}
#progress-fill {
height: 100%;
background-color: #27ae60;
width: 0%;
transition: width 0.3s;
}
#progress-text {
text-align: center;
margin-top: 5px;
font-size: 0.9em;
color: #7f8c8d;
}
.folder-form {
margin-top: 10px;
}
.folder-input {
width: 100%;
padding: 10px;
@ -309,6 +367,7 @@
margin-bottom: 10px;
box-sizing: border-box;
}
.folder-btn {
background-color: #f39c12;
color: white;
@ -319,28 +378,80 @@
font-size: 1em;
width: 100%;
}
.folder-btn:hover {
background-color: #d68910;
}
/* Delete button styles */
.delete-btn {
background: none;
border: none;
cursor: pointer;
font-size: 1.1em;
padding: 4px 8px;
border-radius: 4px;
color: #95a5a6;
transition: all 0.15s;
}
.delete-btn:hover {
background-color: #fee;
color: #e74c3c;
}
/* Kebab menu (three dots) styles */
.actions-col {
width: 60px;
text-align: center;
position: relative;
}
.kebab-menu-btn {
background: none;
border: none;
cursor: pointer;
font-size: 1.3em;
padding: 4px 8px;
border-radius: 4px;
color: #7f8c8d;
transition: all 0.15s;
line-height: 1;
}
.kebab-menu-btn:hover {
background-color: #f0f0f0;
color: #2c3e50;
}
.kebab-dropdown {
display: none;
position: absolute;
right: 10px;
top: 100%;
background: white;
border: 1px solid #ddd;
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
z-index: 100;
min-width: 140px;
overflow: hidden;
}
.kebab-dropdown.show {
display: block;
}
.kebab-dropdown-item {
padding: 10px 16px;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
color: #2c3e50;
font-size: 0.95em;
border: none;
background: none;
width: 100%;
text-align: left;
transition: background-color 0.15s;
}
.kebab-dropdown-item:hover {
background-color: #f8f9fa;
}
.kebab-dropdown-item.delete {
color: #e74c3c;
}
.kebab-dropdown-item.delete:hover {
background-color: #fee;
}
/* Failed uploads banner */
.failed-uploads-banner {
background-color: #fff3cd;
@ -350,20 +461,24 @@
margin-bottom: 15px;
display: none;
}
.failed-uploads-banner.show {
display: block;
}
.failed-uploads-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.failed-uploads-title {
font-weight: 600;
color: #856404;
margin: 0;
}
.dismiss-btn {
background: none;
border: none;
@ -373,9 +488,11 @@
padding: 0;
line-height: 1;
}
.dismiss-btn:hover {
color: #533f03;
}
.failed-file-item {
display: flex;
justify-content: space-between;
@ -383,21 +500,26 @@
padding: 8px 0;
border-bottom: 1px solid #ffe69c;
}
.failed-file-item:last-child {
border-bottom: none;
}
.failed-file-info {
flex: 1;
}
.failed-file-name {
font-weight: 500;
color: #856404;
}
.failed-file-error {
font-size: 0.85em;
color: #856404;
opacity: 0.8;
}
.retry-btn {
background-color: #ffc107;
color: #533f03;
@ -408,9 +530,11 @@
font-size: 0.9em;
font-weight: 600;
}
.retry-btn:hover {
background-color: #e0a800;
}
.retry-all-btn {
background-color: #ffc107;
color: #533f03;
@ -422,20 +546,24 @@
font-weight: 600;
margin-top: 10px;
}
.retry-all-btn:hover {
background-color: #e0a800;
}
/* Delete modal */
.delete-warning {
color: #e74c3c;
font-weight: 600;
margin: 10px 0;
}
.delete-item-name {
font-weight: 600;
color: #2c3e50;
word-break: break-all;
}
.delete-btn-confirm {
background-color: #e74c3c;
color: white;
@ -446,9 +574,11 @@
font-size: 1em;
width: 100%;
}
.delete-btn-confirm:hover {
background-color: #c0392b;
}
.delete-btn-cancel {
background-color: #95a5a6;
color: white;
@ -460,15 +590,81 @@
width: 100%;
margin-top: 10px;
}
.delete-btn-cancel:hover {
background-color: #7f8c8d;
}
/* Rename modal */
.rename-input {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
margin-bottom: 10px;
box-sizing: border-box;
}
.rename-btn {
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
width: 100%;
}
.rename-btn:hover {
background-color: #2980b9;
}
/* Move modal */
.move-current-path {
background-color: #f8f9fa;
padding: 10px;
border-radius: 4px;
margin: 10px 0;
font-family: monospace;
font-size: 0.9em;
color: #495057;
}
.move-new-path {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
margin-bottom: 10px;
box-sizing: border-box;
font-family: monospace;
}
.move-btn {
background-color: #9b59b6;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
width: 100%;
}
.move-btn:hover {
background-color: #8e44ad;
}
.loader-container {
display: flex;
justify-content: center;
align-items: center;
margin: 20px 0;
}
.loader {
width: 48px;
height: 48px;
@ -479,91 +675,119 @@
box-sizing: border-box;
animation: rotation 1s linear infinite;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* Mobile responsive styles */
@media (max-width: 600px) {
body {
padding: 10px;
font-size: 14px;
}
.card {
padding: 12px;
margin: 10px 0;
}
.page-header {
gap: 10px;
margin-bottom: 12px;
padding-bottom: 10px;
}
.page-header-left {
gap: 8px;
}
h1 {
font-size: 1.3em;
}
.breadcrumb-inline {
font-size: 0.95em;
}
.nav-links a {
padding: 8px 12px;
margin-right: 6px;
font-size: 0.9em;
}
.action-buttons {
gap: 6px;
}
.action-btn {
padding: 8px 10px;
font-size: 0.85em;
}
.file-table th,
.file-table td {
padding: 8px 6px;
font-size: 0.9em;
}
.file-table th {
font-size: 0.85em;
}
.file-icon {
margin-right: 4px;
}
.epub-badge,
.folder-badge {
padding: 2px 5px;
font-size: 0.65em;
margin-left: 4px;
}
.contents-header {
margin-bottom: 8px;
flex-wrap: wrap;
gap: 4px;
}
.contents-title {
font-size: 1em;
}
.summary-inline {
font-size: 0.8em;
}
.modal {
padding: 15px;
}
.modal h3 {
font-size: 1.1em;
}
.actions-col {
width: 40px;
}
.delete-btn {
font-size: 1em;
.kebab-menu-btn {
font-size: 1.1em;
padding: 2px 4px;
}
.kebab-dropdown {
right: 5px;
min-width: 120px;
}
.no-files {
padding: 20px;
font-size: 0.9em;
@ -571,6 +795,7 @@
}
</style>
</head>
<body>
<div class="nav-links">
<a href="/">Home</a>
@ -628,7 +853,9 @@
<input type="file" id="fileInput" onchange="validateFile()" multiple>
<button id="uploadBtn" class="upload-btn" onclick="uploadFile()" disabled>Upload</button>
<div id="progress-container">
<div id="progress-bar"><div id="progress-fill"></div></div>
<div id="progress-bar">
<div id="progress-fill"></div>
</div>
<div id="progress-text"></div>
</div>
</div>
@ -648,6 +875,41 @@
</div>
</div>
<!-- Rename Modal -->
<div class="modal-overlay" id="renameModal">
<div class="modal">
<button class="modal-close" onclick="closeRenameModal()">&times;</button>
<h3>✏️ Rename</h3>
<div class="folder-form">
<p class="file-info">Current name: <strong id="renameCurrentName"></strong></p>
<input type="text" id="renameNewName" class="rename-input" placeholder="New name...">
<input type="hidden" id="renameItemPath">
<input type="hidden" id="renameItemType">
<button class="rename-btn" onclick="confirmRename()">Rename</button>
<button class="delete-btn-cancel" onclick="closeRenameModal()">Cancel</button>
</div>
</div>
</div>
<!-- Move Modal -->
<div class="modal-overlay" id="moveModal">
<div class="modal">
<button class="modal-close" onclick="closeMoveModal()">&times;</button>
<h3>📦 Move</h3>
<div class="folder-form">
<p class="file-info">Moving: <strong id="moveItemName"></strong></p>
<p class="file-info">Current path:</p>
<div class="move-current-path" id="moveCurrentPath"></div>
<p class="file-info">New path (include filename):</p>
<input type="text" id="moveNewPath" class="move-new-path" placeholder="/new/path/filename.ext">
<input type="hidden" id="moveItemPath">
<input type="hidden" id="moveItemType">
<button class="move-btn" onclick="confirmMove()">Move</button>
<button class="delete-btn-cancel" onclick="closeMoveModal()">Cancel</button>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal-overlay" id="deleteModal">
<div class="modal">
@ -686,6 +948,29 @@
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)).toLocaleString() + ' ' + sizes[i];
}
// Close dropdown when clicking outside
document.addEventListener('click', function (event) {
if (!event.target.closest('.kebab-menu-btn')) {
document.querySelectorAll('.kebab-dropdown').forEach(function (dropdown) {
dropdown.classList.remove('show');
});
}
});
function toggleKebabMenu(event, index) {
event.stopPropagation();
const dropdown = document.getElementById('kebab-dropdown-' + index);
// Close all other dropdowns
document.querySelectorAll('.kebab-dropdown').forEach(function (d) {
if (d !== dropdown) {
d.classList.remove('show');
}
});
dropdown.classList.toggle('show');
}
async function hydrate() {
// Close modals when clicking overlay
document.querySelectorAll('.modal-overlay').forEach(function (overlay) {
@ -750,7 +1035,7 @@
return a.name.localeCompare(b.name);
});
sortedFiles.forEach(file => {
sortedFiles.forEach((file, index) => {
if (file.isDirectory) {
let folderPath = currentPath;
if (!folderPath.endsWith("/")) folderPath += "/";
@ -760,7 +1045,14 @@
fileTableContent += `<td><span class="file-icon">📁</span><a href="/files?path=${encodeURIComponent(folderPath)}" class="folder-link">${escapeHtml(file.name)}</a><span class="folder-badge">FOLDER</span></td>`;
fileTableContent += '<td>Folder</td>';
fileTableContent += '<td>-</td>';
fileTableContent += `<td class="actions-col"><button class="delete-btn" onclick="openDeleteModal('${file.name.replaceAll("'", "\\'")}', '${folderPath.replaceAll("'", "\\'")}', true)" title="Delete folder">🗑️</button></td>`;
fileTableContent += `<td class="actions-col">
<button class="kebab-menu-btn" onclick="toggleKebabMenu(event, ${index})" title="More actions"></button>
<div class="kebab-dropdown" id="kebab-dropdown-${index}">
<button class="kebab-dropdown-item" onclick="openRenameModal('${file.name.replaceAll("'", "\\'")}', '${folderPath.replaceAll("'", "\\'")}', true)">✏️ Rename</button>
<button class="kebab-dropdown-item" onclick="openMoveModal('${file.name.replaceAll("'", "\\'")}', '${folderPath.replaceAll("'", "\\'")}', true)">📦 Move</button>
<button class="kebab-dropdown-item delete" onclick="openDeleteModal('${file.name.replaceAll("'", "\\'")}', '${folderPath.replaceAll("'", "\\'")}', true)">🗑️ Delete</button>
</div>
</td>`;
fileTableContent += '</tr>';
} else {
let filePath = currentPath;
@ -773,7 +1065,14 @@
fileTableContent += '</td>';
fileTableContent += `<td>${file.name.split('.').pop().toUpperCase()}</td>`;
fileTableContent += `<td>${formatFileSize(file.size)}</td>`;
fileTableContent += `<td class="actions-col"><button class="delete-btn" onclick="openDeleteModal('${file.name.replaceAll("'", "\\'")}', '${filePath.replaceAll("'", "\\'")}', false)" title="Delete file">🗑️</button></td>`;
fileTableContent += `<td class="actions-col">
<button class="kebab-menu-btn" onclick="toggleKebabMenu(event, ${index})" title="More actions"></button>
<div class="kebab-dropdown" id="kebab-dropdown-${index}">
<button class="kebab-dropdown-item" onclick="openRenameModal('${file.name.replaceAll("'", "\\'")}', '${filePath.replaceAll("'", "\\'")}', false)">✏️ Rename</button>
<button class="kebab-dropdown-item" onclick="openMoveModal('${file.name.replaceAll("'", "\\'")}', '${filePath.replaceAll("'", "\\'")}', false)">📦 Move</button>
<button class="kebab-dropdown-item delete" onclick="openDeleteModal('${file.name.replaceAll("'", "\\'")}', '${filePath.replaceAll("'", "\\'")}', false)">🗑️ Delete</button>
</div>
</td>`;
fileTableContent += '</tr>';
}
});
@ -1175,12 +1474,130 @@ function retryAllFailedUploads() {
xhr.send(formData);
}
// Rename functions
function openRenameModal(name, path, isFolder) {
document.getElementById('renameCurrentName').textContent = (isFolder ? '📁 ' : '📄 ') + name;
document.getElementById('renameNewName').value = name;
document.getElementById('renameItemPath').value = path;
document.getElementById('renameItemType').value = isFolder ? 'folder' : 'file';
document.getElementById('renameModal').classList.add('open');
// Close kebab menu
document.querySelectorAll('.kebab-dropdown').forEach(d => d.classList.remove('show'));
}
function closeRenameModal() {
document.getElementById('renameModal').classList.remove('open');
}
function confirmRename() {
const newName = document.getElementById('renameNewName').value.trim();
const oldPath = document.getElementById('renameItemPath').value;
if (!newName) {
alert('Please enter a new name!');
return;
}
// Validate name (letters, numbers, underscores, hyphens, dots, and spaces)
const validName = /^[a-zA-Z0-9_\-\. ]+$/.test(newName);
if (!validName) {
alert('Name can only contain letters, numbers, underscores, hyphens, dots, and spaces.');
return;
}
const pathSegments = oldPath.split('/');
pathSegments[pathSegments.length - 1] = newName;
const newPath = pathSegments.join('/');
const formData = new FormData();
formData.append('oldPath', oldPath);
formData.append('newName', newName);
const xhr = new XMLHttpRequest();
xhr.open('POST', '/rename', true);
xhr.onload = function () {
if (xhr.status === 200) {
window.location.reload();
} else {
alert('Failed to rename: ' + xhr.responseText);
closeRenameModal();
}
};
xhr.onerror = function () {
alert('Failed to rename - network error');
closeRenameModal();
};
xhr.send(formData);
}
// Move functions
function openMoveModal(name, path, isFolder) {
document.getElementById('moveItemName').textContent = (isFolder ? '📁 ' : '📄 ') + name;
document.getElementById('moveCurrentPath').textContent = path;
document.getElementById('moveNewPath').value = path;
document.getElementById('moveItemPath').value = path;
document.getElementById('moveItemType').value = isFolder ? 'folder' : 'file';
document.getElementById('moveModal').classList.add('open');
// Close kebab menu
document.querySelectorAll('.kebab-dropdown').forEach(d => d.classList.remove('show'));
}
function closeMoveModal() {
document.getElementById('moveModal').classList.remove('open');
}
function confirmMove() {
const oldPath = document.getElementById('moveItemPath').value;
const newPath = document.getElementById('moveNewPath').value.trim();
if (!newPath) {
alert('Please enter a new path!');
return;
}
if (oldPath === newPath) {
alert('The new path is the same as the current path!');
return;
}
const formData = new FormData();
formData.append('oldPath', oldPath);
formData.append('newPath', newPath);
const xhr = new XMLHttpRequest();
xhr.open('POST', '/move', true);
xhr.onload = function () {
if (xhr.status === 200) {
window.location.reload();
} else {
alert('Failed to move: ' + xhr.responseText);
closeMoveModal();
}
};
xhr.onerror = function () {
alert('Failed to move - network error');
closeMoveModal();
};
xhr.send(formData);
}
// Delete functions
function openDeleteModal(name, path, isFolder) {
document.getElementById('deleteItemName').textContent = (isFolder ? '📁 ' : '📄 ') + name;
document.getElementById('deleteItemPath').value = path;
document.getElementById('deleteItemType').value = isFolder ? 'folder' : 'file';
document.getElementById('deleteModal').classList.add('open');
// Close kebab menu
document.querySelectorAll('.kebab-dropdown').forEach(d => d.classList.remove('show'));
}
function closeDeleteModal() {
@ -1218,4 +1635,5 @@ function retryAllFailedUploads() {
hydrate();
</script>
</body>
</html>

View 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
}

View 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);
};