mirror of
https://github.com/sigoden/dufs.git
synced 2026-06-07 15:59:03 +03:00
refactor: webui file size format (#698)
This commit is contained in:
+9
-6
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user