refactor: webui file size format (#698)

This commit is contained in:
sigoden
2026-04-27 08:12:35 +08:00
committed by GitHub
parent 4f2dee3916
commit 8e90ffa9c8
+9 -6
View File
@@ -262,12 +262,12 @@ class Uploader {
const now = Date.now(); const now = Date.now();
const elapsed = now - this.lastUptime; const elapsed = now - this.lastUptime;
if (elapsed < 300) return; // throttle update for safari 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 [speedValue, speedUnit] = formatFileSize(speed);
const speedText = `${speedValue} ${speedUnit}/s`; const speedText = `${speedValue} ${speedUnit}/s`;
const progress = formatPercent(((event.loaded + this.uploadOffset) / this.file.size) * 100); const progress = formatPercent(((event.loaded + this.uploadOffset) / this.file.size) * 100);
const duration = formatDuration((event.total - event.loaded) / speed); const duration = formatDuration((event.total - event.loaded) / speed);
this.$uploadStatus.innerHTML = `<span style="width: 80px;">${speedText}</span><span>${progress} ${duration}</span>`; this.$uploadStatus.innerHTML = `<span style="width: 80px;">${speedText}</span><span style="margin-left: 5px;">${progress} ${duration}</span>`;
this.uploaded = event.loaded; this.uploaded = event.loaded;
this.lastUptime = now; this.lastUptime = now;
} }
@@ -937,11 +937,14 @@ function formatFileSize(size) {
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
if (size == 0) return [0, "B"]; if (size == 0) return [0, "B"];
const i = parseInt(Math.floor(Math.log(size) / Math.log(1024))); const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
let ratio = 1; const raw = size / Math.pow(1024, i);
if (i >= 3) { let value;
ratio = 100; 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) { function formatDuration(seconds) {