diff --git a/src/users/schemas/upload-image.schema.ts b/src/users/schemas/upload-image.schema.ts new file mode 100644 index 0000000..3ec8f27 --- /dev/null +++ b/src/users/schemas/upload-image.schema.ts @@ -0,0 +1,14 @@ +const UploadImageSchema = { + required: true, + schema: { + type: "object", + properties: { + image: { + type: "string", + format: "binary", + }, + }, + }, +}; + +export default UploadImageSchema; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 3e772cb..593aea1 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -2,12 +2,9 @@ import { Body, Controller, Delete, - FileTypeValidator, Get, HttpCode, - MaxFileSizeValidator, Param, - ParseFilePipe, Patch, Post, Request, @@ -34,6 +31,8 @@ import { UpdateEmailDTO } from "./dto/update-email.dto"; import { UpdatePasswordDTO } from "./dto/update-password.dto"; import { File, FileInterceptor } from "@nest-lab/fastify-multer"; import { BufferValidator } from "src/validators/buffer-validator.pipe"; +import UploadImageSchema from "./schemas/upload-image.schema"; +import UploadImageValidator from "src/validators/upload-image.validator"; @ApiTags("Users") @Controller("users") @@ -110,29 +109,10 @@ export class UserController { @ApiBearerAuth("JWT") @UseInterceptors(FileInterceptor("image")) @ApiConsumes("multipart/form-data") - @ApiBody({ - required: true, - schema: { - type: "object", - properties: { - image: { - type: "string", - format: "binary", - }, - }, - }, - }) + @ApiBody(UploadImageSchema) uploadProfileImage( @UploadedFile( - new ParseFilePipe({ - validators: [ - new MaxFileSizeValidator({ - maxSize: 15 * 1024 * 1024, - message: "File too big. Max 1MB.", - }), - new FileTypeValidator({ fileType: /^image\/(jpeg|png|webp)$/ }), // File extension validation - ], - }), + UploadImageValidator, new BufferValidator(), // Magic number validation ) image: File, diff --git a/src/validators/upload-image.validator.ts b/src/validators/upload-image.validator.ts new file mode 100644 index 0000000..36004b0 --- /dev/null +++ b/src/validators/upload-image.validator.ts @@ -0,0 +1,17 @@ +import { + FileTypeValidator, + MaxFileSizeValidator, + ParseFilePipe, +} from "@nestjs/common"; + +const UploadImageValidator = new ParseFilePipe({ + validators: [ + new MaxFileSizeValidator({ + maxSize: 15 * 1024 * 1024, + message: "File too big. Max 1MB.", + }), + new FileTypeValidator({ fileType: /^image\/(jpeg|png|webp)$/ }), // File extension validation + ], +}); + +export default UploadImageValidator;