From 5c3bbd0193bbf7010b88bcf5d7fdea7118d5551d Mon Sep 17 00:00:00 2001 From: alexeh Date: Sun, 29 Sep 2024 08:32:24 +0200 Subject: [PATCH] basic validation decorator --- .../import/decorators/xlsm-upload.decorator.ts | 17 +++++++++++++++++ api/src/modules/import/import.controller.ts | 17 ++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 api/src/modules/import/decorators/xlsm-upload.decorator.ts diff --git a/api/src/modules/import/decorators/xlsm-upload.decorator.ts b/api/src/modules/import/decorators/xlsm-upload.decorator.ts new file mode 100644 index 00000000..a18b093d --- /dev/null +++ b/api/src/modules/import/decorators/xlsm-upload.decorator.ts @@ -0,0 +1,17 @@ +import { UploadedFile, ParseFilePipeBuilder, HttpStatus } from '@nestjs/common'; + +export function UploadXlsm(maxSize: number = 5000000) { + return UploadedFile( + new ParseFilePipeBuilder() + .addFileTypeValidator({ + fileType: 'application/vnd.ms-excel.sheet.macroenabled.12', + }) + .addMaxSizeValidator({ + maxSize: maxSize, + }) + .build({ + errorHttpStatusCode: HttpStatus.BAD_REQUEST, + fileIsRequired: true, + }), + ); +} diff --git a/api/src/modules/import/import.controller.ts b/api/src/modules/import/import.controller.ts index ec1b0da6..8c78dc16 100644 --- a/api/src/modules/import/import.controller.ts +++ b/api/src/modules/import/import.controller.ts @@ -5,12 +5,23 @@ import { UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; +import { JwtAuthGuard } from '@api/modules/auth/guards/jwt-auth.guard'; +import { RolesGuard } from '@api/modules/auth/guards/roles.guard'; +import { RequiredRoles } from '@api/modules/auth/decorators/roles.decorator'; +import { ROLES } from '@api/modules/auth/roles.enum'; +import { UploadXlsm } from '@api/modules/import/decorators/xlsm-upload.decorator'; @Controller() +//@UseInterceptors(JwtAuthGuard, RolesGuard) export class ImportController { - @Post() + // TODO: File validation following: + // https://docs.nestjs.com/techniques/file-upload + + @Post('/admin/upload/xlsx') + //@RequiredRoles(ROLES.ADMIN) @UseInterceptors(FileInterceptor('file')) - async uploadFile(@UploadedFile() file: Express.Multer.File): Promise { - return file; + async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise { + const { buffer, ...rest } = file; + return rest; } }