Use correct units for site file sizes

This commit is contained in:
hensm
2020-09-09 15:56:51 +01:00
parent c29827e919
commit c4eccea8bb

View File

@@ -174,30 +174,37 @@ function onError (err) {
} }
function formatSize (bytes, precision = 1) { function formatSize (bytes, precision = 1, useMetric = false) {
// Sizes in bytes const factor = useMetric ? 1000 : 1024;
const kilobyte = 1024;
const megabyte = kilobyte * 1024;
const gigabyte = megabyte * 1024;
const terabyte = gigabyte * 1024;
const petabyte = terabyte * 1024;
if (bytes >= 0 && bytes < kilobyte) { // Sizes in bytes
const kxbyte = factor;
const mxbyte = kxbyte * factor;
const gxbyte = mxbyte * factor;
const txbyte = gxbyte * factor;
const pxbyte = txbyte * factor;
if (bytes >= 0 && bytes < kxbyte) {
return `${bytes} B`; return `${bytes} B`;
} else if (bytes >= kilobyte && bytes < megabyte) { } else if (bytes >= kxbyte && bytes < mxbyte) {
return `${(bytes / kilobyte).toFixed(precision)} KB`; return `${(bytes / kxbyte).toFixed(precision)} ${
useMetric ? "KB" : "KiB"}`;
} else if (bytes >= megabyte && bytes < gigabyte) { } else if (bytes >= mxbyte && bytes < gxbyte) {
return `${(bytes / megabyte).toFixed(precision)} MB`; return `${(bytes / mxbyte).toFixed(precision)} ${
useMetric ? "MB" : "MiB"}`;
} else if (bytes >= gigabyte && bytes < terabyte) { } else if (bytes >= gxbyte && bytes < txbyte) {
return `${(bytes / gigabyte).toFixed(precision)} GB`; return `${(bytes / gxbyte).toFixed(precision)} ${
useMetric ? "GB" : "GiB"}`;
} else if (bytes >= terabyte && bytes < petabyte) { } else if (bytes >= txbyte && bytes < pxbyte) {
return `${(bytes / terabyte).toFixed(precision)} TB`; return `${(bytes / txbyte).toFixed(precision)} ${
useMetric ? "TB" : "TiB"}`;
} else if (bytes >= petabyte) { } else if (bytes >= pxbyte) {
return `${(bytes / petabyte).toFixed(precision)} PB`; return `${(bytes / pxbyte).toFixed(precision)} ${
useMetric ? "PB" : "PiB"}`;
} }
} }