Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9c7bb6493 | ||
|
|
6d588eb143 | ||
|
|
2092512f43 | ||
|
|
833eaa3194 | ||
|
|
edb8ff476a | ||
|
|
2e55f5f4d7 | ||
|
|
ae48119e15 | ||
|
|
f5410a92e7 | ||
|
|
7898ad4f1c | ||
|
|
d3ce26e83d | ||
|
|
b4a903e738 | ||
|
|
53bb72f4ab | ||
|
|
cef96f0047 | ||
|
|
559df3c396 | ||
|
|
a24321455a | ||
|
|
3c2faa5218 |
35
README.md
35
README.md
@@ -1,19 +1,27 @@
|
|||||||
# Multi File Upload Editor
|
# MFE - Lightweight Multi File Upload Editor
|
||||||
|
|
||||||
|
**Video demo:**
|
||||||
|
|
||||||
https://github.com/user-attachments/assets/179e6940-5798-4482-9a69-696f806c37de
|
https://github.com/user-attachments/assets/179e6940-5798-4482-9a69-696f806c37de
|
||||||
|
|
||||||
|
**Dark mode:**
|
||||||
|

|
||||||
|
|
||||||
changelogs available here: <https://github.com/error311/multi-file-upload-editor-docker/>
|
changelogs available here: <https://github.com/error311/multi-file-upload-editor-docker/>
|
||||||
|
|
||||||
Multi File Upload Editor is a lightweight, secure, self-hosted web application for uploading, editing, and managing files. Built with an Apache/PHP backend and a modern JavaScript (ES6 modules) frontend, it offers a responsive, dynamic file management interface. It serves as an alternative to solutions like FileGator or ProjectSend, providing an easy-to-setup experience ideal for document management, image galleries, firmware file hosting, and more.
|
MFE - Multi File Upload Editor is a lightweight, secure, self-hosted web application for uploading, syntax highlight editing, drag & drop and managing files. Built with an Apache/PHP backend and a modern JavaScript (ES6 modules) frontend, it offers a responsive, dynamic file management interface. It serves as an alternative to solutions like FileGator TinyFileManager or ProjectSend, providing an easy-to-setup experience ideal for document management, image galleries, firmware file hosting, and more.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Multiple File/Folder Uploads with Progress:**
|
- **Multiple File/Folder Uploads with Progress (Resumable.js Integration):**
|
||||||
- Users can select and upload multiple files & folders at once.
|
- Users can effortlessly upload multiple files and folders simultaneously by either selecting them through the file picker or dragging and dropping them directly into the interface.
|
||||||
- Each file upload displays an individual progress bar with percentage and upload speed.
|
- **Chunked Uploads:** Files are uploaded in configurable chunks (default set as 3 MB) to efficiently handle large files.
|
||||||
- Image files show a small thumbnail preview (with default Material icons for other file types).
|
- **Pause, Resume, and Retry:** Uploads can be paused and resumed at any time, with support for retrying failed chunks.
|
||||||
|
- **Real-Time Progress:** Each file shows an individual progress bar that displays percentage complete and upload speed.
|
||||||
|
- **File & Folder Grouping:** When many files are dropped, files are automatically grouped into a scrollable wrapper, ensuring the interface remains clean.
|
||||||
|
- **Secure Uploads:** All uploads integrate CSRF token validation and other security checks.
|
||||||
- **Built-in File Editing & Renaming:**
|
- **Built-in File Editing & Renaming:**
|
||||||
- Text-based files (e.g., .txt, .html, .js) can be opened and edited in a modal window using CodeMirror for:
|
- Text-based files (e.g., .txt, .html, .js) can be opened and edited in a modal window using CodeMirror for:
|
||||||
- Syntax highlighting
|
- Syntax highlighting
|
||||||
@@ -35,11 +43,12 @@ Multi File Upload Editor is a lightweight, secure, self-hosted web application f
|
|||||||
- **Copy Files:** Copy selected files to another folder with a unique-naming feature to prevent overwrites.
|
- **Copy Files:** Copy selected files to another folder with a unique-naming feature to prevent overwrites.
|
||||||
- **Move Files:** Move selected files to a different folder, automatically generating a unique filename if needed to avoid data loss.
|
- **Move Files:** Move selected files to a different folder, automatically generating a unique filename if needed to avoid data loss.
|
||||||
- **Download Files as ZIP:** Download selected files as a ZIP archive. Users can specify a custom name for the ZIP file via a modal dialog.
|
- **Download Files as ZIP:** Download selected files as a ZIP archive. Users can specify a custom name for the ZIP file via a modal dialog.
|
||||||
- **Drag & Drop:** Easily move files by selecting them from the file list and simply dragging them onto your desired folder in the folder tree. When you drop the files onto a folder, the system automatically moves them, updating your file organization in one seamless action.
|
- **Drag & Drop:** Easily move files by selecting them from the file list and simply dragging them onto your desired folder in the folder tree or breadcrumb. When you drop the files onto a folder, the system automatically moves them, updating your file organization in one seamless action.
|
||||||
- **Folder Management:**
|
- **Folder Management:**
|
||||||
- Organize files into folders and subfolders with the ability to create, rename, and delete folders.
|
- Organize files into folders and subfolders with the ability to create, rename, and delete folders.
|
||||||
- A dynamic folder tree in the UI allows users to navigate directories easily, and any changes are immediately reflected in real time.
|
- A dynamic folder tree in the UI allows users to navigate directories easily, and any changes are immediately reflected in real time.
|
||||||
- **Per-Folder Metadata Storage:** Each folder has its own metadata JSON file (e.g., `root_metadata.json`, `FolderName_metadata.json`), and operations (copy/move/rename) update these metadata files accordingly.
|
- **Per-Folder Metadata Storage:** Each folder has its own metadata JSON file (e.g., `root_metadata.json`, `FolderName_metadata.json`), and operations (copy/move/rename) update these metadata files accordingly.
|
||||||
|
- **Intuitive Breadcrumb Navigation:** Clickable breadcrumbs enable users to quickly jump to any parent folder, streamlining navigation across subfolders. Supports drag & drop to move files.
|
||||||
- **Sorting & Pagination:**
|
- **Sorting & Pagination:**
|
||||||
- The file list can be sorted by name, modified date, upload date, file size, or uploader.
|
- The file list can be sorted by name, modified date, upload date, file size, or uploader.
|
||||||
- Pagination controls let users navigate through files with selectable page sizes (10, 20, 50, or 100 items per page) and “Prev”/“Next” navigation buttons.
|
- Pagination controls let users navigate through files with selectable page sizes (10, 20, 50, or 100 items per page) and “Prev”/“Next” navigation buttons.
|
||||||
@@ -53,6 +62,7 @@ Multi File Upload Editor is a lightweight, secure, self-hosted web application f
|
|||||||
- Admin users can add or remove users through the interface.
|
- Admin users can add or remove users through the interface.
|
||||||
- Passwords are hashed using PHP’s `password_hash()` for security.
|
- Passwords are hashed using PHP’s `password_hash()` for security.
|
||||||
- All state-changing endpoints include CSRF token validation.
|
- All state-changing endpoints include CSRF token validation.
|
||||||
|
- Change password supported for all users.
|
||||||
- **Responsive, Dynamic & Persistent UI:**
|
- **Responsive, Dynamic & Persistent UI:**
|
||||||
- The interface is mobile-friendly and adapts to various screen sizes by hiding non-critical columns on small devices.
|
- The interface is mobile-friendly and adapts to various screen sizes by hiding non-critical columns on small devices.
|
||||||
- Asynchronous updates (via Fetch API and XMLHttpRequest) keep the UI responsive without full page reloads.
|
- Asynchronous updates (via Fetch API and XMLHttpRequest) keep the UI responsive without full page reloads.
|
||||||
@@ -91,22 +101,19 @@ Multi File Upload Editor is a lightweight, secure, self-hosted web application f
|
|||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
**Light mode**
|
**Light mode:**
|
||||||

|

|
||||||
|
|
||||||
**Dark mode**
|
**Dark editor:**
|
||||||

|
|
||||||
|
|
||||||
**Dark editor**
|
|
||||||

|

|
||||||
|
|
||||||
**Dark preview**
|
**Dark preview**
|
||||||

|

|
||||||
|
|
||||||
**Restore or Delete Trash**
|
**Restore or Delete Trash:**
|
||||||

|

|
||||||
|
|
||||||
**Login page**
|
**Login page:**
|
||||||

|

|
||||||
|
|
||||||
**iphone screenshots:**
|
**iphone screenshots:**
|
||||||
|
|||||||
20
addUser.php
20
addUser.php
@@ -6,9 +6,9 @@ $usersFile = USERS_DIR . USERS_FILE;
|
|||||||
|
|
||||||
// Determine if we are in setup mode:
|
// Determine if we are in setup mode:
|
||||||
// - Query parameter setup=1 is passed
|
// - Query parameter setup=1 is passed
|
||||||
// - And users.txt is either missing or empty
|
// - And users.txt is either missing or empty (zero bytes or trimmed content is empty)
|
||||||
$isSetup = (isset($_GET['setup']) && $_GET['setup'] == '1');
|
$isSetup = (isset($_GET['setup']) && $_GET['setup'] === '1');
|
||||||
if ($isSetup && (!file_exists($usersFile) || trim(file_get_contents($usersFile)) === '')) {
|
if ($isSetup && (!file_exists($usersFile) || filesize($usersFile) == 0 || trim(file_get_contents($usersFile)) === '')) {
|
||||||
// Allow initial admin creation without session checks.
|
// Allow initial admin creation without session checks.
|
||||||
$setupMode = true;
|
$setupMode = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -16,7 +16,7 @@ if ($isSetup && (!file_exists($usersFile) || trim(file_get_contents($usersFile))
|
|||||||
// In non-setup mode, check CSRF token and require admin privileges.
|
// In non-setup mode, check CSRF token and require admin privileges.
|
||||||
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
|
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
|
||||||
$receivedToken = isset($headers['x-csrf-token']) ? trim($headers['x-csrf-token']) : '';
|
$receivedToken = isset($headers['x-csrf-token']) ? trim($headers['x-csrf-token']) : '';
|
||||||
if ($receivedToken !== $_SESSION['csrf_token']) {
|
if (!isset($_SESSION['csrf_token']) || $receivedToken !== $_SESSION['csrf_token']) {
|
||||||
echo json_encode(["error" => "Invalid CSRF token"]);
|
echo json_encode(["error" => "Invalid CSRF token"]);
|
||||||
http_response_code(403);
|
http_response_code(403);
|
||||||
exit;
|
exit;
|
||||||
@@ -30,7 +30,7 @@ if ($isSetup && (!file_exists($usersFile) || trim(file_get_contents($usersFile))
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get input data from JSON
|
// Get input data from JSON.
|
||||||
$data = json_decode(file_get_contents("php://input"), true);
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
$newUsername = trim($data["username"] ?? "");
|
$newUsername = trim($data["username"] ?? "");
|
||||||
$newPassword = trim($data["password"] ?? "");
|
$newPassword = trim($data["password"] ?? "");
|
||||||
@@ -42,7 +42,7 @@ if ($setupMode) {
|
|||||||
$isAdmin = !empty($data["isAdmin"]) ? "1" : "0"; // "1" for admin, "0" for regular user.
|
$isAdmin = !empty($data["isAdmin"]) ? "1" : "0"; // "1" for admin, "0" for regular user.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate input
|
// Validate input.
|
||||||
if (!$newUsername || !$newPassword) {
|
if (!$newUsername || !$newPassword) {
|
||||||
echo json_encode(["error" => "Username and password required"]);
|
echo json_encode(["error" => "Username and password required"]);
|
||||||
exit;
|
exit;
|
||||||
@@ -54,12 +54,12 @@ if (!preg_match('/^[A-Za-z0-9_\- ]+$/', $newUsername)) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure users.txt exists
|
// Ensure users.txt exists.
|
||||||
if (!file_exists($usersFile)) {
|
if (!file_exists($usersFile)) {
|
||||||
file_put_contents($usersFile, '');
|
file_put_contents($usersFile, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if username already exists
|
// Check if username already exists.
|
||||||
$existingUsers = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
$existingUsers = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
foreach ($existingUsers as $line) {
|
foreach ($existingUsers as $line) {
|
||||||
list($storedUser, $storedHash, $storedRole) = explode(':', trim($line));
|
list($storedUser, $storedHash, $storedRole) = explode(':', trim($line));
|
||||||
@@ -69,10 +69,10 @@ foreach ($existingUsers as $line) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash the password
|
// Hash the password.
|
||||||
$hashedPassword = password_hash($newPassword, PASSWORD_BCRYPT);
|
$hashedPassword = password_hash($newPassword, PASSWORD_BCRYPT);
|
||||||
|
|
||||||
// Prepare new user line
|
// Prepare new user line.
|
||||||
$newUserLine = $newUsername . ":" . $hashedPassword . ":" . $isAdmin . PHP_EOL;
|
$newUserLine = $newUsername . ":" . $hashedPassword . ":" . $isAdmin . PHP_EOL;
|
||||||
|
|
||||||
// In setup mode, overwrite users.txt; otherwise, append to it.
|
// In setup mode, overwrite users.txt; otherwise, append to it.
|
||||||
|
|||||||
98
auth.js
98
auth.js
@@ -37,12 +37,10 @@ function initAuth() {
|
|||||||
restoreBtn.classList.add("btn", "btn-warning");
|
restoreBtn.classList.add("btn", "btn-warning");
|
||||||
// Use a material icon.
|
// Use a material icon.
|
||||||
restoreBtn.innerHTML = '<i class="material-icons" title="Restore/Delete Trash">restore_from_trash</i>';
|
restoreBtn.innerHTML = '<i class="material-icons" title="Restore/Delete Trash">restore_from_trash</i>';
|
||||||
|
|
||||||
const headerButtons = document.querySelector(".header-buttons");
|
const headerButtons = document.querySelector(".header-buttons");
|
||||||
if (headerButtons) {
|
if (headerButtons) {
|
||||||
// Insert after the third child if available.
|
if (headerButtons.children.length >= 5) {
|
||||||
if (headerButtons.children.length >= 4) {
|
headerButtons.insertBefore(restoreBtn, headerButtons.children[5]);
|
||||||
headerButtons.insertBefore(restoreBtn, headerButtons.children[4]);
|
|
||||||
} else {
|
} else {
|
||||||
headerButtons.appendChild(restoreBtn);
|
headerButtons.appendChild(restoreBtn);
|
||||||
}
|
}
|
||||||
@@ -54,20 +52,17 @@ function initAuth() {
|
|||||||
const removeUserBtn = document.getElementById("removeUserBtn");
|
const removeUserBtn = document.getElementById("removeUserBtn");
|
||||||
if (addUserBtn) addUserBtn.style.display = "none";
|
if (addUserBtn) addUserBtn.style.display = "none";
|
||||||
if (removeUserBtn) removeUserBtn.style.display = "none";
|
if (removeUserBtn) removeUserBtn.style.display = "none";
|
||||||
// If not admin, hide the restore button.
|
|
||||||
const restoreBtn = document.getElementById("restoreFilesBtn");
|
const restoreBtn = document.getElementById("restoreFilesBtn");
|
||||||
if (restoreBtn) {
|
if (restoreBtn) {
|
||||||
restoreBtn.style.display = "none";
|
restoreBtn.style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Set items-per-page.
|
|
||||||
const selectElem = document.querySelector(".form-control.bottom-select");
|
const selectElem = document.querySelector(".form-control.bottom-select");
|
||||||
if (selectElem) {
|
if (selectElem) {
|
||||||
const stored = localStorage.getItem("itemsPerPage") || "10";
|
const stored = localStorage.getItem("itemsPerPage") || "10";
|
||||||
selectElem.value = stored;
|
selectElem.value = stored;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Do not show a toast message repeatedly during initial check.
|
|
||||||
toggleVisibility("loginForm", true);
|
toggleVisibility("loginForm", true);
|
||||||
toggleVisibility("mainOperations", false);
|
toggleVisibility("mainOperations", false);
|
||||||
toggleVisibility("uploadFileForm", false);
|
toggleVisibility("uploadFileForm", false);
|
||||||
@@ -78,14 +73,19 @@ function initAuth() {
|
|||||||
console.error("Error checking authentication:", error);
|
console.error("Error checking authentication:", error);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Attach login event listener once.
|
// Attach login event listener.
|
||||||
const authForm = document.getElementById("authForm");
|
const authForm = document.getElementById("authForm");
|
||||||
if (authForm) {
|
if (authForm) {
|
||||||
authForm.addEventListener("submit", function (event) {
|
authForm.addEventListener("submit", function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
// Get the "Remember me" checkbox value.
|
||||||
|
const rememberMe = document.getElementById("rememberMeCheckbox")
|
||||||
|
? document.getElementById("rememberMeCheckbox").checked
|
||||||
|
: false;
|
||||||
const formData = {
|
const formData = {
|
||||||
username: document.getElementById("loginUsername").value.trim(),
|
username: document.getElementById("loginUsername").value.trim(),
|
||||||
password: document.getElementById("loginPassword").value.trim()
|
password: document.getElementById("loginPassword").value.trim(),
|
||||||
|
remember_me: rememberMe
|
||||||
};
|
};
|
||||||
sendRequest("auth.php", "POST", formData, { "X-CSRF-Token": window.csrfToken })
|
sendRequest("auth.php", "POST", formData, { "X-CSRF-Token": window.csrfToken })
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@@ -94,7 +94,19 @@ function initAuth() {
|
|||||||
sessionStorage.setItem("welcomeMessage", "Welcome back, " + formData.username + "!");
|
sessionStorage.setItem("welcomeMessage", "Welcome back, " + formData.username + "!");
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
} else {
|
} else {
|
||||||
showToast("Login failed: " + (data.error || "Unknown error"));
|
if (data.error && data.error.includes("Too many failed login attempts")) {
|
||||||
|
showToast(data.error);
|
||||||
|
const loginButton = authForm.querySelector("button[type='submit']");
|
||||||
|
if (loginButton) {
|
||||||
|
loginButton.disabled = true;
|
||||||
|
setTimeout(() => {
|
||||||
|
loginButton.disabled = false;
|
||||||
|
showToast("You can now try logging in again.");
|
||||||
|
}, 30 * 60 * 1000);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
showToast("Login failed: " + (data.error || "Unknown error"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => console.error("❌ Error logging in:", error));
|
.catch(error => console.error("❌ Error logging in:", error));
|
||||||
@@ -119,8 +131,10 @@ function initAuth() {
|
|||||||
});
|
});
|
||||||
document.getElementById("saveUserBtn").addEventListener("click", function () {
|
document.getElementById("saveUserBtn").addEventListener("click", function () {
|
||||||
const newUsername = document.getElementById("newUsername").value.trim();
|
const newUsername = document.getElementById("newUsername").value.trim();
|
||||||
const newPassword = document.getElementById("newPassword").value.trim();
|
// Use the new ID for the add user modal's password field.
|
||||||
|
const newPassword = document.getElementById("addUserPassword").value.trim();
|
||||||
const isAdmin = document.getElementById("isAdmin").checked;
|
const isAdmin = document.getElementById("isAdmin").checked;
|
||||||
|
console.log("newUsername:", newUsername, "newPassword:", newPassword);
|
||||||
if (!newUsername || !newPassword) {
|
if (!newUsername || !newPassword) {
|
||||||
showToast("Username and password are required!");
|
showToast("Username and password are required!");
|
||||||
return;
|
return;
|
||||||
@@ -143,7 +157,7 @@ function initAuth() {
|
|||||||
if (data.success) {
|
if (data.success) {
|
||||||
showToast("User added successfully!");
|
showToast("User added successfully!");
|
||||||
closeAddUserModal();
|
closeAddUserModal();
|
||||||
checkAuthentication(false); // Re-check without showing toast
|
checkAuthentication(false);
|
||||||
} else {
|
} else {
|
||||||
showToast("Error: " + (data.error || "Could not add user"));
|
showToast("Error: " + (data.error || "Could not add user"));
|
||||||
}
|
}
|
||||||
@@ -193,10 +207,66 @@ function initAuth() {
|
|||||||
document.getElementById("cancelRemoveUserBtn").addEventListener("click", function () {
|
document.getElementById("cancelRemoveUserBtn").addEventListener("click", function () {
|
||||||
closeRemoveUserModal();
|
closeRemoveUserModal();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById("changePasswordBtn").addEventListener("click", function() {
|
||||||
|
// Show the Change Password modal.
|
||||||
|
document.getElementById("changePasswordModal").style.display = "block";
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById("closeChangePasswordModal").addEventListener("click", function() {
|
||||||
|
// Hide the Change Password modal.
|
||||||
|
document.getElementById("changePasswordModal").style.display = "none";
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById("saveNewPasswordBtn").addEventListener("click", function() {
|
||||||
|
const oldPassword = document.getElementById("oldPassword").value.trim();
|
||||||
|
const newPassword = document.getElementById("newPassword").value.trim(); // Change Password modal field
|
||||||
|
const confirmPassword = document.getElementById("confirmPassword").value.trim();
|
||||||
|
|
||||||
|
if (!oldPassword || !newPassword || !confirmPassword) {
|
||||||
|
showToast("Please fill in all fields.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newPassword !== confirmPassword) {
|
||||||
|
showToast("New passwords do not match.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the data to send.
|
||||||
|
const data = { oldPassword, newPassword, confirmPassword };
|
||||||
|
|
||||||
|
// Send request to changePassword.php.
|
||||||
|
fetch("changePassword.php", {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "include",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-CSRF-Token": window.csrfToken
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
showToast(result.success);
|
||||||
|
// Clear form fields and close modal.
|
||||||
|
document.getElementById("oldPassword").value = "";
|
||||||
|
document.getElementById("newPassword").value = "";
|
||||||
|
document.getElementById("confirmPassword").value = "";
|
||||||
|
document.getElementById("changePasswordModal").style.display = "none";
|
||||||
|
} else {
|
||||||
|
showToast("Error: " + (result.error || "Could not change password."));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error changing password:", error);
|
||||||
|
showToast("Error changing password.");
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkAuthentication(showLoginToast = true) {
|
function checkAuthentication(showLoginToast = true) {
|
||||||
// Optionally pass a flag so we don't show a toast every time.
|
|
||||||
return sendRequest("checkAuth.php")
|
return sendRequest("checkAuth.php")
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.setup) {
|
if (data.setup) {
|
||||||
@@ -246,7 +316,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
|
|
||||||
function resetUserForm() {
|
function resetUserForm() {
|
||||||
document.getElementById("newUsername").value = "";
|
document.getElementById("newUsername").value = "";
|
||||||
document.getElementById("newPassword").value = "";
|
document.getElementById("addUserPassword").value = ""; // Updated for add user modal
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeAddUserModal() {
|
function closeAddUserModal() {
|
||||||
|
|||||||
96
auth.php
96
auth.php
@@ -4,15 +4,54 @@ header('Content-Type: application/json');
|
|||||||
|
|
||||||
$usersFile = USERS_DIR . USERS_FILE;
|
$usersFile = USERS_DIR . USERS_FILE;
|
||||||
|
|
||||||
// Function to authenticate user
|
// --- Brute Force Protection Settings ---
|
||||||
|
$maxAttempts = 5;
|
||||||
|
$lockoutTime = 30 * 60; // 30 minutes in seconds
|
||||||
|
$attemptsFile = USERS_DIR . 'failed_logins.json'; // JSON file for tracking failed login attempts
|
||||||
|
$failedLogFile = USERS_DIR . 'failed_login.log'; // Plain text log for fail2ban
|
||||||
|
|
||||||
|
// Persistent tokens file for "Remember me"
|
||||||
|
$persistentTokensFile = USERS_DIR . 'persistent_tokens.json';
|
||||||
|
|
||||||
|
// Load failed attempts data from file.
|
||||||
|
function loadFailedAttempts($file) {
|
||||||
|
if (file_exists($file)) {
|
||||||
|
$data = json_decode(file_get_contents($file), true);
|
||||||
|
if (is_array($data)) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save failed attempts data to file.
|
||||||
|
function saveFailedAttempts($file, $data) {
|
||||||
|
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get current IP address.
|
||||||
|
$ip = $_SERVER['REMOTE_ADDR'];
|
||||||
|
$currentTime = time();
|
||||||
|
|
||||||
|
// Load failed attempts.
|
||||||
|
$failedAttempts = loadFailedAttempts($attemptsFile);
|
||||||
|
|
||||||
|
// Check if this IP is currently locked out.
|
||||||
|
if (isset($failedAttempts[$ip])) {
|
||||||
|
$attemptData = $failedAttempts[$ip];
|
||||||
|
if ($attemptData['count'] >= $maxAttempts && ($currentTime - $attemptData['last_attempt']) < $lockoutTime) {
|
||||||
|
echo json_encode(["error" => "Too many failed login attempts. Please try again later."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Authentication Function ---
|
||||||
function authenticate($username, $password)
|
function authenticate($username, $password)
|
||||||
{
|
{
|
||||||
global $usersFile;
|
global $usersFile;
|
||||||
|
|
||||||
if (!file_exists($usersFile)) {
|
if (!file_exists($usersFile)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$lines = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
$lines = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
list($storedUser, $storedPass, $storedRole) = explode(':', trim($line), 3);
|
list($storedUser, $storedPass, $storedRole) = explode(':', trim($line), 3);
|
||||||
@@ -23,10 +62,11 @@ function authenticate($username, $password)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get JSON input
|
// Get JSON input.
|
||||||
$data = json_decode(file_get_contents("php://input"), true);
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
$username = trim($data["username"] ?? "");
|
$username = trim($data["username"] ?? "");
|
||||||
$password = trim($data["password"] ?? "");
|
$password = trim($data["password"] ?? "");
|
||||||
|
$rememberMe = isset($data["remember_me"]) && $data["remember_me"] === true;
|
||||||
|
|
||||||
// Validate input: ensure both fields are provided.
|
// Validate input: ensure both fields are provided.
|
||||||
if (!$username || !$password) {
|
if (!$username || !$password) {
|
||||||
@@ -34,22 +74,64 @@ if (!$username || !$password) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate username format: allow only letters, numbers, underscores, dashes, and spaces.
|
// Validate username format.
|
||||||
if (!preg_match('/^[A-Za-z0-9_\- ]+$/', $username)) {
|
if (!preg_match('/^[A-Za-z0-9_\- ]+$/', $username)) {
|
||||||
echo json_encode(["error" => "Invalid username format. Only letters, numbers, underscores, dashes, and spaces are allowed."]);
|
echo json_encode(["error" => "Invalid username format. Only letters, numbers, underscores, dashes, and spaces are allowed."]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Authenticate user
|
// Attempt to authenticate the user.
|
||||||
$userRole = authenticate($username, $password);
|
$userRole = authenticate($username, $password);
|
||||||
if ($userRole !== false) {
|
if ($userRole !== false) {
|
||||||
// Regenerate session ID to mitigate session fixation attacks
|
// On successful login, reset failed attempts for this IP.
|
||||||
|
if (isset($failedAttempts[$ip])) {
|
||||||
|
unset($failedAttempts[$ip]);
|
||||||
|
saveFailedAttempts($attemptsFile, $failedAttempts);
|
||||||
|
}
|
||||||
|
// Regenerate session ID to mitigate session fixation attacks.
|
||||||
session_regenerate_id(true);
|
session_regenerate_id(true);
|
||||||
$_SESSION["authenticated"] = true;
|
$_SESSION["authenticated"] = true;
|
||||||
$_SESSION["username"] = $username;
|
$_SESSION["username"] = $username;
|
||||||
$_SESSION["isAdmin"] = ($userRole === "1"); // "1" indicates admin
|
$_SESSION["isAdmin"] = ($userRole === "1"); // "1" indicates admin
|
||||||
|
|
||||||
|
// If "Remember me" is checked, generate a persistent login token.
|
||||||
|
if ($rememberMe) {
|
||||||
|
// Generate a secure random token.
|
||||||
|
$token = bin2hex(random_bytes(32));
|
||||||
|
$expiry = time() + (30 * 24 * 60 * 60); // 30 days
|
||||||
|
// Load existing persistent tokens.
|
||||||
|
$persistentTokens = [];
|
||||||
|
if (file_exists($persistentTokensFile)) {
|
||||||
|
$persistentTokens = json_decode(file_get_contents($persistentTokensFile), true);
|
||||||
|
if (!is_array($persistentTokens)) {
|
||||||
|
$persistentTokens = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Save token along with username and expiry.
|
||||||
|
$persistentTokens[$token] = [
|
||||||
|
"username" => $username,
|
||||||
|
"expiry" => $expiry
|
||||||
|
];
|
||||||
|
file_put_contents($persistentTokensFile, json_encode($persistentTokens, JSON_PRETTY_PRINT));
|
||||||
|
// Set the cookie. (Assuming $secure is defined in config.php.)
|
||||||
|
setcookie('remember_me_token', $token, $expiry, '/', '', $secure, true);
|
||||||
|
}
|
||||||
|
|
||||||
echo json_encode(["success" => "Login successful", "isAdmin" => $_SESSION["isAdmin"]]);
|
echo json_encode(["success" => "Login successful", "isAdmin" => $_SESSION["isAdmin"]]);
|
||||||
} else {
|
} else {
|
||||||
|
// On failed login, update failed attempts.
|
||||||
|
if (isset($failedAttempts[$ip])) {
|
||||||
|
$failedAttempts[$ip]['count']++;
|
||||||
|
$failedAttempts[$ip]['last_attempt'] = $currentTime;
|
||||||
|
} else {
|
||||||
|
$failedAttempts[$ip] = ['count' => 1, 'last_attempt' => $currentTime];
|
||||||
|
}
|
||||||
|
saveFailedAttempts($attemptsFile, $failedAttempts);
|
||||||
|
|
||||||
|
// Log the failed attempt to the plain text log for fail2ban.
|
||||||
|
$logLine = date('Y-m-d H:i:s') . " - Failed login attempt for username: " . $username . " from IP: " . $ip . PHP_EOL;
|
||||||
|
file_put_contents($failedLogFile, $logLine, FILE_APPEND);
|
||||||
|
|
||||||
echo json_encode(["error" => "Invalid credentials"]);
|
echo json_encode(["error" => "Invalid credentials"]);
|
||||||
}
|
}
|
||||||
|
?>
|
||||||
85
changePassword.php
Normal file
85
changePassword.php
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
// changePassword.php
|
||||||
|
require 'config.php';
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
// Make sure the user is logged in.
|
||||||
|
session_start();
|
||||||
|
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||||
|
echo json_encode(["error" => "Unauthorized"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$username = $_SESSION['username'] ?? '';
|
||||||
|
if (!$username) {
|
||||||
|
echo json_encode(["error" => "No username in session"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CSRF token check.
|
||||||
|
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
|
||||||
|
$receivedToken = isset($headers['x-csrf-token']) ? trim($headers['x-csrf-token']) : '';
|
||||||
|
if ($receivedToken !== $_SESSION['csrf_token']) {
|
||||||
|
echo json_encode(["error" => "Invalid CSRF token"]);
|
||||||
|
http_response_code(403);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get POST data.
|
||||||
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
|
$oldPassword = trim($data["oldPassword"] ?? "");
|
||||||
|
$newPassword = trim($data["newPassword"] ?? "");
|
||||||
|
$confirmPassword = trim($data["confirmPassword"] ?? "");
|
||||||
|
|
||||||
|
// Validate input.
|
||||||
|
if (!$oldPassword || !$newPassword || !$confirmPassword) {
|
||||||
|
echo json_encode(["error" => "All fields are required."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
if ($newPassword !== $confirmPassword) {
|
||||||
|
echo json_encode(["error" => "New passwords do not match."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path to users file.
|
||||||
|
$usersFile = USERS_DIR . USERS_FILE;
|
||||||
|
if (!file_exists($usersFile)) {
|
||||||
|
echo json_encode(["error" => "Users file not found"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read current users.
|
||||||
|
$lines = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
$userFound = false;
|
||||||
|
$newLines = [];
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
list($storedUser, $storedHash, $storedRole) = explode(':', trim($line));
|
||||||
|
if ($storedUser === $username) {
|
||||||
|
$userFound = true;
|
||||||
|
// Verify the old password.
|
||||||
|
if (!password_verify($oldPassword, $storedHash)) {
|
||||||
|
echo json_encode(["error" => "Old password is incorrect."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
// Hash the new password.
|
||||||
|
$newHashedPassword = password_hash($newPassword, PASSWORD_BCRYPT);
|
||||||
|
// Rebuild the line with the new hash.
|
||||||
|
$newLines[] = $username . ":" . $newHashedPassword . ":" . $storedRole;
|
||||||
|
} else {
|
||||||
|
$newLines[] = $line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$userFound) {
|
||||||
|
echo json_encode(["error" => "User not found."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save updated users file.
|
||||||
|
if (file_put_contents($usersFile, implode(PHP_EOL, $newLines) . PHP_EOL)) {
|
||||||
|
echo json_encode(["success" => "Password updated successfully."]);
|
||||||
|
} else {
|
||||||
|
echo json_encode(["error" => "Could not update password."]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
23
config.php
23
config.php
@@ -27,6 +27,29 @@ if (empty($_SESSION['csrf_token'])) {
|
|||||||
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auto-login via persistent token if session is not active.
|
||||||
|
if (!isset($_SESSION["authenticated"]) && isset($_COOKIE['remember_me_token'])) {
|
||||||
|
$persistentTokensFile = USERS_DIR . 'persistent_tokens.json';
|
||||||
|
if (file_exists($persistentTokensFile)) {
|
||||||
|
$persistentTokens = json_decode(file_get_contents($persistentTokensFile), true);
|
||||||
|
if (is_array($persistentTokens) && isset($persistentTokens[$_COOKIE['remember_me_token']])) {
|
||||||
|
$tokenData = $persistentTokens[$_COOKIE['remember_me_token']];
|
||||||
|
if ($tokenData['expiry'] >= time()) {
|
||||||
|
// Token is valid; auto-authenticate the user.
|
||||||
|
$_SESSION["authenticated"] = true;
|
||||||
|
$_SESSION["username"] = $tokenData["username"];
|
||||||
|
// Optionally, set admin status if stored in token data:
|
||||||
|
// $_SESSION["isAdmin"] = $tokenData["isAdmin"];
|
||||||
|
} else {
|
||||||
|
// Token expired; remove it and clear the cookie.
|
||||||
|
unset($persistentTokens[$_COOKIE['remember_me_token']]);
|
||||||
|
file_put_contents($persistentTokensFile, json_encode($persistentTokens, JSON_PRETTY_PRINT));
|
||||||
|
setcookie('remember_me_token', '', time() - 3600, '/', '', $secure, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Define BASE_URL (this should point to where index.html is, e.g. your uploads directory)
|
// Define BASE_URL (this should point to where index.html is, e.g. your uploads directory)
|
||||||
define('BASE_URL', 'http://yourwebsite/uploads/');
|
define('BASE_URL', 'http://yourwebsite/uploads/');
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,8 @@ if ($folder !== 'root' && !preg_match('/^[A-Za-z0-9_\- \/]+$/', $folder)) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optionally, you could check if the file exists in the uploads directory here.
|
|
||||||
|
|
||||||
// Generate a secure token.
|
// Generate a secure token.
|
||||||
$token = bin2hex(random_bytes(4)); // 8 hex characters.
|
$token = bin2hex(random_bytes(16)); // 32 hex characters.
|
||||||
|
|
||||||
// Calculate expiration (Unix timestamp).
|
// Calculate expiration (Unix timestamp).
|
||||||
$expires = time() + ($expirationMinutes * 60);
|
$expires = time() + ($expirationMinutes * 60);
|
||||||
@@ -42,6 +40,14 @@ if (file_exists($shareFile)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up expired share links.
|
||||||
|
$currentTime = time();
|
||||||
|
foreach ($shareLinks as $key => $link) {
|
||||||
|
if ($link["expires"] < $currentTime) {
|
||||||
|
unset($shareLinks[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add record.
|
// Add record.
|
||||||
$shareLinks[$token] = [
|
$shareLinks[$token] = [
|
||||||
"folder" => $folder,
|
"folder" => $folder,
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ function enhancedPreviewFile(fileUrl, fileName) {
|
|||||||
modal.innerHTML = `
|
modal.innerHTML = `
|
||||||
<div class="modal-content image-preview-modal-content" style="position: relative; max-width: 90vw; max-height: 90vh;">
|
<div class="modal-content image-preview-modal-content" style="position: relative; max-width: 90vw; max-height: 90vh;">
|
||||||
<span id="closeFileModal" class="close-image-modal" style="position: absolute; top: 10px; right: 10px; font-size: 24px; cursor: pointer;">×</span>
|
<span id="closeFileModal" class="close-image-modal" style="position: absolute; top: 10px; right: 10px; font-size: 24px; cursor: pointer;">×</span>
|
||||||
<h4 class="image-modal-header" style="text-align: center; margin-top: 40px;"></h4>
|
<h4 class="image-modal-header"></h4>
|
||||||
<div class="file-preview-container" style="position: relative; text-align: center;"></div>
|
<div class="file-preview-container" style="position: relative; text-align: center;"></div>
|
||||||
</div>`;
|
</div>`;
|
||||||
document.body.appendChild(modal);
|
document.body.appendChild(modal);
|
||||||
@@ -1007,7 +1007,7 @@ function adjustEditorSize() {
|
|||||||
if (modal && window.currentEditor) {
|
if (modal && window.currentEditor) {
|
||||||
// Calculate available height for the editor.
|
// Calculate available height for the editor.
|
||||||
// If you have a header or footer inside the modal, subtract their heights.
|
// If you have a header or footer inside the modal, subtract their heights.
|
||||||
const headerHeight = 60;
|
const headerHeight = 60; // adjust this value as needed
|
||||||
const availableHeight = modal.clientHeight - headerHeight;
|
const availableHeight = modal.clientHeight - headerHeight;
|
||||||
window.currentEditor.setSize("100%", availableHeight + "px");
|
window.currentEditor.setSize("100%", availableHeight + "px");
|
||||||
}
|
}
|
||||||
@@ -1054,12 +1054,12 @@ export function editFile(fileName, folder) {
|
|||||||
modal.innerHTML = `
|
modal.innerHTML = `
|
||||||
<div class="editor-header">
|
<div class="editor-header">
|
||||||
<h3 class="editor-title">Editing: ${fileName}</h3>
|
<h3 class="editor-title">Editing: ${fileName}</h3>
|
||||||
|
<div class="editor-controls">
|
||||||
|
<button id="decreaseFont" class="btn btn-sm btn-secondary">A-</button>
|
||||||
|
<button id="increaseFont" class="btn btn-sm btn-secondary">A+</button>
|
||||||
|
</div>
|
||||||
<button id="closeEditorX" class="editor-close-btn">×</button>
|
<button id="closeEditorX" class="editor-close-btn">×</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="editorControls" class="editor-controls">
|
|
||||||
<button id="decreaseFont" class="btn btn-sm btn-secondary">A-</button>
|
|
||||||
<button id="increaseFont" class="btn btn-sm btn-secondary">A+</button>
|
|
||||||
</div>
|
|
||||||
<textarea id="fileEditor" class="editor-textarea">${content}</textarea>
|
<textarea id="fileEditor" class="editor-textarea">${content}</textarea>
|
||||||
<div class="editor-footer">
|
<div class="editor-footer">
|
||||||
<button id="saveBtn" class="btn btn-primary">Save</button>
|
<button id="saveBtn" class="btn btn-primary">Save</button>
|
||||||
@@ -1157,13 +1157,17 @@ export function saveFile(fileName, folder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function displayFilePreview(file, container) {
|
export function displayFilePreview(file, container) {
|
||||||
|
// Use the underlying File object if it exists (for resumable files)
|
||||||
|
const actualFile = file.file || file;
|
||||||
container.style.display = "inline-block";
|
container.style.display = "inline-block";
|
||||||
if (/\.(jpg|jpeg|png|gif|bmp|webp|svg|ico)$/i.test(file.name)) {
|
if (/\.(jpg|jpeg|png|gif|bmp|webp|svg|ico)$/i.test(actualFile.name)) {
|
||||||
const img = document.createElement("img");
|
const img = document.createElement("img");
|
||||||
img.src = URL.createObjectURL(file);
|
img.src = URL.createObjectURL(actualFile);
|
||||||
img.classList.add("file-preview-img");
|
img.classList.add("file-preview-img");
|
||||||
|
container.innerHTML = ""; // Clear previous content
|
||||||
container.appendChild(img);
|
container.appendChild(img);
|
||||||
} else {
|
} else {
|
||||||
|
container.innerHTML = ""; // Clear previous content
|
||||||
const iconSpan = document.createElement("span");
|
const iconSpan = document.createElement("span");
|
||||||
iconSpan.classList.add("material-icons", "file-icon");
|
iconSpan.classList.add("material-icons", "file-icon");
|
||||||
iconSpan.textContent = "insert_drive_file";
|
iconSpan.textContent = "insert_drive_file";
|
||||||
|
|||||||
159
folderManager.js
159
folderManager.js
@@ -60,7 +60,112 @@ function getParentFolder(folder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------
|
// ----------------------
|
||||||
// DOM Building Functions
|
// Breadcrumb Functions
|
||||||
|
// ----------------------
|
||||||
|
// Render breadcrumb for a normalized folder path.
|
||||||
|
// For example, if window.currentFolder is "Folder1/Folder1SubFolder2",
|
||||||
|
// this will return: Root / Folder1 / Folder1SubFolder2.
|
||||||
|
function renderBreadcrumb(normalizedFolder) {
|
||||||
|
if (normalizedFolder === "root") {
|
||||||
|
return `<span class="breadcrumb-link" data-folder="root">Root</span>`;
|
||||||
|
}
|
||||||
|
const parts = normalizedFolder.split("/");
|
||||||
|
let breadcrumbItems = [];
|
||||||
|
// Always start with "Root".
|
||||||
|
breadcrumbItems.push(`<span class="breadcrumb-link" data-folder="root">Root</span>`);
|
||||||
|
let cumulative = "";
|
||||||
|
parts.forEach((part, index) => {
|
||||||
|
cumulative = index === 0 ? part : cumulative + "/" + part;
|
||||||
|
breadcrumbItems.push(`<span class="breadcrumb-separator"> / </span>`);
|
||||||
|
breadcrumbItems.push(`<span class="breadcrumb-link" data-folder="${cumulative}">${escapeHTML(part)}</span>`);
|
||||||
|
});
|
||||||
|
return breadcrumbItems.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind click and drag-and-drop events to breadcrumb links.
|
||||||
|
function bindBreadcrumbEvents() {
|
||||||
|
const breadcrumbLinks = document.querySelectorAll(".breadcrumb-link");
|
||||||
|
breadcrumbLinks.forEach(link => {
|
||||||
|
// Click event for navigation.
|
||||||
|
link.addEventListener("click", function (e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
let folder = this.getAttribute("data-folder");
|
||||||
|
console.log("Breadcrumb clicked, folder:", folder);
|
||||||
|
window.currentFolder = folder;
|
||||||
|
localStorage.setItem("lastOpenedFolder", folder);
|
||||||
|
const titleEl = document.getElementById("fileListTitle");
|
||||||
|
if (folder === "root") {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb("root") + ")";
|
||||||
|
} else {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb(folder) + ")";
|
||||||
|
}
|
||||||
|
// Expand the folder tree to ensure the target is visible.
|
||||||
|
expandTreePath(folder);
|
||||||
|
// Update folder tree selection.
|
||||||
|
document.querySelectorAll(".folder-option").forEach(item => item.classList.remove("selected"));
|
||||||
|
const targetOption = document.querySelector(`.folder-option[data-folder="${folder}"]`);
|
||||||
|
if (targetOption) {
|
||||||
|
targetOption.classList.add("selected");
|
||||||
|
}
|
||||||
|
// Load the file list.
|
||||||
|
loadFileList(folder);
|
||||||
|
// Re-bind breadcrumb events to ensure all links remain active.
|
||||||
|
bindBreadcrumbEvents();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Drag-and-drop events.
|
||||||
|
link.addEventListener("dragover", function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
this.classList.add("drop-hover");
|
||||||
|
});
|
||||||
|
link.addEventListener("dragleave", function (e) {
|
||||||
|
this.classList.remove("drop-hover");
|
||||||
|
});
|
||||||
|
link.addEventListener("drop", function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
this.classList.remove("drop-hover");
|
||||||
|
const dropFolder = this.getAttribute("data-folder");
|
||||||
|
let dragData;
|
||||||
|
try {
|
||||||
|
dragData = JSON.parse(e.dataTransfer.getData("application/json"));
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Invalid drag data on breadcrumb:", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const filesToMove = dragData.files ? dragData.files : (dragData.fileName ? [dragData.fileName] : []);
|
||||||
|
if (filesToMove.length === 0) return;
|
||||||
|
fetch("moveFiles.php", {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "include",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').getAttribute("content")
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
source: dragData.sourceFolder,
|
||||||
|
files: filesToMove,
|
||||||
|
destination: dropFolder
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
showToast(`File(s) moved successfully to ${dropFolder}!`);
|
||||||
|
loadFileList(dragData.sourceFolder);
|
||||||
|
} else {
|
||||||
|
showToast("Error moving files: " + (data.error || "Unknown error"));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error moving files via drop on breadcrumb:", error);
|
||||||
|
showToast("Error moving files.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------
|
||||||
|
// DOM Building Functions for Folder Tree
|
||||||
// ----------------------
|
// ----------------------
|
||||||
|
|
||||||
// Recursively builds HTML for the folder tree as nested <ul> elements.
|
// Recursively builds HTML for the folder tree as nested <ul> elements.
|
||||||
@@ -72,18 +177,16 @@ function renderFolderTree(tree, parentPath = "", defaultDisplay = "block") {
|
|||||||
if (folder.toLowerCase() === "trash") {
|
if (folder.toLowerCase() === "trash") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fullPath = parentPath ? parentPath + "/" + folder : folder;
|
const fullPath = parentPath ? parentPath + "/" + folder : folder;
|
||||||
const hasChildren = Object.keys(tree[folder]).length > 0;
|
const hasChildren = Object.keys(tree[folder]).length > 0;
|
||||||
const displayState = state[fullPath] !== undefined ? state[fullPath] : defaultDisplay;
|
const displayState = state[fullPath] !== undefined ? state[fullPath] : defaultDisplay;
|
||||||
html += `<li class="folder-item">`;
|
html += `<li class="folder-item">`;
|
||||||
if (hasChildren) {
|
if (hasChildren) {
|
||||||
const toggleSymbol = (displayState === "none") ? "[+]" : "[-]";
|
const toggleSymbol = (displayState === 'none') ? '[+]' : '[' + '<span class="custom-dash">-</span>' + ']';
|
||||||
html += `<span class="folder-toggle" data-folder="${fullPath}">${toggleSymbol}</span>`;
|
html += `<span class="folder-toggle" data-folder="${fullPath}">${toggleSymbol}</span>`;
|
||||||
} else {
|
} else {
|
||||||
html += `<span class="folder-indent-placeholder"></span>`;
|
html += `<span class="folder-indent-placeholder"></span>`;
|
||||||
}
|
}
|
||||||
// Use escapeHTML to safely render the folder name.
|
|
||||||
html += `<span class="folder-option" data-folder="${fullPath}">${escapeHTML(folder)}</span>`;
|
html += `<span class="folder-option" data-folder="${fullPath}">${escapeHTML(folder)}</span>`;
|
||||||
if (hasChildren) {
|
if (hasChildren) {
|
||||||
html += renderFolderTree(tree[folder], fullPath, displayState);
|
html += renderFolderTree(tree[folder], fullPath, displayState);
|
||||||
@@ -94,7 +197,7 @@ function renderFolderTree(tree, parentPath = "", defaultDisplay = "block") {
|
|||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expands the folder tree along a given path.
|
// Expands the folder tree along a given normalized path.
|
||||||
function expandTreePath(path) {
|
function expandTreePath(path) {
|
||||||
const parts = path.split("/");
|
const parts = path.split("/");
|
||||||
let cumulative = "";
|
let cumulative = "";
|
||||||
@@ -109,7 +212,7 @@ function expandTreePath(path) {
|
|||||||
nestedUl.classList.add("expanded");
|
nestedUl.classList.add("expanded");
|
||||||
const toggle = li.querySelector(".folder-toggle");
|
const toggle = li.querySelector(".folder-toggle");
|
||||||
if (toggle) {
|
if (toggle) {
|
||||||
toggle.textContent = "[-]";
|
toggle.innerHTML = "[" + '<span class="custom-dash">-</span>' + "]";
|
||||||
let state = loadFolderTreeState();
|
let state = loadFolderTreeState();
|
||||||
state[cumulative] = "block";
|
state[cumulative] = "block";
|
||||||
saveFolderTreeState(state);
|
saveFolderTreeState(state);
|
||||||
@@ -122,19 +225,15 @@ function expandTreePath(path) {
|
|||||||
// ----------------------
|
// ----------------------
|
||||||
// Drag & Drop Support for Folder Tree Nodes
|
// Drag & Drop Support for Folder Tree Nodes
|
||||||
// ----------------------
|
// ----------------------
|
||||||
|
|
||||||
// When a draggable file is dragged over a folder node, allow the drop and add a visual cue.
|
|
||||||
function folderDragOverHandler(event) {
|
function folderDragOverHandler(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.currentTarget.classList.add("drop-hover");
|
event.currentTarget.classList.add("drop-hover");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the visual cue when the drag leaves.
|
|
||||||
function folderDragLeaveHandler(event) {
|
function folderDragLeaveHandler(event) {
|
||||||
event.currentTarget.classList.remove("drop-hover");
|
event.currentTarget.classList.remove("drop-hover");
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a file is dropped onto a folder node, send a move request.
|
|
||||||
function folderDropHandler(event) {
|
function folderDropHandler(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.currentTarget.classList.remove("drop-hover");
|
event.currentTarget.classList.remove("drop-hover");
|
||||||
@@ -143,10 +242,9 @@ function folderDropHandler(event) {
|
|||||||
try {
|
try {
|
||||||
dragData = JSON.parse(event.dataTransfer.getData("application/json"));
|
dragData = JSON.parse(event.dataTransfer.getData("application/json"));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Invalid drag data");
|
console.error("Invalid drag data", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Use the files array if present, or fall back to a single file.
|
|
||||||
const filesToMove = dragData.files ? dragData.files : (dragData.fileName ? [dragData.fileName] : []);
|
const filesToMove = dragData.files ? dragData.files : (dragData.fileName ? [dragData.fileName] : []);
|
||||||
if (filesToMove.length === 0) return;
|
if (filesToMove.length === 0) return;
|
||||||
fetch("moveFiles.php", {
|
fetch("moveFiles.php", {
|
||||||
@@ -210,7 +308,7 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let html = `<div id="rootRow" class="root-row">
|
let html = `<div id="rootRow" class="root-row">
|
||||||
<span class="folder-toggle" data-folder="root">[-]</span>
|
<span class="folder-toggle" data-folder="root">[<span class="custom-dash">-</span>]</span>
|
||||||
<span class="folder-option root-folder-option" data-folder="root">(Root)</span>
|
<span class="folder-option root-folder-option" data-folder="root">(Root)</span>
|
||||||
</div>`;
|
</div>`;
|
||||||
if (folders.length === 0) {
|
if (folders.length === 0) {
|
||||||
@@ -232,15 +330,25 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
el.addEventListener("drop", folderDropHandler);
|
el.addEventListener("drop", folderDropHandler);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Determine current folder.
|
// Determine current folder (normalized).
|
||||||
if (selectedFolder) {
|
if (selectedFolder) {
|
||||||
window.currentFolder = selectedFolder;
|
window.currentFolder = selectedFolder;
|
||||||
} else {
|
} else {
|
||||||
window.currentFolder = localStorage.getItem("lastOpenedFolder") || "root";
|
window.currentFolder = localStorage.getItem("lastOpenedFolder") || "root";
|
||||||
}
|
}
|
||||||
localStorage.setItem("lastOpenedFolder", window.currentFolder);
|
localStorage.setItem("lastOpenedFolder", window.currentFolder);
|
||||||
document.getElementById("fileListTitle").textContent =
|
|
||||||
window.currentFolder === "root" ? "Files in (Root)" : "Files in (" + window.currentFolder + ")";
|
// Update file list title using breadcrumb.
|
||||||
|
const titleEl = document.getElementById("fileListTitle");
|
||||||
|
if (window.currentFolder === "root") {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb("root") + ")";
|
||||||
|
} else {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb(window.currentFolder) + ")";
|
||||||
|
}
|
||||||
|
// Bind breadcrumb events (click and drag/drop).
|
||||||
|
bindBreadcrumbEvents();
|
||||||
|
|
||||||
|
// Load file list.
|
||||||
loadFileList(window.currentFolder);
|
loadFileList(window.currentFolder);
|
||||||
|
|
||||||
// Expand tree to current folder.
|
// Expand tree to current folder.
|
||||||
@@ -249,13 +357,14 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
expandTreePath(window.currentFolder);
|
expandTreePath(window.currentFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Highlight current folder.
|
// Highlight current folder in folder tree.
|
||||||
const selectedEl = container.querySelector(`.folder-option[data-folder="${window.currentFolder}"]`);
|
const selectedEl = container.querySelector(`.folder-option[data-folder="${window.currentFolder}"]`);
|
||||||
if (selectedEl) {
|
if (selectedEl) {
|
||||||
|
container.querySelectorAll(".folder-option").forEach(item => item.classList.remove("selected"));
|
||||||
selectedEl.classList.add("selected");
|
selectedEl.classList.add("selected");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event binding for folder selection.
|
// Event binding for folder selection in folder tree.
|
||||||
container.querySelectorAll(".folder-option").forEach(el => {
|
container.querySelectorAll(".folder-option").forEach(el => {
|
||||||
el.addEventListener("click", function (e) {
|
el.addEventListener("click", function (e) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
@@ -264,8 +373,14 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
const selected = this.getAttribute("data-folder");
|
const selected = this.getAttribute("data-folder");
|
||||||
window.currentFolder = selected;
|
window.currentFolder = selected;
|
||||||
localStorage.setItem("lastOpenedFolder", selected);
|
localStorage.setItem("lastOpenedFolder", selected);
|
||||||
document.getElementById("fileListTitle").textContent =
|
const titleEl = document.getElementById("fileListTitle");
|
||||||
selected === "root" ? "Files in (Root)" : "Files in (" + selected + ")";
|
if (selected === "root") {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb("root") + ")";
|
||||||
|
} else {
|
||||||
|
titleEl.innerHTML = "Files in (" + renderBreadcrumb(selected) + ")";
|
||||||
|
}
|
||||||
|
// Re-bind breadcrumb events so the new breadcrumb is clickable.
|
||||||
|
bindBreadcrumbEvents();
|
||||||
loadFileList(selected);
|
loadFileList(selected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -281,7 +396,7 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
if (nestedUl.classList.contains("collapsed") || !nestedUl.classList.contains("expanded")) {
|
if (nestedUl.classList.contains("collapsed") || !nestedUl.classList.contains("expanded")) {
|
||||||
nestedUl.classList.remove("collapsed");
|
nestedUl.classList.remove("collapsed");
|
||||||
nestedUl.classList.add("expanded");
|
nestedUl.classList.add("expanded");
|
||||||
this.textContent = "[-]";
|
this.innerHTML = "[" + '<span class="custom-dash">-</span>' + "]";
|
||||||
state["root"] = "block";
|
state["root"] = "block";
|
||||||
} else {
|
} else {
|
||||||
nestedUl.classList.remove("expanded");
|
nestedUl.classList.remove("expanded");
|
||||||
@@ -304,7 +419,7 @@ export async function loadFolderTree(selectedFolder) {
|
|||||||
if (siblingUl.classList.contains("collapsed") || !siblingUl.classList.contains("expanded")) {
|
if (siblingUl.classList.contains("collapsed") || !siblingUl.classList.contains("expanded")) {
|
||||||
siblingUl.classList.remove("collapsed");
|
siblingUl.classList.remove("collapsed");
|
||||||
siblingUl.classList.add("expanded");
|
siblingUl.classList.add("expanded");
|
||||||
this.textContent = "[-]";
|
this.innerHTML = "[" + '<span class="custom-dash">-</span>' + "]";
|
||||||
state[folderPath] = "block";
|
state[folderPath] = "block";
|
||||||
} else {
|
} else {
|
||||||
siblingUl.classList.remove("expanded");
|
siblingUl.classList.remove("expanded");
|
||||||
|
|||||||
41
index.html
41
index.html
@@ -20,6 +20,7 @@
|
|||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/xml/xml.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/xml/xml.min.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/css/css.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/css/css.min.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/javascript/javascript.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/javascript/javascript.min.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/resumable.js/1.1.0/resumable.min.js"></script>
|
||||||
<link rel="stylesheet" href="styles.css" />
|
<link rel="stylesheet" href="styles.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
@@ -91,6 +92,9 @@
|
|||||||
<button id="logoutBtn" title="Logout">
|
<button id="logoutBtn" title="Logout">
|
||||||
<i class="material-icons">exit_to_app</i>
|
<i class="material-icons">exit_to_app</i>
|
||||||
</button>
|
</button>
|
||||||
|
<button id="changePasswordBtn" title="Change Password">
|
||||||
|
<i class="material-icons">vpn_key</i>
|
||||||
|
</button>
|
||||||
<!-- Restore Files Modal (Admin Only) -->
|
<!-- Restore Files Modal (Admin Only) -->
|
||||||
<div id="restoreFilesModal" class="modal centered-modal" style="display: none;">
|
<div id="restoreFilesModal" class="modal centered-modal" style="display: none;">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
@@ -140,6 +144,10 @@
|
|||||||
<input type="password" class="form-control" id="loginPassword" name="password" required />
|
<input type="password" class="form-control" id="loginPassword" name="password" required />
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary btn-block btn-login">Login</button>
|
<button type="submit" class="btn btn-primary btn-block btn-login">Login</button>
|
||||||
|
<div class="form-group remember-me-container">
|
||||||
|
<input type="checkbox" id="rememberMeCheckbox" name="remember_me" />
|
||||||
|
<label for="rememberMeCheckbox">Remember me</label>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -154,15 +162,16 @@
|
|||||||
<div class="card-header">Upload Files/Folders</div>
|
<div class="card-header">Upload Files/Folders</div>
|
||||||
<div class="card-body d-flex flex-column">
|
<div class="card-body d-flex flex-column">
|
||||||
<form id="uploadFileForm" method="post" enctype="multipart/form-data" class="d-flex flex-column"
|
<form id="uploadFileForm" method="post" enctype="multipart/form-data" class="d-flex flex-column"
|
||||||
style="height: 100%;">
|
style="height: 100%;" novalidate>
|
||||||
<div class="form-group flex-grow-1" style="margin-bottom: 1rem;">
|
<div class="form-group flex-grow-1" style="margin-bottom: 1rem;">
|
||||||
<div id="uploadDropArea"
|
<div id="uploadDropArea"
|
||||||
style="border:2px dashed #ccc; padding:20px; cursor:pointer; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center;">
|
style="border:2px dashed #ccc; padding:20px; cursor:pointer; height:100%; display:flex; flex-direction:column; justify-content:center; align-items:center; position:relative;">
|
||||||
<span>Drop files/folders here or click 'Choose files'</span>
|
<span>Drop files/folders here or click 'Choose Files'</span>
|
||||||
<br />
|
<br />
|
||||||
<input type="file" id="file" name="file[]" class="form-control-file" multiple required
|
<!-- Note: Remove directory attributes so file picker only allows files -->
|
||||||
webkitdirectory directory mozdirectory style="opacity:0; position:absolute; z-index:-1;" />
|
<input type="file" id="file" name="file[]" class="form-control-file" multiple
|
||||||
<button type="button" onclick="document.getElementById('file').click();">Choose Folder</button>
|
style="opacity:0; position:absolute; width:1px; height:1px;" />
|
||||||
|
<button type="button" id="customChooseBtn">Choose Files</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" id="uploadBtn" class="btn btn-primary d-block mx-auto">Upload</button>
|
<button type="submit" id="uploadBtn" class="btn btn-primary d-block mx-auto">Upload</button>
|
||||||
@@ -174,7 +183,8 @@
|
|||||||
|
|
||||||
<!-- Folder Management Card -->
|
<!-- Folder Management Card -->
|
||||||
<div class="col-md-6 col-lg-5 d-flex">
|
<div class="col-md-6 col-lg-5 d-flex">
|
||||||
<div id="folderManagementCard" class="card flex-fill" style="max-width: 900px; width: 100%; position: relative;">
|
<div id="folderManagementCard" class="card flex-fill"
|
||||||
|
style="max-width: 900px; width: 100%; position: relative;">
|
||||||
<!-- Card header with folder management title and help icon -->
|
<!-- Card header with folder management title and help icon -->
|
||||||
<div class="card-header" style="display: flex; justify-content: space-between; align-items: center;">
|
<div class="card-header" style="display: flex; justify-content: space-between; align-items: center;">
|
||||||
<span>Folder Navigation & Management</span>
|
<span>Folder Navigation & Management</span>
|
||||||
@@ -311,14 +321,27 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Change Password-->
|
||||||
|
<div id="changePasswordModal" class="modal" style="display:none;">
|
||||||
|
<div class="modal-content" style="max-width:400px; margin:auto;">
|
||||||
|
<span id="closeChangePasswordModal" style="cursor:pointer;">×</span>
|
||||||
|
<h3>Change Password</h3>
|
||||||
|
<input type="password" id="oldPassword" placeholder="Old Password" style="width:100%; margin: 5px 0;" />
|
||||||
|
<input type="password" id="newPassword" placeholder="New Password" style="width:100%; margin: 5px 0;" />
|
||||||
|
<input type="password" id="confirmPassword" placeholder="Confirm New Password"
|
||||||
|
style="width:100%; margin: 5px 0;" />
|
||||||
|
<button id="saveNewPasswordBtn" class="btn btn-primary" style="width:100%;">Save</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Add User Modal -->
|
<!-- Add User Modal -->
|
||||||
<div id="addUserModal" class="modal">
|
<div id="addUserModal" class="modal">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<h3>Create New User</h3>
|
<h3>Create New User</h3>
|
||||||
<label for="newUsername">Username:</label>
|
<label for="newUsername">Username:</label>
|
||||||
<input type="text" id="newUsername" class="form-control" />
|
<input type="text" id="newUsername" class="form-control" />
|
||||||
<label for="newPassword">Password:</label>
|
<label for="addUserPassword">Password:</label>
|
||||||
<input type="password" id="newPassword" class="form-control" />
|
<input type="password" id="addUserPassword" class="form-control" />
|
||||||
<div id="adminCheckboxContainer">
|
<div id="adminCheckboxContainer">
|
||||||
<input type="checkbox" id="isAdmin" />
|
<input type="checkbox" id="isAdmin" />
|
||||||
<label for="isAdmin">Grant Admin Access</label>
|
<label for="isAdmin">Grant Admin Access</label>
|
||||||
|
|||||||
63
removeChunks.php
Normal file
63
removeChunks.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'config.php';
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
// Validate CSRF token from POST
|
||||||
|
$receivedToken = isset($_POST['csrf_token']) ? trim($_POST['csrf_token']) : '';
|
||||||
|
if ($receivedToken !== $_SESSION['csrf_token']) {
|
||||||
|
echo json_encode(["error" => "Invalid CSRF token"]);
|
||||||
|
http_response_code(403);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure a folder parameter is provided
|
||||||
|
if (!isset($_POST['folder'])) {
|
||||||
|
echo json_encode(["error" => "No folder specified"]);
|
||||||
|
http_response_code(400);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$folder = $_POST['folder'];
|
||||||
|
// Validate the folder name to allow only expected characters (adjust the regex as needed)
|
||||||
|
if (!preg_match('/^resumable_[A-Za-z0-9\-]+$/', $folder)) {
|
||||||
|
echo json_encode(["error" => "Invalid folder name"]);
|
||||||
|
http_response_code(400);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tempDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder;
|
||||||
|
|
||||||
|
// If the folder doesn't exist, simply return success.
|
||||||
|
if (!is_dir($tempDir)) {
|
||||||
|
echo json_encode(["success" => true, "message" => "Temporary folder already removed."]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively delete directory and its contents
|
||||||
|
function rrmdir($dir) {
|
||||||
|
if (is_dir($dir)) {
|
||||||
|
$objects = scandir($dir);
|
||||||
|
foreach ($objects as $object) {
|
||||||
|
if ($object != "." && $object != "..") {
|
||||||
|
$path = $dir . DIRECTORY_SEPARATOR . $object;
|
||||||
|
if (is_dir($path) && !is_link($path)) {
|
||||||
|
rrmdir($path);
|
||||||
|
} else {
|
||||||
|
@unlink($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@rmdir($dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rrmdir($tempDir);
|
||||||
|
|
||||||
|
// check if folder still exists
|
||||||
|
if (!is_dir($tempDir)) {
|
||||||
|
echo json_encode(["success" => true, "message" => "Temporary folder removed."]);
|
||||||
|
} else {
|
||||||
|
echo json_encode(["error" => "Failed to remove temporary folder."]);
|
||||||
|
http_response_code(500);
|
||||||
|
}
|
||||||
|
?>
|
||||||
160
styles.css
160
styles.css
@@ -12,20 +12,24 @@ body {
|
|||||||
|
|
||||||
body {
|
body {
|
||||||
letter-spacing: 0.2px;
|
letter-spacing: 0.2px;
|
||||||
|
overflow-x: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.custom-dash {
|
||||||
|
display: inline-block;
|
||||||
|
transform: scaleX(1.5);
|
||||||
|
padding-left: 2px !important;
|
||||||
|
padding-right: 2px !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* CONTAINER */
|
/* CONTAINER */
|
||||||
.container {
|
.container,
|
||||||
margin-top: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container-fluid {
|
.container-fluid {
|
||||||
padding-left: 5px !important;
|
|
||||||
padding-right: 5px !important;
|
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
|
padding-right: 4px !important;
|
||||||
|
padding-left: 4px !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Increase left/right padding for larger screens */
|
|
||||||
@media (min-width: 768px) {
|
@media (min-width: 768px) {
|
||||||
.container-fluid {
|
.container-fluid {
|
||||||
padding-left: 50px !important;
|
padding-left: 50px !important;
|
||||||
@@ -47,7 +51,10 @@ body {
|
|||||||
/************************************************************/
|
/************************************************************/
|
||||||
/* FLEXBOX HEADER: LOGO, TITLE, BUTTONS FIXED */
|
/* FLEXBOX HEADER: LOGO, TITLE, BUTTONS FIXED */
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
#uploadCard, #folderManagementCard {
|
|
||||||
|
|
||||||
|
#uploadCard,
|
||||||
|
#folderManagementCard {
|
||||||
min-height: 342px;
|
min-height: 342px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,14 +149,14 @@ body.dark-mode header {
|
|||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
min-width: 150px;
|
min-width: 150px;
|
||||||
gap: 10px;
|
gap: 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.header-buttons button {
|
.header-buttons button {
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
padding: 10px;
|
padding: 9px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
||||||
@@ -209,7 +216,6 @@ body.dark-mode header {
|
|||||||
background-color: rgba(255, 255, 255, 0.2);
|
background-color: rgba(255, 255, 255, 0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Folder Help Tooltip - Light Mode */
|
|
||||||
.folder-help-tooltip {
|
.folder-help-tooltip {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
color: #333;
|
color: #333;
|
||||||
@@ -219,7 +225,6 @@ body.dark-mode header {
|
|||||||
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
|
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Folder Help Tooltip - Dark Mode */
|
|
||||||
body.dark-mode .folder-help-tooltip {
|
body.dark-mode .folder-help-tooltip {
|
||||||
background-color: #333 !important;
|
background-color: #333 !important;
|
||||||
color: #eee !important;
|
color: #eee !important;
|
||||||
@@ -292,12 +297,14 @@ body.dark-mode #folderHelpBtn i.material-icons.folder-help-icon {
|
|||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
.material-icons.folder-icon {
|
.material-icons.folder-icon,
|
||||||
|
.material-icons.gallery-icon {
|
||||||
color: black;
|
color: black;
|
||||||
margin-right: 5px;
|
margin-right: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
body.dark-mode .material-icons.folder-icon {
|
body.dark-mode .material-icons.folder-icon,
|
||||||
|
body.dark-mode .material-icons.gallery-icon {
|
||||||
color: white;
|
color: white;
|
||||||
margin-right: 5px;
|
margin-right: 5px;
|
||||||
}
|
}
|
||||||
@@ -311,7 +318,7 @@ body.dark-mode .material-icons.folder-icon {
|
|||||||
border: none;
|
border: none;
|
||||||
color: red;
|
color: red;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
margin-right: 8px;
|
margin-right: 0px;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
transition: background-color 0.3s;
|
transition: background-color 0.3s;
|
||||||
@@ -325,6 +332,10 @@ body.dark-mode .material-icons.folder-icon {
|
|||||||
/* ===========================================================
|
/* ===========================================================
|
||||||
FORMS & LOGIN
|
FORMS & LOGIN
|
||||||
=========================================================== */
|
=========================================================== */
|
||||||
|
.remember-me-container {
|
||||||
|
margin-top: 20px !important;
|
||||||
|
}
|
||||||
|
|
||||||
#loginForm {
|
#loginForm {
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
max-width: 400px;
|
max-width: 400px;
|
||||||
@@ -521,17 +532,6 @@ body.dark-mode .modal .modal-content {
|
|||||||
border-color: #444;
|
border-color: #444;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-header {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
padding: 5px 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
body.dark-mode .editor-header {
|
|
||||||
background-color: #2c2c2c;
|
|
||||||
}
|
|
||||||
|
|
||||||
.editor-close-btn {
|
.editor-close-btn {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 10px;
|
top: 10px;
|
||||||
@@ -574,12 +574,12 @@ body.dark-mode .editor-close-btn:hover {
|
|||||||
/* Editor Modal */
|
/* Editor Modal */
|
||||||
.editor-modal {
|
.editor-modal {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 5%;
|
top: 2%;
|
||||||
left: 5%;
|
left: 5%;
|
||||||
width: 90vw;
|
width: 90vw;
|
||||||
height: 90vh;
|
height: 90vh;
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
padding: 20px;
|
padding: 10px 20px 20px 20px;
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
border-radius: 4px !important;
|
border-radius: 4px !important;
|
||||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2) !important;
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2) !important;
|
||||||
@@ -616,15 +616,25 @@ body.dark-mode .editor-modal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-title {
|
.editor-header {
|
||||||
white-space: nowrap !important;
|
display: flex;
|
||||||
overflow: hidden !important;
|
align-items: center;
|
||||||
text-overflow: ellipsis !important;
|
justify-content: space-between;
|
||||||
font-size: 1.5rem;
|
height: 33px;
|
||||||
max-width: 95%;
|
padding: 0 10px;
|
||||||
display: block;
|
margin-bottom: 7px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.editor-title {
|
||||||
|
margin: 0;
|
||||||
|
line-height: 33px;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.dark-mode .editor-header {
|
||||||
|
background-color: #2c2c2c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@media (max-width: 600px) {
|
@media (max-width: 600px) {
|
||||||
.editor-title {
|
.editor-title {
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
@@ -634,6 +644,7 @@ body.dark-mode .editor-modal {
|
|||||||
|
|
||||||
.editor-controls {
|
.editor-controls {
|
||||||
text-align: right;
|
text-align: right;
|
||||||
|
margin-right: 30px;
|
||||||
margin-bottom: 5px;
|
margin-bottom: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -690,6 +701,24 @@ body.dark-mode .editor-modal {
|
|||||||
/* ===========================================================
|
/* ===========================================================
|
||||||
UPLOAD PROGRESS STYLES
|
UPLOAD PROGRESS STYLES
|
||||||
=========================================================== */
|
=========================================================== */
|
||||||
|
.pause-resume-btn {
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
cursor: pointer;
|
||||||
|
outline: none;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.material-icons.pauseResumeBtn {
|
||||||
|
color: black !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.dark-mode .material-icons.pauseResumeBtn {
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
#uploadProgressContainer ul {
|
#uploadProgressContainer ul {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
@@ -899,6 +928,7 @@ body.dark-mode #fileList table tr {
|
|||||||
word-break: break-word !important;
|
word-break: break-word !important;
|
||||||
text-align: left !important;
|
text-align: left !important;
|
||||||
line-height: 1.2 !important;
|
line-height: 1.2 !important;
|
||||||
|
vertical-align: middle !important;
|
||||||
padding: 8px 10px !important;
|
padding: 8px 10px !important;
|
||||||
max-width: 250px !important;
|
max-width: 250px !important;
|
||||||
min-width: 120px !important;
|
min-width: 120px !important;
|
||||||
@@ -1129,9 +1159,15 @@ body.dark-mode .folder-option:hover {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#fileListContainer {
|
#fileListContainer {
|
||||||
padding: 10px;
|
max-width: 100%;
|
||||||
margin-top: 20px;
|
padding: 10px 5px;
|
||||||
margin-bottom: 20px;
|
margin: 20px auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 750px) {
|
||||||
|
#fileListContainer {
|
||||||
|
width: 99%;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body.dark-mode #fileListContainer {
|
body.dark-mode #fileListContainer {
|
||||||
@@ -1139,9 +1175,11 @@ body.dark-mode #fileListContainer {
|
|||||||
color: #e0e0e0;
|
color: #e0e0e0;
|
||||||
border: 1px solid #444;
|
border: 1px solid #444;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
padding: 10px;
|
padding-top: 10px !important;
|
||||||
|
padding-bottom: 10px !important;
|
||||||
|
padding-left: 5px !important;
|
||||||
|
padding-right: 5px !important;
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#fileListContainer>h2,
|
#fileListContainer>h2,
|
||||||
@@ -1160,7 +1198,7 @@ body.dark-mode #fileListContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.col-12.col-md-4.text-left {
|
.col-12.col-md-4.text-left {
|
||||||
margin-left: -15px;
|
margin-left: -17px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 600px) {
|
@media (max-width: 600px) {
|
||||||
@@ -1231,6 +1269,23 @@ body.dark-mode #fileListContainer {
|
|||||||
/* ===========================================================
|
/* ===========================================================
|
||||||
FOLDER TREE STYLES
|
FOLDER TREE STYLES
|
||||||
=========================================================== */
|
=========================================================== */
|
||||||
|
.breadcrumb-link {
|
||||||
|
cursor: pointer;
|
||||||
|
color: #007bff;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.breadcrumb-link:hover {
|
||||||
|
color: #0056b3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.breadcrumb-link.selected {
|
||||||
|
background-color: #e9ecef;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 2px 4px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
.folder-tree {
|
.folder-tree {
|
||||||
list-style-type: none;
|
list-style-type: none;
|
||||||
padding-left: 10px;
|
padding-left: 10px;
|
||||||
@@ -1280,12 +1335,13 @@ body.dark-mode #fileListContainer {
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
flex-wrap: nowrap;
|
white-space: nowrap;
|
||||||
text-align: center;
|
overflow: hidden;
|
||||||
min-height: 30px;
|
text-overflow: ellipsis;
|
||||||
margin: 0 auto 10px;
|
height: 25px;
|
||||||
padding: 10px;
|
padding: 5px;
|
||||||
width: 90% !important;
|
margin-bottom: 10px;
|
||||||
|
max-width: 90%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.image-preview-modal-content {
|
.image-preview-modal-content {
|
||||||
@@ -1567,7 +1623,7 @@ body.dark-mode .btn-secondary {
|
|||||||
|
|
||||||
#toggleViewBtn {
|
#toggleViewBtn {
|
||||||
margin-bottom: 20px;
|
margin-bottom: 20px;
|
||||||
margin-left: 15px;
|
margin-left: 14px;
|
||||||
padding: 10px 20px;
|
padding: 10px 20px;
|
||||||
background: rgba(0, 0, 0, 0.6);
|
background: rgba(0, 0, 0, 0.6);
|
||||||
color: #fff;
|
color: #fff;
|
||||||
@@ -1579,9 +1635,15 @@ body.dark-mode .btn-secondary {
|
|||||||
transition: background 0.3s ease, box-shadow 0.3s ease;
|
transition: background 0.3s ease, box-shadow 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
#toggleViewBtn {
|
||||||
|
margin-left: 0 !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@media (max-width: 600px) {
|
@media (max-width: 600px) {
|
||||||
#toggleViewBtn {
|
#toggleViewBtn {
|
||||||
margin-left: auto !important;
|
margin-left: 0 !important;
|
||||||
margin-right: auto !important;
|
margin-right: auto !important;
|
||||||
display: block !important;
|
display: block !important;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ function showConfirm(message, onConfirm) {
|
|||||||
* This function should be called from main.js after authentication.
|
* This function should be called from main.js after authentication.
|
||||||
*/
|
*/
|
||||||
export function setupTrashRestoreDelete() {
|
export function setupTrashRestoreDelete() {
|
||||||
console.log("Setting up trash restore/delete listeners.");
|
|
||||||
|
|
||||||
// --- Attach listener to the restore button (created in auth.js) to open the modal.
|
// --- Attach listener to the restore button (created in auth.js) to open the modal.
|
||||||
const restoreBtn = document.getElementById("restoreFilesBtn");
|
const restoreBtn = document.getElementById("restoreFilesBtn");
|
||||||
@@ -57,7 +56,6 @@ export function setupTrashRestoreDelete() {
|
|||||||
loadTrashItems();
|
loadTrashItems();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.warn("restoreFilesBtn not found. It may not be available for the current user.");
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const retryBtn = document.getElementById("restoreFilesBtn");
|
const retryBtn = document.getElementById("restoreFilesBtn");
|
||||||
if (retryBtn) {
|
if (retryBtn) {
|
||||||
|
|||||||
472
upload.js
472
upload.js
@@ -2,11 +2,15 @@ import { loadFileList, displayFilePreview, initFileActions } from './fileManager
|
|||||||
import { showToast, escapeHTML } from './domUtils.js';
|
import { showToast, escapeHTML } from './domUtils.js';
|
||||||
import { loadFolderTree } from './folderManager.js';
|
import { loadFolderTree } from './folderManager.js';
|
||||||
|
|
||||||
// Helper: Recursively traverse a dropped folder.
|
/* -----------------------------------------------------
|
||||||
|
Helpers for Drag–and–Drop Folder Uploads (Original Code)
|
||||||
|
----------------------------------------------------- */
|
||||||
|
// Recursively traverse a dropped folder.
|
||||||
function traverseFileTreePromise(item, path = "") {
|
function traverseFileTreePromise(item, path = "") {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
if (item.isFile) {
|
if (item.isFile) {
|
||||||
item.file(file => {
|
item.file(file => {
|
||||||
|
// Store relative path for folder uploads.
|
||||||
Object.defineProperty(file, 'customRelativePath', {
|
Object.defineProperty(file, 'customRelativePath', {
|
||||||
value: path + file.name,
|
value: path + file.name,
|
||||||
writable: true,
|
writable: true,
|
||||||
@@ -29,7 +33,7 @@ function traverseFileTreePromise(item, path = "") {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Given DataTransfer items, recursively retrieve files.
|
// Recursively retrieve files from DataTransfer items.
|
||||||
function getFilesFromDataTransferItems(items) {
|
function getFilesFromDataTransferItems(items) {
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let i = 0; i < items.length; i++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
@@ -41,25 +45,27 @@ function getFilesFromDataTransferItems(items) {
|
|||||||
return Promise.all(promises).then(results => results.flat());
|
return Promise.all(promises).then(results => results.flat());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Set default drop area content.
|
/* -----------------------------------------------------
|
||||||
|
UI Helpers (Mostly unchanged from your original code)
|
||||||
|
----------------------------------------------------- */
|
||||||
function setDropAreaDefault() {
|
function setDropAreaDefault() {
|
||||||
const dropArea = document.getElementById("uploadDropArea");
|
const dropArea = document.getElementById("uploadDropArea");
|
||||||
if (dropArea) {
|
if (dropArea) {
|
||||||
dropArea.innerHTML = `
|
dropArea.innerHTML = `
|
||||||
<div id="uploadInstruction" class="upload-instruction">
|
<div id="uploadInstruction" class="upload-instruction">
|
||||||
Drop files/folders here or click 'Choose files'
|
Drop files/folders here or click 'Choose files'
|
||||||
</div>
|
</div>
|
||||||
<div id="uploadFileRow" class="upload-file-row">
|
<div id="uploadFileRow" class="upload-file-row">
|
||||||
<button id="customChooseBtn" type="button">
|
<button id="customChooseBtn" type="button">Choose files</button>
|
||||||
Choose files
|
</div>
|
||||||
</button>
|
<div id="fileInfoWrapper" class="file-info-wrapper">
|
||||||
</div>
|
<div id="fileInfoContainer" class="file-info-container">
|
||||||
<div id="fileInfoWrapper" class="file-info-wrapper">
|
<span id="fileInfoDefault">No files selected</span>
|
||||||
<div id="fileInfoContainer" class="file-info-container">
|
</div>
|
||||||
<span id="fileInfoDefault">No files selected</span>
|
</div>
|
||||||
</div>
|
<!-- File input for file picker (files only) -->
|
||||||
</div>
|
<input type="file" id="file" name="file[]" class="form-control-file" multiple style="opacity:0; position:absolute; width:1px; height:1px;" />
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +88,6 @@ function adjustFolderHelpExpansionClosed() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Update file info container count/preview.
|
|
||||||
function updateFileInfoCount() {
|
function updateFileInfoCount() {
|
||||||
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
||||||
if (fileInfoContainer && window.selectedFiles) {
|
if (fileInfoContainer && window.selectedFiles) {
|
||||||
@@ -90,64 +95,180 @@ function updateFileInfoCount() {
|
|||||||
fileInfoContainer.innerHTML = `<span id="fileInfoDefault">No files selected</span>`;
|
fileInfoContainer.innerHTML = `<span id="fileInfoDefault">No files selected</span>`;
|
||||||
} else if (window.selectedFiles.length === 1) {
|
} else if (window.selectedFiles.length === 1) {
|
||||||
fileInfoContainer.innerHTML = `
|
fileInfoContainer.innerHTML = `
|
||||||
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;"></div>
|
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;">
|
||||||
<span id="fileNameDisplay" class="file-name-display">${escapeHTML(window.selectedFiles[0].name)}</span>
|
<span class="material-icons file-icon">insert_drive_file</span>
|
||||||
|
</div>
|
||||||
|
<span id="fileNameDisplay" class="file-name-display">${escapeHTML(window.selectedFiles[0].name || window.selectedFiles[0].fileName || "Unnamed File")}</span>
|
||||||
`;
|
`;
|
||||||
} else {
|
} else {
|
||||||
fileInfoContainer.innerHTML = `
|
fileInfoContainer.innerHTML = `
|
||||||
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;"></div>
|
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;">
|
||||||
|
<span class="material-icons file-icon">insert_drive_file</span>
|
||||||
|
</div>
|
||||||
<span id="fileCountDisplay" class="file-name-display">${window.selectedFiles.length} files selected</span>
|
<span id="fileCountDisplay" class="file-name-display">${window.selectedFiles.length} files selected</span>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
const previewContainer = document.getElementById("filePreviewContainer");
|
const previewContainer = document.getElementById("filePreviewContainer");
|
||||||
if (previewContainer && window.selectedFiles.length > 0) {
|
if (previewContainer && window.selectedFiles.length > 0) {
|
||||||
previewContainer.innerHTML = "";
|
previewContainer.innerHTML = "";
|
||||||
displayFilePreview(window.selectedFiles[0], previewContainer);
|
// For image files, try to show a preview (if available from the file object).
|
||||||
|
displayFilePreview(window.selectedFiles[0].file || window.selectedFiles[0], previewContainer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Create a file entry element with a remove button.
|
// Helper function to repeatedly call removeChunks.php
|
||||||
|
function removeChunkFolderRepeatedly(identifier, csrfToken, maxAttempts = 3, interval = 1000) {
|
||||||
|
let attempt = 0;
|
||||||
|
const removalInterval = setInterval(() => {
|
||||||
|
attempt++;
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
// Prefix with "resumable_" to match your PHP regex.
|
||||||
|
params.append('folder', 'resumable_' + identifier);
|
||||||
|
params.append('csrf_token', csrfToken);
|
||||||
|
fetch('removeChunks.php', {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
body: params.toString()
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
console.log(`Chunk folder removal attempt ${attempt}:`, data);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(`Error on removal attempt ${attempt}:`, err);
|
||||||
|
});
|
||||||
|
if (attempt >= maxAttempts) {
|
||||||
|
clearInterval(removalInterval);
|
||||||
|
}
|
||||||
|
}, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -----------------------------------------------------
|
||||||
|
File Entry Creation (with Pause/Resume and Restart)
|
||||||
|
----------------------------------------------------- */
|
||||||
|
// Create a file entry element with a remove button and a pause/resume button.
|
||||||
function createFileEntry(file) {
|
function createFileEntry(file) {
|
||||||
const li = document.createElement("li");
|
const li = document.createElement("li");
|
||||||
li.classList.add("upload-progress-item");
|
li.classList.add("upload-progress-item");
|
||||||
li.style.display = "flex";
|
li.style.display = "flex";
|
||||||
li.dataset.uploadIndex = file.uploadIndex;
|
li.dataset.uploadIndex = file.uploadIndex;
|
||||||
|
|
||||||
|
// Remove button (always added)
|
||||||
const removeBtn = document.createElement("button");
|
const removeBtn = document.createElement("button");
|
||||||
removeBtn.classList.add("remove-file-btn");
|
removeBtn.classList.add("remove-file-btn");
|
||||||
removeBtn.textContent = "×";
|
removeBtn.textContent = "×";
|
||||||
removeBtn.addEventListener("click", function (e) {
|
// In your remove button event listener, replace the fetch call with:
|
||||||
e.stopPropagation();
|
removeBtn.addEventListener("click", function (e) {
|
||||||
const uploadIndex = file.uploadIndex;
|
e.stopPropagation();
|
||||||
window.selectedFiles = window.selectedFiles.filter(f => f.uploadIndex !== uploadIndex);
|
const uploadIndex = file.uploadIndex;
|
||||||
li.remove();
|
window.selectedFiles = window.selectedFiles.filter(f => f.uploadIndex !== uploadIndex);
|
||||||
updateFileInfoCount();
|
|
||||||
});
|
|
||||||
li.removeBtn = removeBtn;
|
|
||||||
|
|
||||||
|
// Cancel the file upload if possible.
|
||||||
|
if (typeof file.cancel === "function") {
|
||||||
|
file.cancel();
|
||||||
|
console.log("Canceled file upload:", file.fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove file from the resumable queue.
|
||||||
|
if (resumableInstance && typeof resumableInstance.removeFile === "function") {
|
||||||
|
resumableInstance.removeFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call our helper repeatedly to remove the chunk folder.
|
||||||
|
if (file.uniqueIdentifier) {
|
||||||
|
removeChunkFolderRepeatedly(file.uniqueIdentifier, window.csrfToken, 3, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
li.remove();
|
||||||
|
updateFileInfoCount();
|
||||||
|
});
|
||||||
|
li.removeBtn = removeBtn;
|
||||||
|
li.appendChild(removeBtn);
|
||||||
|
|
||||||
|
// Add pause/resume/restart button if the file supports pause/resume.
|
||||||
|
// Conditionally add the pause/resume button only if file.pause is available
|
||||||
|
// Pause/Resume button (for resumable file–picker uploads)
|
||||||
|
if (typeof file.pause === "function") {
|
||||||
|
const pauseResumeBtn = document.createElement("button");
|
||||||
|
pauseResumeBtn.setAttribute("type", "button"); // not a submit button
|
||||||
|
pauseResumeBtn.classList.add("pause-resume-btn");
|
||||||
|
// Start with pause icon and disable button until upload starts
|
||||||
|
pauseResumeBtn.innerHTML = '<span class="material-icons pauseResumeBtn">pause_circle_outline</span>';
|
||||||
|
pauseResumeBtn.disabled = true;
|
||||||
|
pauseResumeBtn.addEventListener("click", function (e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (file.isError) {
|
||||||
|
// If the file previously failed, try restarting upload.
|
||||||
|
if (typeof file.retry === "function") {
|
||||||
|
file.retry();
|
||||||
|
file.isError = false;
|
||||||
|
pauseResumeBtn.innerHTML = '<span class="material-icons pauseResumeBtn">pause_circle_outline</span>';
|
||||||
|
}
|
||||||
|
} else if (!file.paused) {
|
||||||
|
// Pause the upload (if possible)
|
||||||
|
if (typeof file.pause === "function") {
|
||||||
|
file.pause();
|
||||||
|
file.paused = true;
|
||||||
|
pauseResumeBtn.innerHTML = '<span class="material-icons pauseResumeBtn">play_circle_outline</span>';
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else if (file.paused) {
|
||||||
|
// Resume sequence: first call to resume (or upload() fallback)
|
||||||
|
if (typeof file.resume === "function") {
|
||||||
|
file.resume();
|
||||||
|
} else {
|
||||||
|
resumableInstance.upload();
|
||||||
|
}
|
||||||
|
// After a short delay, pause again then resume
|
||||||
|
setTimeout(() => {
|
||||||
|
if (typeof file.pause === "function") {
|
||||||
|
file.pause();
|
||||||
|
} else {
|
||||||
|
resumableInstance.upload();
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
if (typeof file.resume === "function") {
|
||||||
|
file.resume();
|
||||||
|
} else {
|
||||||
|
resumableInstance.upload();
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
}, 100);
|
||||||
|
file.paused = false;
|
||||||
|
pauseResumeBtn.innerHTML = '<span class="material-icons pauseResumeBtn">pause_circle_outline</span>';
|
||||||
|
} else {
|
||||||
|
console.error("Pause/resume function not available for file", file);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
li.appendChild(pauseResumeBtn);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preview element
|
||||||
const preview = document.createElement("div");
|
const preview = document.createElement("div");
|
||||||
preview.className = "file-preview";
|
preview.className = "file-preview";
|
||||||
displayFilePreview(file, preview);
|
displayFilePreview(file, preview);
|
||||||
|
li.appendChild(preview);
|
||||||
|
|
||||||
|
// File name display
|
||||||
const nameDiv = document.createElement("div");
|
const nameDiv = document.createElement("div");
|
||||||
nameDiv.classList.add("upload-file-name");
|
nameDiv.classList.add("upload-file-name");
|
||||||
nameDiv.textContent = file.name;
|
nameDiv.textContent = file.name || file.fileName || "Unnamed File";
|
||||||
|
li.appendChild(nameDiv);
|
||||||
|
|
||||||
|
// Progress bar container
|
||||||
const progDiv = document.createElement("div");
|
const progDiv = document.createElement("div");
|
||||||
progDiv.classList.add("progress", "upload-progress-div");
|
progDiv.classList.add("progress", "upload-progress-div");
|
||||||
progDiv.style.flex = "0 0 250px";
|
progDiv.style.flex = "0 0 250px";
|
||||||
progDiv.style.marginLeft = "5px";
|
progDiv.style.marginLeft = "5px";
|
||||||
|
|
||||||
const progBar = document.createElement("div");
|
const progBar = document.createElement("div");
|
||||||
progBar.classList.add("progress-bar");
|
progBar.classList.add("progress-bar");
|
||||||
progBar.style.width = "0%";
|
progBar.style.width = "0%";
|
||||||
progBar.innerText = "0%";
|
progBar.innerText = "0%";
|
||||||
|
|
||||||
progDiv.appendChild(progBar);
|
progDiv.appendChild(progBar);
|
||||||
li.appendChild(removeBtn);
|
|
||||||
li.appendChild(preview);
|
|
||||||
li.appendChild(nameDiv);
|
|
||||||
li.appendChild(progDiv);
|
li.appendChild(progDiv);
|
||||||
|
|
||||||
li.progressBar = progBar;
|
li.progressBar = progBar;
|
||||||
@@ -155,7 +276,11 @@ function createFileEntry(file) {
|
|||||||
return li;
|
return li;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process selected files: Build preview/progress list and store files for later submission.
|
/* -----------------------------------------------------
|
||||||
|
Processing Files
|
||||||
|
- For drag–and–drop, use original processing (supports folders).
|
||||||
|
- For file picker, if using Resumable, those files use resumable.
|
||||||
|
----------------------------------------------------- */
|
||||||
function processFiles(filesInput) {
|
function processFiles(filesInput) {
|
||||||
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
||||||
const files = Array.from(filesInput);
|
const files = Array.from(filesInput);
|
||||||
@@ -164,12 +289,16 @@ function processFiles(filesInput) {
|
|||||||
if (files.length > 0) {
|
if (files.length > 0) {
|
||||||
if (files.length === 1) {
|
if (files.length === 1) {
|
||||||
fileInfoContainer.innerHTML = `
|
fileInfoContainer.innerHTML = `
|
||||||
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;"></div>
|
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;">
|
||||||
<span id="fileNameDisplay" class="file-name-display">${escapeHTML(files[0].name)}</span>
|
<span class="material-icons file-icon">insert_drive_file</span>
|
||||||
|
</div>
|
||||||
|
<span id="fileNameDisplay" class="file-name-display">${escapeHTML(files[0].name || files[0].fileName || "Unnamed File")}</span>
|
||||||
`;
|
`;
|
||||||
} else {
|
} else {
|
||||||
fileInfoContainer.innerHTML = `
|
fileInfoContainer.innerHTML = `
|
||||||
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;"></div>
|
<div id="filePreviewContainer" class="file-preview-container" style="display:inline-block;">
|
||||||
|
<span class="material-icons file-icon">insert_drive_file</span>
|
||||||
|
</div>
|
||||||
<span id="fileCountDisplay" class="file-name-display">${files.length} files selected</span>
|
<span id="fileCountDisplay" class="file-name-display">${files.length} files selected</span>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
@@ -195,12 +324,14 @@ function processFiles(filesInput) {
|
|||||||
const list = document.createElement("ul");
|
const list = document.createElement("ul");
|
||||||
list.classList.add("upload-progress-list");
|
list.classList.add("upload-progress-list");
|
||||||
|
|
||||||
|
// Check for relative paths (for folder uploads).
|
||||||
const hasRelativePaths = files.some(file => {
|
const hasRelativePaths = files.some(file => {
|
||||||
const rel = file.webkitRelativePath || file.customRelativePath || "";
|
const rel = file.webkitRelativePath || file.customRelativePath || "";
|
||||||
return rel.trim() !== "";
|
return rel.trim() !== "";
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hasRelativePaths) {
|
if (hasRelativePaths) {
|
||||||
|
// Group files by folder.
|
||||||
const fileGroups = {};
|
const fileGroups = {};
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
let folderName = "Root";
|
let folderName = "Root";
|
||||||
@@ -218,11 +349,13 @@ function processFiles(filesInput) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Object.keys(fileGroups).forEach(folderName => {
|
Object.keys(fileGroups).forEach(folderName => {
|
||||||
const folderLi = document.createElement("li");
|
// Only show folder grouping if folderName is not "Root"
|
||||||
folderLi.classList.add("upload-folder-group");
|
if (folderName !== "Root") {
|
||||||
folderLi.innerHTML = `<i class="material-icons folder-icon" style="vertical-align:middle; margin-right:8px;">folder</i> ${folderName}:`;
|
const folderLi = document.createElement("li");
|
||||||
list.appendChild(folderLi);
|
folderLi.classList.add("upload-folder-group");
|
||||||
|
folderLi.innerHTML = `<i class="material-icons folder-icon" style="vertical-align:middle; margin-right:8px;">folder</i> ${folderName}:`;
|
||||||
|
list.appendChild(folderLi);
|
||||||
|
}
|
||||||
const nestedUl = document.createElement("ul");
|
const nestedUl = document.createElement("ul");
|
||||||
nestedUl.classList.add("upload-folder-group-list");
|
nestedUl.classList.add("upload-folder-group-list");
|
||||||
fileGroups[folderName]
|
fileGroups[folderName]
|
||||||
@@ -234,6 +367,7 @@ function processFiles(filesInput) {
|
|||||||
list.appendChild(nestedUl);
|
list.appendChild(nestedUl);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// No relative paths – list files directly.
|
||||||
files.forEach((file, index) => {
|
files.forEach((file, index) => {
|
||||||
const li = createFileEntry(file);
|
const li = createFileEntry(file);
|
||||||
li.style.display = (index < maxDisplay) ? "flex" : "none";
|
li.style.display = (index < maxDisplay) ? "flex" : "none";
|
||||||
@@ -263,7 +397,159 @@ function processFiles(filesInput) {
|
|||||||
updateFileInfoCount();
|
updateFileInfoCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to handle file uploads; triggered when the user clicks the "Upload" button.
|
/* -----------------------------------------------------
|
||||||
|
Resumable.js Integration for File Picker Uploads
|
||||||
|
(Only files chosen via file input use Resumable; folder uploads use original code.)
|
||||||
|
----------------------------------------------------- */
|
||||||
|
const useResumable = true; // Enable resumable for file picker uploads
|
||||||
|
let resumableInstance;
|
||||||
|
function initResumableUpload() {
|
||||||
|
resumableInstance = new Resumable({
|
||||||
|
target: "upload.php",
|
||||||
|
query: { folder: window.currentFolder || "root", upload_token: window.csrfToken },
|
||||||
|
chunkSize: 3 * 1024 * 1024, // 3 MB chunks
|
||||||
|
simultaneousUploads: 3,
|
||||||
|
testChunks: false,
|
||||||
|
throttleProgressCallbacks: 1,
|
||||||
|
headers: { "X-CSRF-Token": window.csrfToken }
|
||||||
|
});
|
||||||
|
|
||||||
|
const fileInput = document.getElementById("file");
|
||||||
|
if (fileInput) {
|
||||||
|
// Assign Resumable to file input for file picker uploads.
|
||||||
|
resumableInstance.assignBrowse(fileInput);
|
||||||
|
fileInput.addEventListener("change", function () {
|
||||||
|
for (let i = 0; i < fileInput.files.length; i++) {
|
||||||
|
resumableInstance.addFile(fileInput.files[i]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
resumableInstance.on("fileAdded", function (file) {
|
||||||
|
// Initialize custom paused flag
|
||||||
|
file.paused = false;
|
||||||
|
file.uploadIndex = file.uniqueIdentifier;
|
||||||
|
if (!window.selectedFiles) {
|
||||||
|
window.selectedFiles = [];
|
||||||
|
}
|
||||||
|
window.selectedFiles.push(file);
|
||||||
|
const progressContainer = document.getElementById("uploadProgressContainer");
|
||||||
|
|
||||||
|
// Check if a wrapper already exists; if not, create one with a UL inside.
|
||||||
|
let listWrapper = progressContainer.querySelector(".upload-progress-wrapper");
|
||||||
|
let list;
|
||||||
|
if (!listWrapper) {
|
||||||
|
listWrapper = document.createElement("div");
|
||||||
|
listWrapper.classList.add("upload-progress-wrapper");
|
||||||
|
listWrapper.style.maxHeight = "300px";
|
||||||
|
listWrapper.style.overflowY = "auto";
|
||||||
|
list = document.createElement("ul");
|
||||||
|
list.classList.add("upload-progress-list");
|
||||||
|
listWrapper.appendChild(list);
|
||||||
|
progressContainer.appendChild(listWrapper);
|
||||||
|
} else {
|
||||||
|
list = listWrapper.querySelector("ul.upload-progress-list");
|
||||||
|
}
|
||||||
|
|
||||||
|
const li = createFileEntry(file);
|
||||||
|
li.dataset.uploadIndex = file.uniqueIdentifier;
|
||||||
|
list.appendChild(li);
|
||||||
|
updateFileInfoCount();
|
||||||
|
});
|
||||||
|
|
||||||
|
resumableInstance.on("fileProgress", function (file) {
|
||||||
|
const percent = Math.floor(file.progress() * 100);
|
||||||
|
const li = document.querySelector(`li.upload-progress-item[data-upload-index="${file.uniqueIdentifier}"]`);
|
||||||
|
if (li && li.progressBar) {
|
||||||
|
li.progressBar.style.width = percent + "%";
|
||||||
|
|
||||||
|
// Calculate elapsed time since file entry was created.
|
||||||
|
const elapsed = (Date.now() - li.startTime) / 1000;
|
||||||
|
let speed = "";
|
||||||
|
if (elapsed > 0) {
|
||||||
|
// Calculate total bytes uploaded so far using file.progress() * file.size
|
||||||
|
const bytesUploaded = file.progress() * file.size;
|
||||||
|
const spd = bytesUploaded / elapsed;
|
||||||
|
if (spd < 1024) {
|
||||||
|
speed = spd.toFixed(0) + " B/s";
|
||||||
|
} else if (spd < 1048576) {
|
||||||
|
speed = (spd / 1024).toFixed(1) + " KB/s";
|
||||||
|
} else {
|
||||||
|
speed = (spd / 1048576).toFixed(1) + " MB/s";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
li.progressBar.innerText = percent + "% (" + speed + ")";
|
||||||
|
|
||||||
|
// Enable the pause/resume button once progress starts
|
||||||
|
const pauseResumeBtn = li.querySelector(".pause-resume-btn");
|
||||||
|
if (pauseResumeBtn) {
|
||||||
|
pauseResumeBtn.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
resumableInstance.on("fileSuccess", function (file, message) {
|
||||||
|
const li = document.querySelector(`li.upload-progress-item[data-upload-index="${file.uniqueIdentifier}"]`);
|
||||||
|
if (li && li.progressBar) {
|
||||||
|
li.progressBar.style.width = "100%";
|
||||||
|
li.progressBar.innerText = "Done";
|
||||||
|
// Hide the pause/resume button when upload is complete.
|
||||||
|
const pauseResumeBtn = li.querySelector(".pause-resume-btn");
|
||||||
|
if (pauseResumeBtn) {
|
||||||
|
pauseResumeBtn.style.display = "none";
|
||||||
|
}
|
||||||
|
const removeBtn = li.querySelector(".remove-file-btn");
|
||||||
|
if (removeBtn) {
|
||||||
|
removeBtn.style.display = "none";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loadFileList(window.currentFolder);
|
||||||
|
});
|
||||||
|
|
||||||
|
resumableInstance.on("fileError", function (file, message) {
|
||||||
|
const li = document.querySelector(`li.upload-progress-item[data-upload-index="${file.uniqueIdentifier}"]`);
|
||||||
|
if (li && li.progressBar) {
|
||||||
|
li.progressBar.innerText = "Error";
|
||||||
|
}
|
||||||
|
// Mark file as errored so that the pause/resume button acts as a restart button.
|
||||||
|
file.isError = true;
|
||||||
|
// Change the pause/resume button to show a restart icon.
|
||||||
|
const pauseResumeBtn = li ? li.querySelector(".pause-resume-btn") : null;
|
||||||
|
if (pauseResumeBtn) {
|
||||||
|
pauseResumeBtn.innerHTML = '<span class="material-icons pauseResumeBtn">replay</span>';
|
||||||
|
pauseResumeBtn.disabled = false;
|
||||||
|
}
|
||||||
|
showToast("Error uploading file: " + file.fileName);
|
||||||
|
});
|
||||||
|
|
||||||
|
resumableInstance.on("complete", function () {
|
||||||
|
// Check if any file in the current selection is marked with an error.
|
||||||
|
const hasError = window.selectedFiles.some(f => f.isError);
|
||||||
|
if (!hasError) {
|
||||||
|
// All files succeeded; clear the file list after 5 seconds.
|
||||||
|
setTimeout(() => {
|
||||||
|
if (fileInput) fileInput.value = "";
|
||||||
|
const progressContainer = document.getElementById("uploadProgressContainer");
|
||||||
|
progressContainer.innerHTML = "";
|
||||||
|
window.selectedFiles = [];
|
||||||
|
adjustFolderHelpExpansionClosed();
|
||||||
|
window.addEventListener("resize", adjustFolderHelpExpansionClosed);
|
||||||
|
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
||||||
|
if (fileInfoContainer) {
|
||||||
|
fileInfoContainer.innerHTML = `<span id="fileInfoDefault">No files selected</span>`;
|
||||||
|
}
|
||||||
|
const dropArea = document.getElementById("uploadDropArea");
|
||||||
|
if (dropArea) setDropAreaDefault();
|
||||||
|
}, 5000);
|
||||||
|
} else {
|
||||||
|
showToast("Some files failed to upload. Please check the list.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -----------------------------------------------------
|
||||||
|
XHR-based submitFiles for Drag–and–Drop (Folder) Uploads
|
||||||
|
----------------------------------------------------- */
|
||||||
function submitFiles(allFiles) {
|
function submitFiles(allFiles) {
|
||||||
const folderToUse = window.currentFolder || "root";
|
const folderToUse = window.currentFolder || "root";
|
||||||
const progressContainer = document.getElementById("uploadProgressContainer");
|
const progressContainer = document.getElementById("uploadProgressContainer");
|
||||||
@@ -323,9 +609,7 @@ function submitFiles(allFiles) {
|
|||||||
if (li) {
|
if (li) {
|
||||||
li.progressBar.style.width = "100%";
|
li.progressBar.style.width = "100%";
|
||||||
li.progressBar.innerText = "Done";
|
li.progressBar.innerText = "Done";
|
||||||
if (li.removeBtn) {
|
if (li.removeBtn) li.removeBtn.style.display = "none";
|
||||||
li.removeBtn.style.display = "none";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
uploadResults[file.uploadIndex] = true;
|
uploadResults[file.uploadIndex] = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -367,7 +651,6 @@ function submitFiles(allFiles) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
xhr.open("POST", "upload.php", true);
|
xhr.open("POST", "upload.php", true);
|
||||||
// Set the CSRF token header to match the folderManager approach.
|
|
||||||
xhr.setRequestHeader("X-CSRF-Token", window.csrfToken);
|
xhr.setRequestHeader("X-CSRF-Token", window.csrfToken);
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
});
|
});
|
||||||
@@ -377,35 +660,40 @@ function submitFiles(allFiles) {
|
|||||||
.then(serverFiles => {
|
.then(serverFiles => {
|
||||||
initFileActions();
|
initFileActions();
|
||||||
serverFiles = (serverFiles || []).map(item => item.name.trim().toLowerCase());
|
serverFiles = (serverFiles || []).map(item => item.name.trim().toLowerCase());
|
||||||
|
let allSucceeded = true;
|
||||||
allFiles.forEach(file => {
|
allFiles.forEach(file => {
|
||||||
if ((file.webkitRelativePath || file.customRelativePath || "").trim() !== "") {
|
// For files without a relative path
|
||||||
return;
|
if ((file.webkitRelativePath || file.customRelativePath || "").trim() === "") {
|
||||||
}
|
const clientFileName = file.name.trim().toLowerCase();
|
||||||
const clientFileName = file.name.trim().toLowerCase();
|
if (!uploadResults[file.uploadIndex] || !serverFiles.includes(clientFileName)) {
|
||||||
if (!uploadResults[file.uploadIndex] || !serverFiles.includes(clientFileName)) {
|
const li = progressElements[file.uploadIndex];
|
||||||
const li = progressElements[file.uploadIndex];
|
if (li) {
|
||||||
if (li) {
|
li.progressBar.innerText = "Error";
|
||||||
li.progressBar.innerText = "Error";
|
}
|
||||||
|
allSucceeded = false;
|
||||||
}
|
}
|
||||||
allSucceeded = false;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setTimeout(() => {
|
|
||||||
if (fileInput) fileInput.value = "";
|
if (allSucceeded) {
|
||||||
const removeBtns = progressContainer.querySelectorAll("button.remove-file-btn");
|
// All files succeeded—clear the list after 5 seconds.
|
||||||
removeBtns.forEach(btn => btn.style.display = "none");
|
setTimeout(() => {
|
||||||
progressContainer.innerHTML = "";
|
if (fileInput) fileInput.value = "";
|
||||||
window.selectedFiles = [];
|
const removeBtns = progressContainer.querySelectorAll("button.remove-file-btn");
|
||||||
adjustFolderHelpExpansionClosed();
|
removeBtns.forEach(btn => btn.style.display = "none");
|
||||||
window.addEventListener("resize", adjustFolderHelpExpansionClosed);
|
progressContainer.innerHTML = "";
|
||||||
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
window.selectedFiles = [];
|
||||||
if (fileInfoContainer) {
|
adjustFolderHelpExpansionClosed();
|
||||||
fileInfoContainer.innerHTML = `<span id="fileInfoDefault">No files selected</span>`;
|
window.addEventListener("resize", adjustFolderHelpExpansionClosed);
|
||||||
}
|
const fileInfoContainer = document.getElementById("fileInfoContainer");
|
||||||
const dropArea = document.getElementById("uploadDropArea");
|
if (fileInfoContainer) {
|
||||||
if (dropArea) setDropAreaDefault();
|
fileInfoContainer.innerHTML = `<span id="fileInfoDefault">No files selected</span>`;
|
||||||
}, 5000);
|
}
|
||||||
if (!allSucceeded) {
|
const dropArea = document.getElementById("uploadDropArea");
|
||||||
|
if (dropArea) setDropAreaDefault();
|
||||||
|
}, 5000);
|
||||||
|
} else {
|
||||||
|
// Some files failed—keep the list visible and show a toast.
|
||||||
showToast("Some files failed to upload. Please check the list.");
|
showToast("Some files failed to upload. Please check the list.");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -419,12 +707,15 @@ function submitFiles(allFiles) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main initUpload: sets up file input, drop area, and form submission.
|
/* -----------------------------------------------------
|
||||||
|
Main initUpload: Sets up file input, drop area, and form submission.
|
||||||
|
----------------------------------------------------- */
|
||||||
function initUpload() {
|
function initUpload() {
|
||||||
const fileInput = document.getElementById("file");
|
const fileInput = document.getElementById("file");
|
||||||
const dropArea = document.getElementById("uploadDropArea");
|
const dropArea = document.getElementById("uploadDropArea");
|
||||||
const uploadForm = document.getElementById("uploadFileForm");
|
const uploadForm = document.getElementById("uploadFileForm");
|
||||||
|
|
||||||
|
// For file picker, remove directory attributes so only files can be chosen.
|
||||||
if (fileInput) {
|
if (fileInput) {
|
||||||
fileInput.removeAttribute("webkitdirectory");
|
fileInput.removeAttribute("webkitdirectory");
|
||||||
fileInput.removeAttribute("mozdirectory");
|
fileInput.removeAttribute("mozdirectory");
|
||||||
@@ -434,6 +725,7 @@ function initUpload() {
|
|||||||
|
|
||||||
setDropAreaDefault();
|
setDropAreaDefault();
|
||||||
|
|
||||||
|
// Drag–and–drop events (for folder uploads) use original processing.
|
||||||
if (dropArea) {
|
if (dropArea) {
|
||||||
dropArea.classList.add("upload-drop-area");
|
dropArea.classList.add("upload-drop-area");
|
||||||
dropArea.addEventListener("dragover", function (e) {
|
dropArea.addEventListener("dragover", function (e) {
|
||||||
@@ -458,6 +750,7 @@ function initUpload() {
|
|||||||
processFiles(dt.files);
|
processFiles(dt.files);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// Clicking drop area triggers file input.
|
||||||
dropArea.addEventListener("click", function () {
|
dropArea.addEventListener("click", function () {
|
||||||
if (fileInput) fileInput.click();
|
if (fileInput) fileInput.click();
|
||||||
});
|
});
|
||||||
@@ -465,7 +758,14 @@ function initUpload() {
|
|||||||
|
|
||||||
if (fileInput) {
|
if (fileInput) {
|
||||||
fileInput.addEventListener("change", function () {
|
fileInput.addEventListener("change", function () {
|
||||||
processFiles(fileInput.files);
|
if (useResumable) {
|
||||||
|
// For file picker, if resumable is enabled, let it handle the files.
|
||||||
|
for (let i = 0; i < fileInput.files.length; i++) {
|
||||||
|
resumableInstance.addFile(fileInput.files[i]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
processFiles(fileInput.files);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,9 +777,21 @@ function initUpload() {
|
|||||||
showToast("No files selected.");
|
showToast("No files selected.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
submitFiles(files);
|
// If files come from file picker (no relative path), use Resumable.
|
||||||
|
if (useResumable && (!files[0].customRelativePath || files[0].customRelativePath === "")) {
|
||||||
|
// Ensure current folder is updated.
|
||||||
|
resumableInstance.opts.query.folder = window.currentFolder || "root";
|
||||||
|
resumableInstance.upload();
|
||||||
|
showToast("Resumable upload started...");
|
||||||
|
} else {
|
||||||
|
submitFiles(files);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (useResumable) {
|
||||||
|
initResumableUpload();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { initUpload };
|
export { initUpload };
|
||||||
271
upload.php
271
upload.php
@@ -12,122 +12,225 @@ if ($receivedToken !== $_SESSION['csrf_token']) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure user is authenticated
|
// Ensure user is authenticated.
|
||||||
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||||
echo json_encode(["error" => "Unauthorized"]);
|
echo json_encode(["error" => "Unauthorized"]);
|
||||||
http_response_code(401);
|
http_response_code(401);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate folder name input.
|
// Check if this is a chunked upload (Resumable.js sends "resumableChunkNumber").
|
||||||
$folder = isset($_POST['folder']) ? trim($_POST['folder']) : 'root';
|
if (isset($_POST['resumableChunkNumber'])) {
|
||||||
if ($folder !== 'root' && !preg_match('/^[A-Za-z0-9_\- \/]+$/', $folder)) {
|
// ------------- Chunked Upload Handling -------------
|
||||||
echo json_encode(["error" => "Invalid folder name"]);
|
$chunkNumber = intval($_POST['resumableChunkNumber']); // current chunk (1-indexed)
|
||||||
exit;
|
$totalChunks = intval($_POST['resumableTotalChunks']);
|
||||||
}
|
$chunkSize = intval($_POST['resumableChunkSize']);
|
||||||
|
$totalSize = intval($_POST['resumableTotalSize']);
|
||||||
// Determine the base upload directory.
|
$resumableIdentifier = $_POST['resumableIdentifier']; // unique file identifier
|
||||||
$baseUploadDir = UPLOAD_DIR;
|
$resumableFilename = $_POST['resumableFilename'];
|
||||||
if ($folder !== 'root') {
|
$folder = isset($_POST['folder']) ? trim($_POST['folder']) : 'root';
|
||||||
$baseUploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
|
if ($folder !== 'root' && !preg_match('/^[A-Za-z0-9_\- \/]+$/', $folder)) {
|
||||||
if (!is_dir($baseUploadDir)) {
|
echo json_encode(["error" => "Invalid folder name"]);
|
||||||
mkdir($baseUploadDir, 0775, true);
|
exit;
|
||||||
}
|
}
|
||||||
} else {
|
// Determine the base upload directory.
|
||||||
if (!is_dir($baseUploadDir)) {
|
$baseUploadDir = UPLOAD_DIR;
|
||||||
mkdir($baseUploadDir, 0775, true);
|
if ($folder !== 'root') {
|
||||||
|
$baseUploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
|
||||||
|
if (!is_dir($baseUploadDir)) {
|
||||||
|
mkdir($baseUploadDir, 0775, true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!is_dir($baseUploadDir)) {
|
||||||
|
mkdir($baseUploadDir, 0775, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare a collection to hold metadata for each folder.
|
// Use a temporary directory for the chunks.
|
||||||
$metadataCollection = []; // key: folder path, value: metadata array
|
$tempDir = $baseUploadDir . 'resumable_' . $resumableIdentifier . DIRECTORY_SEPARATOR;
|
||||||
$metadataChanged = []; // key: folder path, value: boolean
|
if (!is_dir($tempDir)) {
|
||||||
|
mkdir($tempDir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
$safeFileNamePattern = '/^[A-Za-z0-9_\-\.\(\) ]+$/';
|
// Save the current chunk.
|
||||||
|
$chunkFile = $tempDir . $chunkNumber; // store chunk using its number as filename
|
||||||
foreach ($_FILES["file"]["name"] as $index => $fileName) {
|
if (!move_uploaded_file($_FILES["file"]["tmp_name"], $chunkFile)) {
|
||||||
$safeFileName = basename($fileName);
|
echo json_encode(["error" => "Failed to move uploaded chunk"]);
|
||||||
if (!preg_match($safeFileNamePattern, $safeFileName)) {
|
|
||||||
echo json_encode(["error" => "Invalid file name: " . $fileName]);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Minimal Folder/Subfolder Logic ---
|
// Check if all chunks have been uploaded.
|
||||||
$relativePath = '';
|
$uploadedChunks = glob($tempDir . "*");
|
||||||
if (isset($_POST['relativePath'])) {
|
if (count($uploadedChunks) >= $totalChunks) {
|
||||||
if (is_array($_POST['relativePath'])) {
|
// All chunks uploaded. Merge chunks.
|
||||||
$relativePath = $_POST['relativePath'][$index] ?? '';
|
$targetPath = $baseUploadDir . $resumableFilename;
|
||||||
} else {
|
if (!$out = fopen($targetPath, "wb")) {
|
||||||
$relativePath = $_POST['relativePath'];
|
echo json_encode(["error" => "Failed to open target file for writing"]);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
}
|
// Concatenate each chunk in order.
|
||||||
|
for ($i = 1; $i <= $totalChunks; $i++) {
|
||||||
// Determine the complete folder path for upload and for metadata.
|
$chunkPath = $tempDir . $i;
|
||||||
$folderPath = $folder; // Base folder as provided ("root" or a subfolder)
|
if (!$in = fopen($chunkPath, "rb")) {
|
||||||
$uploadDir = $baseUploadDir; // Start with the base upload directory
|
fclose($out);
|
||||||
if (!empty($relativePath)) {
|
echo json_encode(["error" => "Failed to open chunk $i"]);
|
||||||
$subDir = dirname($relativePath);
|
exit;
|
||||||
if ($subDir !== '.' && $subDir !== '') {
|
}
|
||||||
// If base folder is 'root', then folderPath is just the subDir
|
while ($buff = fread($in, 4096)) {
|
||||||
// Otherwise, append the subdirectory to the base folder
|
fwrite($out, $buff);
|
||||||
$folderPath = ($folder === 'root') ? $subDir : $folder . "/" . $subDir;
|
}
|
||||||
// Update the upload directory accordingly.
|
fclose($in);
|
||||||
$uploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR
|
|
||||||
. str_replace('/', DIRECTORY_SEPARATOR, $folderPath) . DIRECTORY_SEPARATOR;
|
|
||||||
}
|
}
|
||||||
// Ensure the file name is taken from the relative path.
|
fclose($out);
|
||||||
$safeFileName = basename($relativePath);
|
|
||||||
}
|
|
||||||
// --- End Minimal Folder/Subfolder Logic ---
|
|
||||||
|
|
||||||
// Make sure the final upload directory exists.
|
// --- Metadata Update for Chunked Upload ---
|
||||||
if (!is_dir($uploadDir)) {
|
// For chunked uploads, assume no relativePath; so folderPath is simply $folder.
|
||||||
mkdir($uploadDir, 0775, true);
|
$folderPath = $folder;
|
||||||
}
|
|
||||||
|
|
||||||
$targetPath = $uploadDir . $safeFileName;
|
|
||||||
|
|
||||||
if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $targetPath)) {
|
|
||||||
// Generate a unique metadata file name based on the folder path.
|
|
||||||
// Replace slashes, backslashes, and spaces with dashes.
|
|
||||||
$metadataKey = ($folderPath === '' || $folderPath === 'root') ? "root" : $folderPath;
|
$metadataKey = ($folderPath === '' || $folderPath === 'root') ? "root" : $folderPath;
|
||||||
|
// Generate a metadata file name based on the folder path.
|
||||||
$metadataFileName = str_replace(['/', '\\', ' '], '-', $metadataKey) . '_metadata.json';
|
$metadataFileName = str_replace(['/', '\\', ' '], '-', $metadataKey) . '_metadata.json';
|
||||||
$metadataFile = META_DIR . $metadataFileName;
|
$metadataFile = META_DIR . $metadataFileName;
|
||||||
|
|
||||||
// Load metadata for this folder if not already loaded.
|
$uploadedDate = date(DATE_TIME_FORMAT);
|
||||||
if (!isset($metadataCollection[$metadataKey])) {
|
$uploader = $_SESSION['username'] ?? "Unknown";
|
||||||
if (file_exists($metadataFile)) {
|
|
||||||
$metadataCollection[$metadataKey] = json_decode(file_get_contents($metadataFile), true);
|
// Load existing metadata, if any.
|
||||||
} else {
|
if (file_exists($metadataFile)) {
|
||||||
$metadataCollection[$metadataKey] = [];
|
$metadataCollection = json_decode(file_get_contents($metadataFile), true);
|
||||||
|
if (!is_array($metadataCollection)) {
|
||||||
|
$metadataCollection = [];
|
||||||
}
|
}
|
||||||
$metadataChanged[$metadataKey] = false;
|
} else {
|
||||||
|
$metadataCollection = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add metadata for this file if not already present.
|
// Add metadata for this file if not already present.
|
||||||
if (!isset($metadataCollection[$metadataKey][$safeFileName])) {
|
if (!isset($metadataCollection[$resumableFilename])) {
|
||||||
$uploadedDate = date(DATE_TIME_FORMAT);
|
$metadataCollection[$resumableFilename] = [
|
||||||
$uploader = $_SESSION['username'] ?? "Unknown";
|
|
||||||
$metadataCollection[$metadataKey][$safeFileName] = [
|
|
||||||
"uploaded" => $uploadedDate,
|
"uploaded" => $uploadedDate,
|
||||||
"uploader" => $uploader
|
"uploader" => $uploader
|
||||||
];
|
];
|
||||||
$metadataChanged[$metadataKey] = true;
|
file_put_contents($metadataFile, json_encode($metadataCollection, JSON_PRETTY_PRINT));
|
||||||
}
|
}
|
||||||
|
// --- End Metadata Update ---
|
||||||
|
|
||||||
|
// Cleanup: remove the temporary directory and its chunks.
|
||||||
|
array_map('unlink', glob("$tempDir*"));
|
||||||
|
rmdir($tempDir);
|
||||||
|
|
||||||
|
echo json_encode(["success" => "File uploaded successfully"]);
|
||||||
|
exit;
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(["error" => "Error uploading file"]);
|
// Chunk successfully uploaded, but more chunks remain.
|
||||||
|
echo json_encode(["status" => "chunk uploaded"]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// After processing all files, write out metadata files for folders that changed.
|
} else {
|
||||||
foreach ($metadataCollection as $folderKey => $data) {
|
// ------------- Full Upload (Non-chunked) -------------
|
||||||
if ($metadataChanged[$folderKey]) {
|
// Validate folder name input.
|
||||||
$metadataFileName = str_replace(['/', '\\', ' '], '-', $folderKey) . '_metadata.json';
|
$folder = isset($_POST['folder']) ? trim($_POST['folder']) : 'root';
|
||||||
$metadataFile = META_DIR . $metadataFileName;
|
if ($folder !== 'root' && !preg_match('/^[A-Za-z0-9_\- \/]+$/', $folder)) {
|
||||||
file_put_contents($metadataFile, json_encode($data, JSON_PRETTY_PRINT));
|
echo json_encode(["error" => "Invalid folder name"]);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
echo json_encode(["success" => "Files uploaded successfully"]);
|
// Determine the base upload directory.
|
||||||
|
$baseUploadDir = UPLOAD_DIR;
|
||||||
|
if ($folder !== 'root') {
|
||||||
|
$baseUploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
|
||||||
|
if (!is_dir($baseUploadDir)) {
|
||||||
|
mkdir($baseUploadDir, 0775, true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!is_dir($baseUploadDir)) {
|
||||||
|
mkdir($baseUploadDir, 0775, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare a collection to hold metadata for each folder.
|
||||||
|
$metadataCollection = []; // key: folder path, value: metadata array
|
||||||
|
$metadataChanged = []; // key: folder path, value: boolean
|
||||||
|
|
||||||
|
$safeFileNamePattern = '/^[A-Za-z0-9_\-\.\(\) ]+$/';
|
||||||
|
|
||||||
|
foreach ($_FILES["file"]["name"] as $index => $fileName) {
|
||||||
|
$safeFileName = basename($fileName);
|
||||||
|
if (!preg_match($safeFileNamePattern, $safeFileName)) {
|
||||||
|
echo json_encode(["error" => "Invalid file name: " . $fileName]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Minimal Folder/Subfolder Logic ---
|
||||||
|
$relativePath = '';
|
||||||
|
if (isset($_POST['relativePath'])) {
|
||||||
|
if (is_array($_POST['relativePath'])) {
|
||||||
|
$relativePath = $_POST['relativePath'][$index] ?? '';
|
||||||
|
} else {
|
||||||
|
$relativePath = $_POST['relativePath'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the complete folder path for upload and for metadata.
|
||||||
|
$folderPath = $folder; // Base folder as provided ("root" or a subfolder)
|
||||||
|
$uploadDir = $baseUploadDir; // Start with the base upload directory
|
||||||
|
if (!empty($relativePath)) {
|
||||||
|
$subDir = dirname($relativePath);
|
||||||
|
if ($subDir !== '.' && $subDir !== '') {
|
||||||
|
$folderPath = ($folder === 'root') ? $subDir : $folder . "/" . $subDir;
|
||||||
|
$uploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR
|
||||||
|
. str_replace('/', DIRECTORY_SEPARATOR, $folderPath) . DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
$safeFileName = basename($relativePath);
|
||||||
|
}
|
||||||
|
// --- End Minimal Folder/Subfolder Logic ---
|
||||||
|
|
||||||
|
// Make sure the final upload directory exists.
|
||||||
|
if (!is_dir($uploadDir)) {
|
||||||
|
mkdir($uploadDir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetPath = $uploadDir . $safeFileName;
|
||||||
|
|
||||||
|
if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $targetPath)) {
|
||||||
|
// Generate a unique metadata file name based on the folder path.
|
||||||
|
$metadataKey = ($folderPath === '' || $folderPath === 'root') ? "root" : $folderPath;
|
||||||
|
$metadataFileName = str_replace(['/', '\\', ' '], '-', $metadataKey) . '_metadata.json';
|
||||||
|
$metadataFile = META_DIR . $metadataFileName;
|
||||||
|
|
||||||
|
if (!isset($metadataCollection[$metadataKey])) {
|
||||||
|
if (file_exists($metadataFile)) {
|
||||||
|
$metadataCollection[$metadataKey] = json_decode(file_get_contents($metadataFile), true);
|
||||||
|
} else {
|
||||||
|
$metadataCollection[$metadataKey] = [];
|
||||||
|
}
|
||||||
|
$metadataChanged[$metadataKey] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($metadataCollection[$metadataKey][$safeFileName])) {
|
||||||
|
$uploadedDate = date(DATE_TIME_FORMAT);
|
||||||
|
$uploader = $_SESSION['username'] ?? "Unknown";
|
||||||
|
$metadataCollection[$metadataKey][$safeFileName] = [
|
||||||
|
"uploaded" => $uploadedDate,
|
||||||
|
"uploader" => $uploader
|
||||||
|
];
|
||||||
|
$metadataChanged[$metadataKey] = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode(["error" => "Error uploading file"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// After processing all files, write out metadata files for folders that changed.
|
||||||
|
foreach ($metadataCollection as $folderKey => $data) {
|
||||||
|
if ($metadataChanged[$folderKey]) {
|
||||||
|
$metadataFileName = str_replace(['/', '\\', ' '], '-', $folderKey) . '_metadata.json';
|
||||||
|
$metadataFile = META_DIR . $metadataFileName;
|
||||||
|
file_put_contents($metadataFile, json_encode($data, JSON_PRETTY_PRINT));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode(["success" => "Files uploaded successfully"]);
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
@@ -1 +1,7 @@
|
|||||||
|
<IfModule mod_php7.c>
|
||||||
|
php_flag engine off
|
||||||
|
</IfModule>
|
||||||
|
<IfModule mod_php.c>
|
||||||
|
php_flag engine off
|
||||||
|
</IfModule>
|
||||||
Options -Indexes
|
Options -Indexes
|
||||||
Reference in New Issue
Block a user