mirror of
https://github.com/immich-app/immich.git
synced 2025-12-20 17:25:35 +03:00
@@ -8,7 +8,7 @@ import { downloadManager } from '$lib/stores/download';
|
||||
import { preferences } from '$lib/stores/user.store';
|
||||
import { downloadRequest, getKey, s, withError } from '$lib/utils';
|
||||
import { createAlbum } from '$lib/utils/album-utils';
|
||||
import { asByteUnitString } from '$lib/utils/byte-units';
|
||||
import { getByteUnitString } from '$lib/utils/byte-units';
|
||||
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
|
||||
import {
|
||||
addAssetsToAlbum as addAssets,
|
||||
@@ -232,7 +232,7 @@ export function isFlipped(orientation?: string | null) {
|
||||
|
||||
export function getFileSize(asset: AssetResponseDto): string {
|
||||
const size = asset.exifInfo?.fileSizeInByte || 0;
|
||||
return size > 0 ? asByteUnitString(size, undefined, 4) : 'Invalid Data';
|
||||
return size > 0 ? getByteUnitString(size, undefined, 4) : 'Invalid Data';
|
||||
}
|
||||
|
||||
export function getAssetResolution(asset: AssetResponseDto): string {
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* Convert to bytes from on a specified unit.
|
||||
*
|
||||
* * `1, 'GiB'`, returns `1073741824` bytes
|
||||
*
|
||||
* @param size value to be converted
|
||||
* @param unit unit to convert from
|
||||
* @returns bytes (number)
|
||||
*/
|
||||
export function convertToBytes(size: number, unit: 'GiB'): number {
|
||||
let bytes = 0;
|
||||
|
||||
if (unit === 'GiB') {
|
||||
bytes = size * 1_073_741_824;
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from bytes to a specified unit.
|
||||
*
|
||||
* * `11073741824, 'GiB'`, returns `1` GiB
|
||||
*
|
||||
* @param bytes value to be converted
|
||||
* @param unit unit to convert to
|
||||
* @returns bytes (number)
|
||||
*/
|
||||
export function convertFromBytes(bytes: number, unit: 'GiB'): number {
|
||||
let size = 0;
|
||||
|
||||
if (unit === 'GiB') {
|
||||
size = bytes / 1_073_741_824;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
25
web/src/lib/utils/byte-units.spec.ts
Normal file
25
web/src/lib/utils/byte-units.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { ByteUnit, getByteUnitString, getBytesWithUnit } from '$lib/utils/byte-units';
|
||||
|
||||
describe('getBytesWithUnit', () => {
|
||||
const tests = [
|
||||
{ bytes: 0, expected: [0, ByteUnit.B] },
|
||||
{ bytes: 42 * 2 ** 20, expected: [42, ByteUnit.MiB] },
|
||||
{ bytes: 69 * 2 ** 20 + 420 * 2 ** 19, expected: [279, ByteUnit.MiB] },
|
||||
{ bytes: 42 + 1337, maxPrecision: 3, expected: [1.347, ByteUnit.KiB] },
|
||||
{ bytes: 42 + 69, expected: [111, ByteUnit.B] },
|
||||
{ bytes: 2 ** 30 - 1, expected: [1024, ByteUnit.MiB] },
|
||||
{ bytes: 2 ** 30, expected: [1, ByteUnit.GiB] },
|
||||
{ bytes: 2 ** 30 + 1, expected: [1, ByteUnit.GiB] },
|
||||
];
|
||||
for (const { bytes, maxPrecision, expected } of tests) {
|
||||
it(`${bytes} should be split up in the factor ${expected[0]} and unit ${expected[1]}`, () => {
|
||||
expect(getBytesWithUnit(bytes, maxPrecision)).toEqual(expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('asByteUnitString', () => {
|
||||
it('should correctly return string', () => {
|
||||
expect(getByteUnitString(42 * 2 ** 20)).toEqual('42 MiB');
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,13 @@
|
||||
export enum ByteUnit {
|
||||
'B' = 0,
|
||||
'KiB' = 1,
|
||||
'MiB' = 2,
|
||||
'GiB' = 3,
|
||||
'TiB' = 4,
|
||||
'PiB' = 5,
|
||||
'EiB' = 6,
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bytes to best human readable unit and number of that unit.
|
||||
*
|
||||
@@ -8,23 +18,10 @@
|
||||
* @param maxPrecision maximum number of decimal places, default is `1`
|
||||
* @returns size (number) and unit (string)
|
||||
*/
|
||||
export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, string] {
|
||||
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
|
||||
export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, ByteUnit] {
|
||||
const magnitude = Math.floor(Math.log(bytes === 0 ? 1 : bytes) / Math.log(1024));
|
||||
|
||||
let magnitude = 0;
|
||||
let remainder = bytes;
|
||||
while (remainder >= 1024) {
|
||||
if (magnitude + 1 < units.length) {
|
||||
magnitude++;
|
||||
remainder /= 1024;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
remainder = Number.parseFloat(remainder.toFixed(maxPrecision));
|
||||
|
||||
return [remainder, units[magnitude]];
|
||||
return [Number.parseFloat((bytes / 1024 ** magnitude).toFixed(maxPrecision)), magnitude];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +35,33 @@ export function getBytesWithUnit(bytes: number, maxPrecision = 1): [number, stri
|
||||
* @param maxPrecision maximum number of decimal places, default is `1`
|
||||
* @returns localized bytes with unit as string
|
||||
*/
|
||||
export function asByteUnitString(bytes: number, locale?: string, maxPrecision = 1): string {
|
||||
export function getByteUnitString(bytes: number, locale?: string, maxPrecision = 1): string {
|
||||
const [size, unit] = getBytesWithUnit(bytes, maxPrecision);
|
||||
return `${size.toLocaleString(locale)} ${unit}`;
|
||||
return `${size.toLocaleString(locale)} ${ByteUnit[unit]}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to bytes from on a specified unit.
|
||||
*
|
||||
* * `1, 'GiB'`, returns `1073741824` bytes
|
||||
*
|
||||
* @param size value to be converted
|
||||
* @param unit unit to convert from
|
||||
* @returns bytes (number)
|
||||
*/
|
||||
export function convertToBytes(size: number, unit: ByteUnit): number {
|
||||
return size * 1024 ** unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from bytes to a specified unit.
|
||||
*
|
||||
* * `11073741824, 'GiB'`, returns `1` GiB
|
||||
*
|
||||
* @param bytes value to be converted
|
||||
* @param unit unit to convert to
|
||||
* @returns bytes (number)
|
||||
*/
|
||||
export function convertFromBytes(bytes: number, unit: ByteUnit): number {
|
||||
return bytes / 1024 ** unit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user