Viewing File: /home/ubuntu/efiexchange-node-base/src/helpers/upload.helper.ts
import config from "config";
import multer from "multer";
import path from "path";
export type UploadFields = {
name: string;
maxCount: number;
};
export type UploadFileResponse = {
fieldname: string;
originalname: string;
encoding: string;
mimetype: string;
destination: string;
filename: string;
path: string;
size: number;
};
const storage = multer.diskStorage({
destination: config.get("PROJECT_DIR") + "public/uploads/",
filename: (req, file, cb) =>
cb(null, file.fieldname + "-" + Date.now() + ".jpg"),
});
const maxSize = 1 * 1000 * 1000;
const upload = multer({
storage,
limits: { fileSize: maxSize },
fileFilter: (req, file, cb) => {
const filetypes = /jpeg|jpg|png/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(
path.extname(file.originalname).toLowerCase()
);
if (mimetype && extname) {
return cb(null, true);
}
cb(
new Error(
"Error: File upload only supports the following filetypes - " +
filetypes
)
);
},
});
export const uploadImageRequest = (field: string) => {
return upload.single(field);
};
export const uploadImagesRequest = (fields: UploadFields[]) => {
return upload.fields(fields);
};
Back to Directory
File Manager