Files
vces/src/files.ts
T
2026-10-29 20:44:26 -03:00

42 lines
1.1 KiB
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);
}
export interface ShareEntry {
id: string;
// The link secret encrypted with the master key
secret: string;
}
// What the files API returns, size is of the encrypted file
export interface FileEntry {
id: string;
date_created: string;
size: number;
metadata: string;
share: ShareEntry | null;
}
// What the public shares API returns, metadata is encrypted with the link secret
export interface SharedFileEntry {
size: number;
metadata: string;
}