From 140a360e371b78d5cdae67588b4a82372699119c Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 5 Jul 2022 09:16:21 +0800 Subject: [PATCH] chore: optimize move path default value --- assets/index.js | 52 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/assets/index.js b/assets/index.js index 27b48cd..8f89950 100644 --- a/assets/index.js +++ b/assets/index.js @@ -90,9 +90,9 @@ class Uploader { } progress(event) { - let now = Date.now(); - let speed = (event.loaded - this.uploaded) / (now - this.lastUptime) * 1000; - let [speedValue, speedUnit] = formatSize(speed); + const now = Date.now(); + const speed = (event.loaded - this.uploaded) / (now - this.lastUptime) * 1000; + const [speedValue, speedUnit] = formatSize(speed); const speedText = `${speedValue}${speedUnit.toLowerCase()}/s`; const progress = formatPercent((event.loaded / event.total) * 100); const duration = formatDuration((event.total - event.loaded) / speed) @@ -180,10 +180,12 @@ function addPath(file, index) { `; } if (DATA.allow_delete) { - actionMove = ` -
- -
`; + if (DATA.allow_upload) { + actionMove = ` +
+ +
`; + } actionDelete = `
@@ -251,20 +253,26 @@ async function movePath(index) { const file = DATA.paths[index]; if (!file) return; - let filePath = decodeURI(getUrl(file.name)); - let newPath = prompt("Enter new path", filePath) + const fileUrl = getUrl(file.name); + const fileUrlObj = new URL(fileUrl) + + const prefix = DATA.uri_prefix.slice(0, -1); + + const filePath = decodeURIComponent(fileUrlObj.pathname.slice(prefix.length)); + + const newPath = prompt("Enter new path", filePath) if (!newPath || filePath === newPath) return; - const encodedNewPath = encodeURI(newPath); - + const newFileUrl = fileUrlObj.origin + prefix + encodeURI(newPath); + try { const res = await fetch(getUrl(file.name), { method: "MOVE", headers: { - "Destination": encodedNewPath, + "Destination": newFileUrl, } }); if (res.status >= 200 && res.status < 300) { - location.href = encodedNewPath.split("/").slice(0, -1).join("/") + location.href = newFileUrl.split("/").slice(0, -1).join("/") } else { throw new Error(await res.text()) } @@ -374,9 +382,9 @@ function formatSize(size) { function formatDuration(seconds) { seconds = Math.ceil(seconds); - let h = Math.floor(seconds / 3600); - let m = Math.floor((seconds - h * 3600) / 60); - let s = seconds - h * 3600 - m * 60 + const h = Math.floor(seconds / 3600); + const m = Math.floor((seconds - h * 3600) / 60); + const s = seconds - h * 3600 - m * 60 return `${padZero(h, 2)}:${padZero(m, 2)}:${padZero(s, 2)}`; } @@ -426,11 +434,13 @@ function ready() { } if (DATA.allow_upload) { dropzone(); - $newFolder.classList.remove("hidden"); - $newFolder.addEventListener("click", () => { - const name = prompt("Enter name of new folder"); - if (name) createFolder(name); - }); + if (DATA.allow_delete) { + $newFolder.classList.remove("hidden"); + $newFolder.addEventListener("click", () => { + const name = prompt("Enter name of new folder"); + if (name) createFolder(name); + }); + } document.querySelector(".upload-file").classList.remove("hidden"); document.getElementById("file").addEventListener("change", e => { const files = e.target.files;