var fmPath = "/home"; var fmLocked = true; var fmPageActive = false; var fmUploading = false; var fmDownloading = false; var fmLoadingList = false; var fmStatusText = ""; var fmStatusErr = false; var fmUploadStatus = ""; var fmDownloadStatus = ""; var fmTaskResult = ""; var fmTaskResultErr = false; function initPage0() { fmPageActive = true; fmLocked = true; fmUpdateButtons(); if (fmStatusText) { fmSetStatus(fmStatusText, fmStatusErr); } else { fmRenderStatus(); } fmCheckUnlock(); } function exitPage0() { fmPageActive = false; } // ---------- 状态显示 ---------- function fmRenderStatus() { var el = document.getElementById("fm-status"); if (!el) { return; } var text = ""; var isErr = false; if (fmUploadStatus && fmDownloadStatus) { text = fmUploadStatus + "\n" + fmDownloadStatus; } else if (fmUploadStatus) { text = fmUploadStatus; } else if (fmDownloadStatus) { text = fmDownloadStatus; } else if (fmStatusText) { text = fmStatusText; isErr = fmStatusErr; } else { text = fmTaskResult; isErr = fmTaskResultErr; } el.textContent = text; el.className = "fm-status" + (isErr ? " err" : ""); } function fmSetStatus(text, isErr) { fmStatusText = text || ""; fmStatusErr = !!isErr && !!fmStatusText; if (fmStatusText) { fmTaskResult = ""; fmTaskResultErr = false; } fmRenderStatus(); } function fmSetTaskStatus(type, text) { if (type === "upload") { fmUploadStatus = text || ""; } else { fmDownloadStatus = text || ""; } fmRenderStatus(); } function fmSetTaskResult(text, isErr) { fmStatusText = ""; fmStatusErr = false; fmTaskResult = text || ""; fmTaskResultErr = !!isErr && !!fmTaskResult; fmRenderStatus(); } function fmStatusIdle() { return !fmUploading && !fmDownloading; } function fmUpdateButtons() { setBtnStatus("fm-upload-btn", !fmLocked && !fmUploading, "page0"); setBtnStatus("fm-refresh-btn", !fmLocked, "page0"); var tbody = document.getElementById("fm-tbody"); if (tbody) { var buttons = tbody.querySelectorAll(".fm-download-btn"); for (var i = 0; i < buttons.length; i++) { buttons[i].disabled = fmLocked || fmDownloading; } } } // ---------- 解锁操作 ---------- function fmCheckUnlock() { fetch("/api/files/unlock", { credentials: "include" }) .then(function (r) { if (!r.ok) { throw new Error("请求失败"); } return r.json(); }) .then(function (d) { if (d && d.unlocked) { fmShowBrowser(); } else { fmShowLock(); } }) .catch(function () { fmShowLock(); }); } function fmShowLock() { fmLocked = true; var lock = document.getElementById("fm-lock"); if (lock) { lock.style.display = "flex"; } fmUpdateButtons(); var input = document.getElementById("fm-password"); if (input) { input.value = ""; setTimeout(function () { if (fmPageActive) { input.focus(); } }, 0); } } function fmShowBrowser() { fmLocked = false; var lock = document.getElementById("fm-lock"); if (lock) { lock.style.display = "none"; } fmUpdateButtons(); fmLoad(fmPath || "/"); } function fmUnlock() { var errEl = document.getElementById("fm-lock-err"); var input = document.getElementById("fm-password"); if (!errEl || !input) { return; } var pwd = input.value; if (!pwd) { errEl.textContent = "请输入密码"; return; } fetch("/api/files/unlock", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password: pwd }) }) .then(function (r) { if (!r.ok) { throw new Error("密码错误"); } return r.json(); }) .then(function () { errEl.textContent = ""; input.value = ""; fmShowBrowser(); }) .catch(function (e) { errEl.textContent = e.message === "密码错误" ? "密码错误" : "请求失败"; }); } // ---------- 内部函数 ---------- function fmJoin(path, name) { return path === "/" ? "/" + name : path + "/" + name; } function fmEncodePath(path) { return encodeURIComponent(path).replace(/'/g, "%27"); } function fmFormatSize(n) { if (n < 1024) { return n + " B"; } var units = ["KB", "MB", "GB", "TB"]; var v = n; for (var i = 0; i < units.length; i++) { v /= 1024; if (v < 1024 || i === units.length - 1) { return v.toFixed(v < 10 ? 1 : 0) + " " + units[i]; } } return n + " B"; } function fmFormatTime(ts) { if (!ts) { return "--"; } var d = new Date(ts * 1000); function p(x) { return String(x).padStart(2, "0"); } return d.getFullYear() + "-" + p(d.getMonth() + 1) + "-" + p(d.getDate()) + " " + p(d.getHours()) + ":" + p(d.getMinutes()) + ":" + p(d.getSeconds()); } function fmCheckAuthStatus(r) { if (r.status === 401 || r.status === 403) { fmShowLock(); return true; } return false; } // ---------- 文件浏览 ---------- function fmLoad(path, keepStatus) { if (!fmPageActive || fmLoadingList || fmLocked) { return; } fmLoadingList = true; var loadingText = "正在加载 " + path + " ..."; if (!keepStatus && fmStatusIdle()) { fmSetStatus(loadingText); } fetch("/api/files/list?path=" + encodeURIComponent(path), { credentials: "include" }) .then(function (r) { if (fmCheckAuthStatus(r)) { throw new Error("locked"); } if (!r.ok) { return r.text().then(function (t) { throw new Error(t || "加载失败"); }); } return r.json(); }) .then(function (d) { fmPath = d.path || path; fmRender(d.list || []); if (!keepStatus && fmStatusText === loadingText) { fmSetStatus(""); } }) .catch(function (e) { if (e.message !== "locked" && !keepStatus && fmStatusText === loadingText) { fmSetStatus("加载失败: " + e.message, true); } }) .finally(function () { fmLoadingList = false; }); } function fmRefresh() { fmLoad(fmPath); } function fmRender(list) { var tbody = document.getElementById("fm-tbody"); var bc = document.getElementById("fm-breadcrumb"); if (!tbody || !bc) { return; } var html = '/'; if (fmPath !== "/") { var parts = fmPath.split("/").filter(Boolean); var acc = ""; for (var i = 0; i < parts.length; i++) { acc += "/" + parts[i]; if (i > 0) { html += '/'; } html += '' + escapeHtml(parts[i]) + ""; } } bc.innerHTML = html; var rows = ""; if (fmPath !== "/") { var parent = fmPath.replace(/\/+$/, "").replace(/\/[^/]*$/, "") || "/"; rows += '' + '' + '..' + '-' + '-' + '-'; } for (var i = 0; i < list.length; i++) { var e = list[i]; var child = fmJoin(fmPath, e.name); var pathArg = "decodeURIComponent('" + fmEncodePath(child) + "')"; if (e.isDir) { rows += '' + '' + '' + escapeHtml(e.name) + "" + '4.0K' + '' + fmFormatTime(e.modTime) + "" + ''; } else { rows += '' + '' + '' + escapeHtml(e.name) + "" + '' + fmFormatSize(e.size) + "" + '' + fmFormatTime(e.modTime) + "" + ''; } } tbody.innerHTML = rows || '空目录'; fmUpdateButtons(); } // ---------- 文件上传 ---------- function fmPickUpload() { document.getElementById("fm-upload-input").click(); } function fmUpload(input) { var files = input.files; if (!files || !files.length || fmUploading || fmLocked) { return; } fmUploading = true; fmStatusText = ""; fmTaskResult = ""; fmTaskResultErr = false; fmSetTaskStatus("upload", "正在准备上传..."); fmUpdateButtons(); var uploadDir = fmPath; var uploaded = 0; function next() { if (uploaded >= files.length) { fmUploading = false; fmSetTaskStatus("upload", ""); fmUpdateButtons(); input.value = ""; setTimeout(function () { if (fmPageActive) { fmPath = uploadDir; fmLoad(uploadDir, true); } }, 800); return; } var file = files[uploaded++]; fmSetTaskStatus("upload", "正在上传 " + file.name + " (0%)"); var fd = new FormData(); fd.append("file", file); var xhr = new XMLHttpRequest(); xhr.open("POST", "/api/files/upload?path=" + encodeURIComponent(uploadDir), true); xhr.timeout = 0; xhr.upload.onprogress = function (e) { if (!e.lengthComputable) { return; } var percent = Math.floor((e.loaded * 100) / e.total); if (percent >= 100) { fmSetTaskStatus("upload", "上传完成,正在保存 " + file.name + " ..."); } else { fmSetTaskStatus("upload", "正在上传 " + file.name + " (" + percent + "%)"); } }; xhr.onload = function () { if (xhr.status === 401 || xhr.status === 403) { fmUploading = false; fmSetTaskStatus("upload", ""); fmUpdateButtons(); input.value = ""; if (fmPageActive) { fmShowLock(); } return; } if (xhr.status === 200) { fmSetTaskResult("上传完成: " + file.name + " (已保存到 " + uploadDir + ")"); } else if (xhr.status === 0) { fmSetTaskResult("上传失败: 网络连接中断 (" + file.name + ")", true); } else { fmSetTaskResult("上传失败: " + (xhr.responseText || "HTTP " + xhr.status), true); } next(); }; xhr.onerror = function () { fmSetTaskResult("上传失败: 网络错误 (" + file.name + ")", true); next(); }; xhr.send(fd); } next(); } // ---------- 文件下载 ---------- function fmDownload(path, isDir) { if (fmDownloading || fmLocked) { return; } var name = path.split("/").filter(Boolean).pop() || "download"; if (isDir) { name += ".tar.gz"; } fmDownloading = true; fmStatusText = ""; fmTaskResult = ""; fmTaskResultErr = false; fmSetTaskStatus("download", "准备下载 " + name + " ..."); fmUpdateButtons(); var xhr = new XMLHttpRequest(); xhr.open("GET", "/api/files/download?path=" + encodeURIComponent(path), true); xhr.responseType = "blob"; xhr.timeout = 0; xhr.onprogress = function (e) { if (e.lengthComputable && e.total > 0) { var percent = Math.floor((e.loaded * 100) / e.total); fmSetTaskStatus("download", "正在下载 " + name + " (" + percent + "%)"); } else { fmSetTaskStatus("download", "正在下载 " + name + " (已接收 " + fmFormatSize(e.loaded) + ")"); } }; xhr.onload = function () { if (xhr.status === 401 || xhr.status === 403) { fmDownloading = false; fmSetTaskStatus("download", ""); fmUpdateButtons(); if (fmPageActive) { fmShowLock(); } return; } if (xhr.status !== 200) { var reader = new FileReader(); reader.onload = function () { fmDownloading = false; fmSetTaskStatus("download", ""); fmSetTaskResult("下载失败: " + (reader.result || "HTTP " + xhr.status) + " (" + name + ")", true); fmUpdateButtons(); }; reader.onerror = function () { fmDownloading = false; fmSetTaskStatus("download", ""); fmSetTaskResult("下载失败: HTTP " + xhr.status + " (" + name + ")", true); fmUpdateButtons(); }; reader.readAsText(xhr.response); return; } var url = URL.createObjectURL(xhr.response); var a = document.createElement("a"); a.href = url; a.download = name; document.body.appendChild(a); a.click(); document.body.removeChild(a); setTimeout(function () { URL.revokeObjectURL(url); }, 10000); fmDownloading = false; fmSetTaskStatus("download", ""); fmSetTaskResult("下载完成: " + name); fmUpdateButtons(); }; xhr.onerror = function () { fmDownloading = false; fmSetTaskStatus("download", ""); fmSetTaskResult("下载失败: 网络错误 (" + name + ")", true); fmUpdateButtons(); }; xhr.send(); }