From 8e90ffa9c87e5c3ccc3b08cf37061215472aff8c Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 27 Apr 2026 08:12:35 +0800 Subject: [PATCH] refactor: webui file size format (#698) --- assets/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/assets/index.js b/assets/index.js index 63ad194..3e01af1 100644 --- a/assets/index.js +++ b/assets/index.js @@ -262,12 +262,12 @@ class Uploader { const now = Date.now(); const elapsed = now - this.lastUptime; if (elapsed < 300) return; // throttle update for safari - const speed = (event.loaded - this.uploaded) / (elapsed) * 1000; + const speed = (event.loaded - this.uploaded) / elapsed * 1000; const [speedValue, speedUnit] = formatFileSize(speed); const speedText = `${speedValue} ${speedUnit}/s`; const progress = formatPercent(((event.loaded + this.uploadOffset) / this.file.size) * 100); const duration = formatDuration((event.total - event.loaded) / speed); - this.$uploadStatus.innerHTML = `${speedText}${progress} ${duration}`; + this.$uploadStatus.innerHTML = `${speedText}${progress} ${duration}`; this.uploaded = event.loaded; this.lastUptime = now; } @@ -937,11 +937,14 @@ function formatFileSize(size) { const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; if (size == 0) return [0, "B"]; const i = parseInt(Math.floor(Math.log(size) / Math.log(1024))); - let ratio = 1; - if (i >= 3) { - ratio = 100; + const raw = size / Math.pow(1024, i); + let value; + if (i > 0 && raw < 999.95) { + value = Math.round(raw * 10) / 10; + } else { + value = Math.round(raw); } - return [Math.round(size * ratio / Math.pow(1024, i), 2) / ratio, sizes[i]]; + return [value, sizes[i]]; } function formatDuration(seconds) {