Skip to content

Commit

Permalink
Merge pull request #102 from jihwooon/issue-67
Browse files Browse the repository at this point in the history
이미지 파일 업로드 및 업데이트 시 에러 케이스를 추가하라
  • Loading branch information
jihwooon authored Nov 9, 2023
2 parents c930ef8 + 3f5d202 commit 5f881f3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
13 changes: 2 additions & 11 deletions server/src/config/multer/multer.options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus } from '@nestjs/common'
import { BadRequestException } from '@nestjs/common'
import { diskStorage } from 'multer'
import { existsSync, mkdirSync } from 'fs'
import * as mime from 'mime-types'
Expand All @@ -25,16 +25,7 @@ export const multerDiskOptions = {
if (file.mimetype.match(/\/(jpg|jpeg|png)$/)) {
callback(null, true)
} else {
callback(
new HttpException(
{
message: 'NOT_MATCHED_IMAGE_FILE_FORMAT',
error: '지원하지 않는 이미지 형식입니다.',
},
HttpStatus.BAD_REQUEST,
),
false,
)
callback(new BadRequestException('지원하지 않는 이미지 형식입니다.'), false)
}
},
}
5 changes: 5 additions & 0 deletions server/src/item-images/application/item-image.creater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ItemImageRepository } from '../domain/item-image.repository'
import { ItemImage } from '../domain/item-image.entity'
import { ItemRepository } from '../../items/domain/item.repository'
import { ItemNotFoundException } from '../../items/error/item-not-found.exception'
import { FileIsNotEmpty } from '../error/file-is-not-empty'

@Injectable()
export class ItemImageCreater {
Expand All @@ -12,6 +13,10 @@ export class ItemImageCreater {
) {}

async saveItemImages(itemId: number, @UploadedFiles() files: Express.Multer.File[]) {
if (Array.isArray(files) && files.length === 0) {
throw new FileIsNotEmpty('파일은 필수 입력 값입니다')
}

files.map((file, i) => {
const isRepresentImage: boolean = i === 0

Expand Down
10 changes: 10 additions & 0 deletions server/src/item-images/error/file-is-not-empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BadRequestException } from '@nestjs/common'

export class FileIsNotEmpty extends BadRequestException {
constructor(message: string) {
super(message, {
cause: 'file',
description: 'FIlE_IS_NOT_EMPTY',
})
}
}

0 comments on commit 5f881f3

Please sign in to comment.