mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-08 08:39:59 +00:00
Use correct units for site file sizes
This commit is contained in:
@@ -174,30 +174,37 @@ function onError (err) {
|
||||
}
|
||||
|
||||
|
||||
function formatSize (bytes, precision = 1) {
|
||||
// Sizes in bytes
|
||||
const kilobyte = 1024;
|
||||
const megabyte = kilobyte * 1024;
|
||||
const gigabyte = megabyte * 1024;
|
||||
const terabyte = gigabyte * 1024;
|
||||
const petabyte = terabyte * 1024;
|
||||
function formatSize (bytes, precision = 1, useMetric = false) {
|
||||
const factor = useMetric ? 1000 : 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`;
|
||||
|
||||
} else if (bytes >= kilobyte && bytes < megabyte) {
|
||||
return `${(bytes / kilobyte).toFixed(precision)} KB`;
|
||||
} else if (bytes >= kxbyte && bytes < mxbyte) {
|
||||
return `${(bytes / kxbyte).toFixed(precision)} ${
|
||||
useMetric ? "KB" : "KiB"}`;
|
||||
|
||||
} else if (bytes >= megabyte && bytes < gigabyte) {
|
||||
return `${(bytes / megabyte).toFixed(precision)} MB`;
|
||||
} else if (bytes >= mxbyte && bytes < gxbyte) {
|
||||
return `${(bytes / mxbyte).toFixed(precision)} ${
|
||||
useMetric ? "MB" : "MiB"}`;
|
||||
|
||||
} else if (bytes >= gigabyte && bytes < terabyte) {
|
||||
return `${(bytes / gigabyte).toFixed(precision)} GB`;
|
||||
} else if (bytes >= gxbyte && bytes < txbyte) {
|
||||
return `${(bytes / gxbyte).toFixed(precision)} ${
|
||||
useMetric ? "GB" : "GiB"}`;
|
||||
|
||||
} else if (bytes >= terabyte && bytes < petabyte) {
|
||||
return `${(bytes / terabyte).toFixed(precision)} TB`;
|
||||
} else if (bytes >= txbyte && bytes < pxbyte) {
|
||||
return `${(bytes / txbyte).toFixed(precision)} ${
|
||||
useMetric ? "TB" : "TiB"}`;
|
||||
|
||||
} else if (bytes >= petabyte) {
|
||||
return `${(bytes / petabyte).toFixed(precision)} PB`;
|
||||
} else if (bytes >= pxbyte) {
|
||||
return `${(bytes / pxbyte).toFixed(precision)} ${
|
||||
useMetric ? "PB" : "PiB"}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user