From eecf68948d866d99260cf00b6e4002834020f6e0 Mon Sep 17 00:00:00 2001 From: hyohyo12 <129946082+hyohyo12@users.noreply.github.com> Date: Wed, 13 Nov 2024 01:36:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20PreSignedPostRequest=20=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=82=AC=EC=9A=A9=ED=95=A0=20IsImageFile=20?= =?UTF-8?q?=EB=8D=B0=EC=BD=94=EB=A0=88=EC=9D=B4=ED=84=B0=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1/=20=ED=95=B4=EB=8B=B9=20=EB=8D=B0=EC=BD=94=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20extension=20=EC=97=90=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC/?= =?UTF-8?q?=20=ED=95=A8=EC=88=98=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=20#88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/storage/dto/PreSignedPostRequest.ts | 2 ++ backend/src/storage/storage.constants.ts | 18 +++++++++++++ backend/src/storage/storage.controller.ts | 4 +-- backend/src/storage/storage.validator.ts | 25 +++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 backend/src/storage/storage.constants.ts create mode 100644 backend/src/storage/storage.validator.ts diff --git a/backend/src/storage/dto/PreSignedPostRequest.ts b/backend/src/storage/dto/PreSignedPostRequest.ts index e7982d77..b336bea7 100644 --- a/backend/src/storage/dto/PreSignedPostRequest.ts +++ b/backend/src/storage/dto/PreSignedPostRequest.ts @@ -1,4 +1,5 @@ import { IsNotEmpty, IsString } from 'class-validator'; +import { IsImageFile } from '../storage.validator'; export class PreSignedPostRequest { @IsString() @@ -7,5 +8,6 @@ export class PreSignedPostRequest { @IsString() @IsNotEmpty() + @IsImageFile() extension: string; } diff --git a/backend/src/storage/storage.constants.ts b/backend/src/storage/storage.constants.ts new file mode 100644 index 00000000..9cd1cf9c --- /dev/null +++ b/backend/src/storage/storage.constants.ts @@ -0,0 +1,18 @@ +export const IMAGE_EXTENSIONS = new Set([ + 'jpg', + 'jpeg', + 'png', + 'gif', + 'bmp', + 'tiff', + 'tif', + 'webp', + 'svg', + 'heic', + 'raw', + 'cr2', + 'nef', + 'arw', + 'dng', + 'ico', +]); diff --git a/backend/src/storage/storage.controller.ts b/backend/src/storage/storage.controller.ts index c5340a20..8ea2f7de 100644 --- a/backend/src/storage/storage.controller.ts +++ b/backend/src/storage/storage.controller.ts @@ -11,9 +11,7 @@ export class StorageController { @Throttle({ default: { limit: 10, ttl: 60000 } }) @Post('/preSignedPost') @UseGuards(JwtAuthGuard) - async getPreSignedPostUrl( - @Body() preSignedPostRequest: PreSignedPostRequest, - ) { + async getPreSignedPost(@Body() preSignedPostRequest: PreSignedPostRequest) { const { dirName, extension } = preSignedPostRequest; return await this.storageService.generatePreSignedPost(dirName, extension); } diff --git a/backend/src/storage/storage.validator.ts b/backend/src/storage/storage.validator.ts new file mode 100644 index 00000000..0473c2bf --- /dev/null +++ b/backend/src/storage/storage.validator.ts @@ -0,0 +1,25 @@ +import { + registerDecorator, + ValidationArguments, + ValidatorOptions, +} from 'class-validator'; +import { IMAGE_EXTENSIONS } from './storage.constants'; + +export function IsImageFile(validationOptions?: ValidatorOptions) { + return function (object: object, propertyName: string) { + registerDecorator({ + name: 'IsImageFile', + target: object.constructor, + propertyName: propertyName, + options: validationOptions, + validator: { + validate(value: any) { + return IMAGE_EXTENSIONS.has(value); + }, + defaultMessage(validationArguments?: ValidationArguments): string { + return `${validationArguments.value} 는 이미지 확장자가 아닙니다.`; + }, + }, + }); + }; +}