19 lines
559 B
TypeScript
19 lines
559 B
TypeScript
import type { ElectronFile } from "./types/file";
|
|
|
|
export function getFileNameSize(file: File | ElectronFile) {
|
|
return `${file.name}_${convertBytesToHumanReadable(file.size)}`;
|
|
}
|
|
|
|
export function convertBytesToHumanReadable(
|
|
bytes: number,
|
|
precision = 2,
|
|
): string {
|
|
if (bytes === 0 || isNaN(bytes)) {
|
|
return "0 MB";
|
|
}
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
return (bytes / Math.pow(1024, i)).toFixed(precision) + " " + sizes[i];
|
|
}
|