feat(sync): format byte numbers in download logs

This commit is contained in:
cswimr 2025-02-12 08:20:57 -06:00
parent 0b3f39b8b2
commit 6907543fb8
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087

View file

@ -41,7 +41,22 @@ async function downloadFile(
doHashesMatch(hash, hashFormat, dataBuffer);
console.log(`Saving file to ${path.toString()}`);
const fileSize = await write(path.toString(), arrayBuffer);
console.log(`Saved ${fileSize} file to ${path.toString()}`);
console.log(`Saved ${formatBytes(fileSize)} file to ${path.toString()}`);
}
/**
* Converts bytes into a human-readable format with size units.
* @param bytes - The number of bytes.
* @param decimals - The number of decimal places to use.
* @returns Formatted string (e.g., "1.23 MB").
*/
function formatBytes(bytes: number, decimals: number = 2) {
if (bytes === 0) return "0 Bytes";
const k = 1024; // or 1000 for decimal units
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
/**