Skip to content

Commit

Permalink
fix: add max file size validator for user avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
gondar00 committed Sep 23, 2022
1 parent ddf28a1 commit 01cd3f0
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion server/src/controllers/users.controller.ts
@@ -1,4 +1,13 @@
import { Body, Controller, Post, Patch, UseGuards, UseInterceptors, UploadedFile } from '@nestjs/common';
import {
Body,
Controller,
Post,
Patch,
UseGuards,
UseInterceptors,
UploadedFile,
BadRequestException,
} from '@nestjs/common';
import { Express } from 'express';
import { FileInterceptor } from '@nestjs/platform-express';
import { JwtAuthGuard } from 'src/modules/auth/jwt-auth.guard';
Expand All @@ -7,6 +16,8 @@ import { UsersService } from 'src/services/users.service';
import { User } from 'src/decorators/user.decorator';
import { UpdateUserDto } from '@dto/user.dto';

const MAX_AVATAR_FILE_SIZE = 1024 * 1024 * 2; // 2MB

@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
Expand All @@ -27,6 +38,10 @@ export class UsersController {
@UseGuards(JwtAuthGuard)
@UseInterceptors(FileInterceptor('file'))
async addAvatar(@User() user, @UploadedFile() file: Express.Multer.File) {
// TODO: use ParseFilePipe to validate file size from nestjs v9
if (file.size > MAX_AVATAR_FILE_SIZE) {
throw new BadRequestException('File size is greater than 2MB');
}
return this.usersService.addAvatar(user.id, file.buffer, file.originalname);
}

Expand Down

0 comments on commit 01cd3f0

Please sign in to comment.