From e9dc11ab70bc0d4a53cbfd8404d6972763e3a05f Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Fri, 22 Apr 2022 17:34:41 +0200 Subject: [PATCH] make extension optional for image request --- .../decorators/image-id/image-full-id.pipe.ts | 32 ++++++++++++------- .../models/constants/image-full-id.const.ts | 10 +++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/backend/src/decorators/image-id/image-full-id.pipe.ts b/backend/src/decorators/image-id/image-full-id.pipe.ts index f0f0e5e..631b13a 100644 --- a/backend/src/decorators/image-id/image-full-id.pipe.ts +++ b/backend/src/decorators/image-id/image-full-id.pipe.ts @@ -12,18 +12,26 @@ import { ImageFullId } from '../../models/constants/image-full-id.const'; export class ImageFullIdPipe implements PipeTransform { 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 }; + } } } diff --git a/backend/src/models/constants/image-full-id.const.ts b/backend/src/models/constants/image-full-id.const.ts index b5f7615..b7d6558 100644 --- a/backend/src/models/constants/image-full-id.const.ts +++ b/backend/src/models/constants/image-full-id.const.ts @@ -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;