I have been making a server on node which can take image files and upload on disk after compressing it. However, image with smaller size are being compressed and image with large size like 10mb or so gives the following error.
{ Error: Error in file: ./uploads/ecqrwxgz6lw.jpg
write EOF
at _errnoException (util.js:1024:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:867:14)
code: 'EOF',
errno: 'EOF',
syscall: 'write',
killed: false,
stdout: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 ff fe 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0d 00 0f 01 02 00 12 00 00 00 ac 00 ... >,
stderr: <Buffer >,
failed: true,
signal: null,
cmd: 'D:\\Projects\\web-development\\Node.js\\Business\\node_modules\\mozjpeg\\vendor\\cjpeg.exe -quality 60',
timedOut: false }
My code for uploading the file by multer is
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
var rand = Math.random().toString(36).substr(2, 12);
req.rand = rand;
ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
callback(null, rand+ext);
}
});
var upload = multer({
storage: storage
}).single('userPhoto');
module.exports = {
upload: upload,
};
and this is my route for uploading the file
app.post('/events/upload', upload, function (req, res) {
var fb_id = req.user.id;
var email = req.user._json.email;
var random = req.rand;
console.log(fb_id);
var query = {
'Fbid': fb_id
},
update = {
$set: {
email: email
},
$push: {
events: {
event: place,
imageId: random
}
}
},
options = {
upsert: true
};
usersuploadImformation.findOneAndUpdate(query, update, options, function (err, data) {
if (err) {
console.log("Not able to update");
}
else{
console.log("Updated");
}
});
imagemin(['./uploads/'+random+'.{JPG,jpg}'], './uploads', {
plugins: [
imageminMozjpeg({
quality: 60,
progressive: true
})
]
}).then(files => {
console.log(files);
//=> [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …]
}).catch((err)=>{
console.log(err);
});
});
Just used another plugin for compression, this was the bug of the imageminmozjpeg. I used imageminJpegRecompress