Skip to content

Commit

Permalink
basic validation decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 29, 2024
1 parent fdd59ba commit 5c3bbd0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 17 additions & 0 deletions api/src/modules/import/decorators/xlsm-upload.decorator.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
);
}
17 changes: 14 additions & 3 deletions api/src/modules/import/import.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
return file;
async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise<any> {
const { buffer, ...rest } = file;
return rest;
}
}

0 comments on commit 5c3bbd0

Please sign in to comment.