2023-07-01 20:27:34 +02:00
|
|
|
import { applyDecorators } from '@nestjs/common';
|
|
|
|
import { ApiProperty } from '@nestjs/swagger';
|
2023-09-04 21:45:59 +02:00
|
|
|
import { IsArray, IsNotEmpty, IsOptional, IsString, IsUUID, ValidateIf, ValidationOptions } from 'class-validator';
|
2023-03-22 03:49:19 +01:00
|
|
|
import { basename, extname } from 'node:path';
|
2023-07-01 20:27:34 +02:00
|
|
|
import sanitize from 'sanitize-filename';
|
|
|
|
|
|
|
|
export type Options = {
|
|
|
|
optional?: boolean;
|
|
|
|
each?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function ValidateUUID({ optional, each }: Options = { optional: false, each: false }) {
|
|
|
|
return applyDecorators(
|
|
|
|
IsUUID('4', { each }),
|
|
|
|
ApiProperty({ format: 'uuid' }),
|
2023-09-01 18:40:00 +02:00
|
|
|
optional ? Optional() : IsNotEmpty(),
|
2023-07-01 20:27:34 +02:00
|
|
|
each ? IsArray() : IsString(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IValue {
|
|
|
|
value?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const toBoolean = ({ value }: IValue) => {
|
|
|
|
if (value == 'true') {
|
|
|
|
return true;
|
|
|
|
} else if (value == 'false') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const toEmail = ({ value }: IValue) => value?.toLowerCase();
|
|
|
|
|
|
|
|
export const toSanitized = ({ value }: IValue) => sanitize((value || '').replace(/\./g, ''));
|
2023-03-22 03:49:19 +01:00
|
|
|
|
|
|
|
export function getFileNameWithoutExtension(path: string): string {
|
|
|
|
return basename(path, extname(path));
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:04:11 +02:00
|
|
|
export function getLivePhotoMotionFilename(stillName: string, motionName: string) {
|
|
|
|
return getFileNameWithoutExtension(stillName) + extname(motionName);
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:39:27 +01:00
|
|
|
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);
|
2022-11-15 16:51:56 +01:00
|
|
|
|
2022-11-24 18:39:27 +01:00
|
|
|
export const HumanReadableSize = { KiB, MiB, GiB, TiB, PiB };
|
2022-11-15 16:51:56 +01:00
|
|
|
|
2022-11-24 18:39:27 +01:00
|
|
|
export function asHumanReadable(bytes: number, precision = 1): string {
|
|
|
|
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
|
2022-11-15 16:51:56 +01:00
|
|
|
|
2023-01-12 15:44:11 +01:00
|
|
|
let magnitude = 0;
|
|
|
|
let remainder = bytes;
|
|
|
|
while (remainder >= 1024) {
|
|
|
|
if (magnitude + 1 < units.length) {
|
|
|
|
magnitude++;
|
|
|
|
remainder /= 1024;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:51:56 +01:00
|
|
|
|
2023-01-12 15:44:11 +01:00
|
|
|
return `${remainder.toFixed(magnitude == 0 ? 0 : precision)} ${units[magnitude]}`;
|
2022-11-15 16:51:56 +01:00
|
|
|
}
|
2023-05-22 20:05:06 +02:00
|
|
|
|
|
|
|
export interface PaginationOptions {
|
|
|
|
take: number;
|
|
|
|
skip?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PaginationResult<T> {
|
|
|
|
items: T[];
|
|
|
|
hasNextPage: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Paginated<T> = Promise<PaginationResult<T>>;
|
|
|
|
|
|
|
|
export async function* usePagination<T>(
|
|
|
|
pageSize: number,
|
|
|
|
getNextPage: (pagination: PaginationOptions) => Paginated<T>,
|
|
|
|
) {
|
|
|
|
let hasNextPage = true;
|
|
|
|
|
|
|
|
for (let skip = 0; hasNextPage; skip += pageSize) {
|
|
|
|
const result = await getNextPage({ take: pageSize, skip });
|
|
|
|
hasNextPage = result.hasNextPage;
|
|
|
|
yield result.items;
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 18:40:00 +02:00
|
|
|
|
|
|
|
export interface OptionalOptions extends ValidationOptions {
|
|
|
|
nullable?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if value is missing and if so, ignores all validators.
|
|
|
|
*
|
|
|
|
* @param validationOptions {@link OptionalOptions}
|
|
|
|
*
|
|
|
|
* @see IsOptional exported from `class-validator.
|
|
|
|
*/
|
|
|
|
// https://stackoverflow.com/a/71353929
|
|
|
|
export function Optional({ nullable, ...validationOptions }: OptionalOptions = {}) {
|
|
|
|
if (nullable === true) {
|
|
|
|
return IsOptional(validationOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidateIf((obj: any, v: any) => v !== undefined, validationOptions);
|
|
|
|
}
|