Skip to content

Commit

Permalink
fix: multipart 관련 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
son-daehyeon committed Sep 14, 2024
1 parent c5011ff commit d7d112e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Controller, Post, UploadedFile } from '@nestjs/common';
import { Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { AuthAdminAccount, AuthAdminAccountException } from '@wink/auth/guard';

import { ImageFilter, ImageFilterException } from '@wink/activity/common/util/multer';
import { UploadResponseDto } from '@wink/activity/common/dto';
import { ActivityAdminService } from '@wink/activity/service';

Expand All @@ -15,9 +17,10 @@ export class ActivityAdminController {

@Post('/upload')
@AuthAdminAccount()
@ApiOperation({ summary: '업로드' })
@UseInterceptors(FileInterceptor('image', { fileFilter: ImageFilter }))
@ApiOperation({ summary: '사진 업로드' })
@ApiCustomResponse()
@ApiCustomErrorResponse([...AuthAdminAccountException])
@ApiCustomErrorResponse([...AuthAdminAccountException, ...ImageFilterException])
async upload(@UploadedFile() file: Express.Multer.File): Promise<UploadResponseDto> {
return this.activityAdminService.upload(file);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HttpStatus } from '@nestjs/common';

import { ApiException } from '@wink/swagger';

export class ImageInvalidMimeException extends ApiException {
constructor() {
super({
swagger: '사진 파일이 아닌 경우',
message: '사진 파일이 아닙니다.',
code: HttpStatus.BAD_REQUEST,
});
}
}
13 changes: 13 additions & 0 deletions src/domain/activity/common/exceptions/image-too-large.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HttpStatus } from '@nestjs/common';

import { ApiException } from '@wink/swagger';

export class ImageTooLargeException extends ApiException {
constructor() {
super({
swagger: '사진의 용량이 너무 큰 경우',
message: '사진의 용량은 10MB를 넘을 수 없습니다.',
code: HttpStatus.BAD_REQUEST,
});
}
}
2 changes: 2 additions & 0 deletions src/domain/activity/common/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './image-invalid-mime.exception';
export * from './image-too-large.exception';
24 changes: 24 additions & 0 deletions src/domain/activity/common/util/multer/image-filter.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ImageInvalidMimeException,
ImageTooLargeException,
} from '@wink/activity/common/exceptions';

const IMAGE_MAX_SIZE = 1024 * 1024 * 10;

export const ImageFilter = (
_req: Request,
file: Express.Multer.File,
callback: (error: Error | null, success: boolean) => void,
) => {
if (!file.mimetype.startsWith('image/')) {
return callback(new ImageInvalidMimeException(), false);
}

if (file.size > IMAGE_MAX_SIZE) {
return callback(new ImageTooLargeException(), false);
}

callback(null, true);
};

export const ImageFilterException = [ImageInvalidMimeException, ImageTooLargeException];
1 change: 1 addition & 0 deletions src/domain/activity/common/util/multer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './image-filter.util';
2 changes: 0 additions & 2 deletions src/domain/member/controller/member.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { AuthAccount, AuthAccountException, ReqMember } from '@wink/auth/guard';

import {
GetMembersResponseDto,
UpdateMyAvatarRequestDto,
UpdateMyAvatarResponseDto,
UpdateMyInfoRequestDto,
UpdateMyPasswordRequestDto,
Expand Down Expand Up @@ -70,7 +69,6 @@ export class MemberController {
@UseInterceptors(FileInterceptor('avatar', { fileFilter: AvatarFilter }))
@ApiOperation({ summary: '내 프로필 사진 수정' })
@ApiConsumes('multipart/form-data')
@ApiProperty({ type: UpdateMyAvatarRequestDto })
@ApiCustomResponse(UpdateMyAvatarResponseDto)
@ApiCustomErrorResponse([...AuthAccountException, ...AvatarFilterException])
async updateMyAvatar(
Expand Down
1 change: 0 additions & 1 deletion src/domain/member/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './request/approve-waiting-member.request.dto';
export * from './request/reject-waiting-member.request.dto';
export * from './request/update-member-fee.request.dto';
export * from './request/update-member-role.request.dto';
export * from './request/update-my-avatar.request.dto';
export * from './request/update-my-info.request.dto';
export * from './request/update-my-password.request.dto';

Expand Down
10 changes: 0 additions & 10 deletions src/domain/member/dto/request/update-my-avatar.request.dto.ts

This file was deleted.

0 comments on commit d7d112e

Please sign in to comment.