Files
vces/src/files.ts
T
2026-10-29 17:50:21 -03:00

21 lines
687 B
TypeScript

const SIZE_UNITS = ["B", "KB", "MB", "GB", "TB"];
export function format_bytes(size: number) {
if (size < 1) {
return "0 B";
}
const exponent = Math.min(Math.floor(Math.log(size) / Math.log(1024)), SIZE_UNITS.length - 1);
const value = size / 1024 ** exponent;
return `${Number.isInteger(value) ? value : value.toFixed(1)} ${SIZE_UNITS[exponent]}`;
}
export function download_blob(blob: Blob, file_name: string) {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = file_name;
link.click();
// Revoking right away can cancel the download in some browsers
setTimeout(() => URL.revokeObjectURL(url), 60_000);
}