Skip to content

Commit

Permalink
Merge pull request #386 from boostcampwm2023/BE-Refactor-#380
Browse files Browse the repository at this point in the history
[BE/#380] 파일 용량 제한 로직 수정
  • Loading branch information
namewhat99 authored Dec 7, 2023
2 parents c5c00d0 + f1c4c58 commit 9e57370
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { MultiPartBody } from '../utils/multiPartBody.decorator';
import { PostListDto } from './dto/postList.dto';
import { AuthGuard } from 'src/utils/auth.guard';
import { UserHash } from 'src/utils/auth.decorator';
import { FileSizeValidator } from '../utils/files.validator';
import { FilesSizeValidator } from '../utils/files.validator';

@Controller('posts')
@ApiTags('posts')
Expand All @@ -41,7 +41,7 @@ export class PostController {
@Post()
@UseInterceptors(FilesInterceptor('image', 12))
async postsCreate(
@UploadedFiles(new FileSizeValidator())
@UploadedFiles(new FilesSizeValidator())
files: Array<Express.Multer.File>,
@MultiPartBody(
'post_info',
Expand All @@ -68,7 +68,7 @@ export class PostController {
@UseInterceptors(FilesInterceptor('image', 12))
async postModify(
@Param('id') id: number,
@UploadedFiles(new FileSizeValidator())
@UploadedFiles(new FilesSizeValidator())
files: Array<Express.Multer.File>,
@MultiPartBody(
'post_info',
Expand Down
6 changes: 1 addition & 5 deletions BE/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export class UsersController {
@Post()
@UseInterceptors(FileInterceptor('profileImage'))
async usersCreate(
@UploadedFile(
new ParseFilePipe({
validators: [new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 20 })],
}),
)
@UploadedFile(new FileSizeValidator())
file: Express.Multer.File,
@MultiPartBody(
'profile',
Expand Down
22 changes: 19 additions & 3 deletions BE/src/utils/files.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from '@nestjs/common';

@Injectable()
export class FileSizeValidator implements PipeTransform {
export class FilesSizeValidator implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata): any {
if (value === undefined || value.length === 0) {
if (value.length === 0) {
return value;
}
const maxSize = 1024 * 1024 * 20;
Expand All @@ -20,6 +20,22 @@ export class FileSizeValidator implements PipeTransform {
);
}
}
return undefined;
return value;
}
}

export class FileSizeValidator implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata): any {
if (value === undefined) {
return value;
}
const maxSize = 1024 * 1024 * 20;
const file: Express.Multer.File = value;
if (file.size > maxSize) {
throw new BadRequestException(
`File ${file.originalname} exceeds the allowed size limit`,
);
}
return value;
}
}

0 comments on commit 9e57370

Please sign in to comment.