mirror of
https://github.com/immich-app/immich.git
synced 2025-01-10 13:56:47 +01:00
17 lines
384 B
TypeScript
17 lines
384 B
TypeScript
|
export function getHumanReadableBytes(bytes: number): string {
|
||
|
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
|
||
|
|
||
|
let magnitude = 0;
|
||
|
let remainder = bytes;
|
||
|
while (remainder >= 1024) {
|
||
|
if (magnitude + 1 < units.length) {
|
||
|
magnitude++;
|
||
|
remainder /= 1024;
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return `${remainder.toFixed(magnitude == 0 ? 0 : 1)} ${units[magnitude]}`;
|
||
|
}
|