mirror of
https://github.com/immich-app/immich.git
synced 2025-12-23 01:11:36 +03:00
32 lines
637 B
TypeScript
32 lines
637 B
TypeScript
|
|
const KB = 1000;
|
||
|
|
const MB = KB * 1000;
|
||
|
|
const GB = MB * 1000;
|
||
|
|
const TB = GB * 1000;
|
||
|
|
const PB = TB * 1000;
|
||
|
|
|
||
|
|
export const HumanReadableSize = { KB, MB, GB, TB, PB };
|
||
|
|
|
||
|
|
export function asHumanReadable(bytes: number, precision = 1) {
|
||
|
|
if (bytes >= PB) {
|
||
|
|
return `${(bytes / PB).toFixed(precision)}PB`;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (bytes >= TB) {
|
||
|
|
return `${(bytes / TB).toFixed(precision)}TB`;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (bytes >= GB) {
|
||
|
|
return `${(bytes / GB).toFixed(precision)}GB`;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (bytes >= MB) {
|
||
|
|
return `${(bytes / MB).toFixed(precision)}MB`;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (bytes >= KB) {
|
||
|
|
return `${(bytes / KB).toFixed(precision)}KB`;
|
||
|
|
}
|
||
|
|
|
||
|
|
return `${bytes}B`;
|
||
|
|
}
|