feat(sync): format byte numbers in download logs
This commit is contained in:
parent
0b3f39b8b2
commit
6907543fb8
1 changed files with 16 additions and 1 deletions
|
@ -41,7 +41,22 @@ async function downloadFile(
|
||||||
doHashesMatch(hash, hashFormat, dataBuffer);
|
doHashesMatch(hash, hashFormat, dataBuffer);
|
||||||
console.log(`Saving file to ${path.toString()}`);
|
console.log(`Saving file to ${path.toString()}`);
|
||||||
const fileSize = await write(path.toString(), arrayBuffer);
|
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue