fix folder renaming

This commit is contained in:
Ryan
2025-03-15 20:18:38 -04:00
committed by GitHub
parent d4efb9d822
commit fc628ed050
2 changed files with 36 additions and 6 deletions

View File

@@ -268,7 +268,9 @@ function openRenameFolderModal() {
showToast("Please select a valid folder to rename.");
return;
}
document.getElementById("newRenameFolderName").value = selectedFolder;
// Extract the basename for display.
const parts = selectedFolder.split("/");
document.getElementById("newRenameFolderName").value = parts[parts.length - 1];
document.getElementById("renameFolderModal").style.display = "block";
}
@@ -279,23 +281,29 @@ document.getElementById("cancelRenameFolder").addEventListener("click", function
document.getElementById("submitRenameFolder").addEventListener("click", function () {
const selectedFolder = window.currentFolder || "root";
const newFolderName = document.getElementById("newRenameFolderName").value.trim();
if (!newFolderName || newFolderName === selectedFolder) {
const newNameBasename = document.getElementById("newRenameFolderName").value.trim();
if (!newNameBasename || newNameBasename === selectedFolder.split("/").pop()) {
showToast("Please enter a valid new folder name.");
return;
}
// Get the parent folder path.
const parentPath = getParentFolder(selectedFolder);
// Build the full new folder path.
// If the parent is "root", new folder is just newNameBasename.
const newFolderFull = parentPath === "root" ? newNameBasename : parentPath + "/" + newNameBasename;
fetch("renameFolder.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ oldFolder: selectedFolder, newFolder: newFolderName })
body: JSON.stringify({ oldFolder: selectedFolder, newFolder: newFolderFull })
})
.then(response => response.json())
.then(data => {
console.log("Rename response:", data);
if (data.success) {
showToast("Folder renamed successfully!");
window.currentFolder = newFolderName;
loadFolderList(newFolderName);
window.currentFolder = newFolderFull;
loadFolderList(newFolderFull);
} else {
showToast("Error: " + (data.error || "Could not rename folder"));
}

View File

@@ -65,6 +65,28 @@ if (file_exists($newPath)) {
// Attempt to rename the folder.
if (rename($oldPath, $newPath)) {
// Update metadata.
$metadataFile = META_DIR . META_FILE;
if (file_exists($metadataFile)) {
$metadata = json_decode(file_get_contents($metadataFile), true);
$updated = false;
// Loop through each key in the metadata.
foreach ($metadata as $key => $value) {
// Check if the key is the folder itself or is inside the folder.
if ($key === $oldFolder || strpos($key, $oldFolder . "/") === 0) {
// Construct the new key by replacing the $oldFolder prefix with $newFolder.
$newKey = $newFolder . substr($key, strlen($oldFolder));
// Optional: remove a leading slash if it appears.
$newKey = ltrim($newKey, "/");
$metadata[$newKey] = $value;
unset($metadata[$key]);
$updated = true;
}
}
if ($updated) {
file_put_contents($metadataFile, json_encode($metadata, JSON_PRETTY_PRINT));
}
}
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Failed to rename folder.']);