make extension optional for image request

This commit is contained in:
rubikscraft
2022-04-22 17:34:41 +02:00
parent 47d6c26dcf
commit e9dc11ab70
2 changed files with 29 additions and 13 deletions

View File

@@ -12,18 +12,26 @@ import { ImageFullId } from '../../models/constants/image-full-id.const';
export class ImageFullIdPipe implements PipeTransform<string, ImageFullId> {
transform(value: string, metadata: ArgumentMetadata): ImageFullId {
const split = value.split('.');
if (split.length !== 2)
if (split.length === 2) {
const [id, ext] = split;
if (!UUIDRegex.test(id))
throw new BadRequestException('Invalid image identifier');
const mime = Ext2Mime(ext);
if (mime === undefined)
throw new BadRequestException('Invalid image identifier');
return { id, ext, mime };
} else if (split.length === 1) {
const [id] = split;
if (!UUIDRegex.test(id))
throw new BadRequestException('Invalid image identifier');
return { id, ext: null, mime: null };
} else {
throw new BadRequestException('Invalid image identifier');
const [id, ext] = split;
if (!UUIDRegex.test(id))
throw new BadRequestException('Invalid image identifier');
const mime = Ext2Mime(ext);
if (mime === undefined)
throw new BadRequestException('Invalid image identifier');
return { id, ext, mime };
}
}
}

View File

@@ -1,5 +1,13 @@
export interface ImageFullId {
interface NormalImage {
id: string;
ext: string;
mime: string;
}
interface OriginalImage {
id: string;
ext: null;
mime: null;
}
export type ImageFullId = NormalImage | OriginalImage;