mirror of
https://github.com/hknsh/project-knedita.git
synced 2024-11-28 09:31:16 +00:00
refactor: upload image controller
This commit is contained in:
parent
c1528f02c3
commit
34944d9000
3 changed files with 35 additions and 24 deletions
14
src/users/schemas/upload-image.schema.ts
Normal file
14
src/users/schemas/upload-image.schema.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
const UploadImageSchema = {
|
||||
required: true,
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
image: {
|
||||
type: "string",
|
||||
format: "binary",
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default UploadImageSchema;
|
|
@ -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,
|
||||
|
|
17
src/validators/upload-image.validator.ts
Normal file
17
src/validators/upload-image.validator.ts
Normal file
|
@ -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;
|
Loading…
Reference in a new issue