mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
feat(server,web,mobile): Use binary prefixes for data sizes (#1009)
This commit is contained in:
@@ -35,7 +35,7 @@ export class DownloadService {
|
||||
fileCount++;
|
||||
|
||||
// for easier testing, can be changed before merging.
|
||||
if (totalSize > HumanReadableSize.GB * 20) {
|
||||
if (totalSize > HumanReadableSize.GiB * 20) {
|
||||
complete = false;
|
||||
this.logger.log(
|
||||
`Archive size exceeded after ${fileCount} files, capping at ${totalSize} bytes (${asHumanReadable(
|
||||
|
||||
@@ -1,31 +1,25 @@
|
||||
const KB = 1000;
|
||||
const MB = KB * 1000;
|
||||
const GB = MB * 1000;
|
||||
const TB = GB * 1000;
|
||||
const PB = TB * 1000;
|
||||
const KiB = Math.pow(1024, 1);
|
||||
const MiB = Math.pow(1024, 2);
|
||||
const GiB = Math.pow(1024, 3);
|
||||
const TiB = Math.pow(1024, 4);
|
||||
const PiB = Math.pow(1024, 5);
|
||||
|
||||
export const HumanReadableSize = { KB, MB, GB, TB, PB };
|
||||
export const HumanReadableSize = { KiB, MiB, GiB, TiB, PiB };
|
||||
|
||||
export function asHumanReadable(bytes: number, precision = 1) {
|
||||
if (bytes >= PB) {
|
||||
return `${(bytes / PB).toFixed(precision)}PB`;
|
||||
}
|
||||
export function asHumanReadable(bytes: number, precision = 1): string {
|
||||
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
|
||||
|
||||
if (bytes >= TB) {
|
||||
return `${(bytes / TB).toFixed(precision)}TB`;
|
||||
}
|
||||
let magnitude = 0;
|
||||
let remainder = bytes;
|
||||
while (remainder >= 1024) {
|
||||
if (magnitude + 1 < units.length) {
|
||||
magnitude++;
|
||||
remainder /= 1024;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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`;
|
||||
return `${remainder.toFixed( magnitude == 0 ? 0 : precision )} ${units[magnitude]}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user