Skip to content

Commit 01cd3f0

Browse files
committed
fix: add max file size validator for user avatar
1 parent ddf28a1 commit 01cd3f0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Diff for: server/src/controllers/users.controller.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Body, Controller, Post, Patch, UseGuards, UseInterceptors, UploadedFile } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Post,
5+
Patch,
6+
UseGuards,
7+
UseInterceptors,
8+
UploadedFile,
9+
BadRequestException,
10+
} from '@nestjs/common';
211
import { Express } from 'express';
312
import { FileInterceptor } from '@nestjs/platform-express';
413
import { JwtAuthGuard } from 'src/modules/auth/jwt-auth.guard';
@@ -7,6 +16,8 @@ import { UsersService } from 'src/services/users.service';
716
import { User } from 'src/decorators/user.decorator';
817
import { UpdateUserDto } from '@dto/user.dto';
918

19+
const MAX_AVATAR_FILE_SIZE = 1024 * 1024 * 2; // 2MB
20+
1021
@Controller('users')
1122
export class UsersController {
1223
constructor(private usersService: UsersService) {}
@@ -27,6 +38,10 @@ export class UsersController {
2738
@UseGuards(JwtAuthGuard)
2839
@UseInterceptors(FileInterceptor('file'))
2940
async addAvatar(@User() user, @UploadedFile() file: Express.Multer.File) {
41+
// TODO: use ParseFilePipe to validate file size from nestjs v9
42+
if (file.size > MAX_AVATAR_FILE_SIZE) {
43+
throw new BadRequestException('File size is greater than 2MB');
44+
}
3045
return this.usersService.addAvatar(user.id, file.buffer, file.originalname);
3146
}
3247

0 commit comments

Comments
 (0)