diff --git a/web/src/lib/components/admin-page/server-stats/stats-card.svelte b/web/src/lib/components/admin-page/server-stats/stats-card.svelte index 6d2cca6c4e..31baa0afdd 100644 --- a/web/src/lib/components/admin-page/server-stats/stats-card.svelte +++ b/web/src/lib/components/admin-page/server-stats/stats-card.svelte @@ -27,7 +27,7 @@ class="text-immich-primary dark:text-immich-dark-primary">{value} {#if unit} - {ByteUnit[unit]} + {unit} {/if} diff --git a/web/src/lib/utils/byte-units.ts b/web/src/lib/utils/byte-units.ts index d610109d01..dae44009e2 100644 --- a/web/src/lib/utils/byte-units.ts +++ b/web/src/lib/utils/byte-units.ts @@ -1,13 +1,15 @@ -export enum ByteUnit { - 'B' = 0, - 'KiB' = 1, - 'MiB' = 2, - 'GiB' = 3, - 'TiB' = 4, - 'PiB' = 5, - 'EiB' = 6, +export const enum ByteUnit { + 'B' = 'B', + 'KiB' = 'KiB', + 'MiB' = 'MiB', + 'GiB' = 'GiB', + 'TiB' = 'TiB', + 'PiB' = 'PiB', + 'EiB' = 'EiB', } +const byteUnits = [ByteUnit.B, ByteUnit.KiB, ByteUnit.MiB, ByteUnit.GiB, ByteUnit.TiB, ByteUnit.PiB, ByteUnit.EiB]; + /** * Convert bytes to best human readable unit and number of that unit. * @@ -21,7 +23,7 @@ export enum ByteUnit { export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, ByteUnit] { const magnitude = Math.floor(Math.log(bytes === 0 ? 1 : bytes) / Math.log(1024)); - return [Number.parseFloat((bytes / 1024 ** magnitude).toFixed(maxPrecision)), magnitude]; + return [Number.parseFloat((bytes / 1024 ** magnitude).toFixed(maxPrecision)), byteUnits[magnitude]]; } /** @@ -37,7 +39,7 @@ export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, Byte */ export function getByteUnitString(bytes: number, locale?: string, maxPrecision = 1): string { const [size, unit] = getBytesWithUnit(bytes, maxPrecision); - return `${size.toLocaleString(locale)} ${ByteUnit[unit]}`; + return `${size.toLocaleString(locale)} ${unit}`; } /** @@ -50,7 +52,7 @@ export function getByteUnitString(bytes: number, locale?: string, maxPrecision = * @returns bytes (number) */ export function convertToBytes(size: number, unit: ByteUnit): number { - return size * 1024 ** unit; + return size * 1024 ** byteUnits.indexOf(unit); } /** @@ -63,5 +65,5 @@ export function convertToBytes(size: number, unit: ByteUnit): number { * @returns bytes (number) */ export function convertFromBytes(bytes: number, unit: ByteUnit): number { - return bytes / 1024 ** unit; + return bytes / 1024 ** byteUnits.indexOf(unit); } diff --git a/web/src/routes/admin/library-management/+page.svelte b/web/src/routes/admin/library-management/+page.svelte index 011726e63c..51be89ae40 100644 --- a/web/src/routes/admin/library-management/+page.svelte +++ b/web/src/routes/admin/library-management/+page.svelte @@ -337,7 +337,7 @@ {totalCount[index]} - {diskUsage[index]} {ByteUnit[diskUsageUnit[index]]} + {diskUsage[index]} {diskUsageUnit[index]} {/if}