Question : file validation or image validation
Answered by : hazrat-umar-cgxu3rye35l7
if (Object.keys(files).length === 0) { errors.push({ errors: "Upload Image" }); } else { const extension = files.image.mimetype .split("/") .slice(-1)[0] .toLowerCase(); if (extension !== "jpg" && extension !== "jpeg" && extension !== "png") { errors.push({ errors: `${extension} is not a valid extension` }); } else { if (files.image.size >= 5242880) { errors.push({ errors: "Image size must be less then 5MB" }); } else { const newName = uuidv4() + "." + extension; const oldPath = files.image.filepath; const newPath = __dirname + "/../client/src/image/post/" + newName; fs.copyFile(oldPath, newPath, (error) => { if (!error) { console.log("Image Upload"); } }); } } }
Source : | Last Update : Sat, 24 Sep 22