mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-26 08:29:41 -05:00
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net> Co-authored-by: Neil <kylelzzz02@icloud.com> Co-authored-by: Graham <info@quantumx-apps.com> Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
33 lines
1012 B
JavaScript
33 lines
1012 B
JavaScript
export function getHumanReadableFilesize(fileSizeBytes) {
|
|
let size; // size in the specified unit
|
|
let unit; // the unit name
|
|
unit = 'bytes';
|
|
size = fileSizeBytes;
|
|
|
|
switch (true) {
|
|
case fileSizeBytes < 1024:
|
|
break;
|
|
case fileSizeBytes < 1024 ** 2: // 1 KB - 1 MB
|
|
size = fileSizeBytes / 1024;
|
|
unit = 'KB';
|
|
break;
|
|
case fileSizeBytes < 1024 ** 3: // 1 MB - 1 GB
|
|
size = fileSizeBytes / (1024 ** 2);
|
|
unit = 'MB';
|
|
break;
|
|
case fileSizeBytes < 1024 ** 4: // 1 GB - 1 TB
|
|
size = fileSizeBytes / (1024 ** 3);
|
|
unit = 'GB';
|
|
break;
|
|
case fileSizeBytes < 1024 ** 5: // 1 TB - 1 PB
|
|
size = fileSizeBytes / (1024 ** 4);
|
|
unit = 'TB';
|
|
break;
|
|
default: // >= 1 PB
|
|
size = fileSizeBytes / (1024 ** 5);
|
|
unit = 'PB';
|
|
break;
|
|
}
|
|
return `${size.toFixed(1)} ${unit}`;
|
|
}
|