1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00

fix(web): byte units enum (#11161)

This commit is contained in:
Michel Heusschen 2024-07-17 13:25:06 +02:00 committed by GitHub
parent c9c56ac600
commit e3fd766e9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 14 deletions

View file

@ -27,7 +27,7 @@
class="text-immich-primary dark:text-immich-dark-primary">{value}</span class="text-immich-primary dark:text-immich-dark-primary">{value}</span
> >
{#if unit} {#if unit}
<span class="absolute -top-5 right-2 text-base font-light text-gray-400">{ByteUnit[unit]}</span> <span class="absolute -top-5 right-2 text-base font-light text-gray-400">{unit}</span>
{/if} {/if}
</div> </div>
</div> </div>

View file

@ -1,13 +1,15 @@
export enum ByteUnit { export const enum ByteUnit {
'B' = 0, 'B' = 'B',
'KiB' = 1, 'KiB' = 'KiB',
'MiB' = 2, 'MiB' = 'MiB',
'GiB' = 3, 'GiB' = 'GiB',
'TiB' = 4, 'TiB' = 'TiB',
'PiB' = 5, 'PiB' = 'PiB',
'EiB' = 6, '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. * 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] { export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, ByteUnit] {
const magnitude = Math.floor(Math.log(bytes === 0 ? 1 : bytes) / Math.log(1024)); 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 { export function getByteUnitString(bytes: number, locale?: string, maxPrecision = 1): string {
const [size, unit] = getBytesWithUnit(bytes, maxPrecision); 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) * @returns bytes (number)
*/ */
export function convertToBytes(size: number, unit: ByteUnit): 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) * @returns bytes (number)
*/ */
export function convertFromBytes(bytes: number, unit: ByteUnit): number { export function convertFromBytes(bytes: number, unit: ByteUnit): number {
return bytes / 1024 ** unit; return bytes / 1024 ** byteUnits.indexOf(unit);
} }

View file

@ -337,7 +337,7 @@
<td class=" text-ellipsis px-4 text-sm"> <td class=" text-ellipsis px-4 text-sm">
{totalCount[index]} {totalCount[index]}
</td> </td>
<td class=" text-ellipsis px-4 text-sm"> {diskUsage[index]} {ByteUnit[diskUsageUnit[index]]}</td> <td class=" text-ellipsis px-4 text-sm">{diskUsage[index]} {diskUsageUnit[index]}</td>
{/if} {/if}
<td class=" text-ellipsis px-4 text-sm"> <td class=" text-ellipsis px-4 text-sm">