Skip to content

Commit

Permalink
feat: PreSignedPostRequest 에서 사용할 IsImageFile 데코레이터 생성/ 해당 데코레이터 exte…
Browse files Browse the repository at this point in the history
…nsion 에 추가하여 이미지 파일 유효성 검사/ 함수 이름 변경 #88
  • Loading branch information
hyohyo12 committed Nov 13, 2024
1 parent f203a99 commit 99377b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions backend/src/storage/dto/PreSignedPostRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { IsImageFile } from '../storage.validator';

export class PreSignedPostRequest {
@IsString()
Expand All @@ -7,5 +8,6 @@ export class PreSignedPostRequest {

@IsString()
@IsNotEmpty()
@IsImageFile()
extension: string;
}
18 changes: 18 additions & 0 deletions backend/src/storage/storage.constants.ts
Original file line number Diff line number Diff line change
@@ -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',
]);
4 changes: 1 addition & 3 deletions backend/src/storage/storage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 25 additions & 0 deletions backend/src/storage/storage.validator.ts
Original file line number Diff line number Diff line change
@@ -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} 는 이미지 확장자가 아닙니다.`;
},
},
});
};
}

0 comments on commit 99377b4

Please sign in to comment.