I am using the multer to receive the files from FrontEnd and after I want to do a validation in this image to know if it's less than 1MB, because if not, I will return that image is big and must be less than 1MB and I want to make too a validation to accept only images type of .jpg, .jpeg and .png.
const multer = require("multer");
const path = require("path");
const shortid = require("shortid");
const diskStorage = multer.diskStorage({
destination: (req, file, cb) =>{
cb(null, 'imagens')
},
filename: (req, file, cb) =>{
cb(null, Date.now() + shortid.generate() + path.extname(file.originalname)); //o nome de como eu quero salvar o nome de cada imagem, tem que ser unico
}
});
module.exports = diskStorage;
After receive the datas from FrontEnd.
const diskStorage = require("../classes/uploadImagem");
const upload = multer({ storage: diskStorage }); //configurando o multer para dizer aonde vai salvar as imagens e como vai ficar o nome do arquivo
router.post("/criarconta", upload.fields([{ name: "photos" }]), async(req, res) =>{
var files = [];
if (req.files) {
files = req.files.photos;
//console.log(files);
}
let photos = []; //criei array de fotos com image path
if (files && files.length > 0) { //para saber se foram enviados as fotos pela requisição
files.forEach((photo, i) => {
photos[i] = photo.path; //salvando todas as fotos
});
}
if (files.length > 1) {
return res.status(400).json({ erro: "Não é permitido o upload de mais de 1 imagem!" });
}
return res.status(200).json({ erro: null, foto: photos });
});
Add the following verifications in the route handler:
const multer = require("multer");
const path = require("path");
const shortid = require("shortid");
const diskStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'imagens');
},
filename: (req, file, cb) => {
cb(null, Date.now() + shortid.generate() + path.extname(file.originalname));
}
});
const upload = multer({
storage: diskStorage,
limits: {
fileSize: 1 * 1024 * 1024 // 1Mb
},
fileFilter: (req, file, cb) => {
const acceptedExtensionsList = [".jpg", ".jpeg", ".png"];
const extname = path.extname(file.originalname).toLowerCase();
if (acceptedExtensionsList.includes(extname)) {
cb(null, true); // Accept the file
} else {
cb(new Error("Invalid file extension"));
}
}
});
// Error handler middleware for Multer errors
const errorHandler = (err, req, res, next) => {
if (err instanceof multer.MulterError) {
// Multer error occurred (e.g., file size limit exceeded)
return res.status(400).json({ error: "File upload error: " + err.message });
} else if (err.code === "LIMIT_FILE_SIZE") {
// File size limit exceeded
return res.status(400).json({ error: "File size limit exceeded. Max file size is 1MB." });
} else {
// Other errors
return res.status(500).json({ error: "Internal server error" });
}
};
router.post("/criarconta", upload.fields([{ name: "photos" }]), async (req, res) => {
var files = [];
if (req.files) {
files = req.files.photos;
}
if (files.length > 1) {
return res.status(400).json({ erro: "Não é permitido o upload de mais de 1 imagem!" });
}
let photos = [];
if (files && files.length > 0) {
files.forEach((photo, i) => {
photos[i] = photo.path;
});
}
return res.status(200).json({ erro: null, foto: photos });
});