I'm using express-fileupload
for reading files from the API. Now I want to process the image in the request body using Sharp
.
I don't want to first save the file at the server and process it using fs.readFileSync
.
I tried passing req.files.image.data
which is supposed to be a buffer.
const image = await sharp(Buffer.from(req.files.image.data))
.resize(500, 500)
.jpeg({ quality: 10 })
.toBuffer()
.then((outputBuffer) =>
({ data: outputBuffer, mimetype: 'image/jpeg' }))
.catch(err => {
console.log(err);
return null;
});
But it is throwing error this error: [Error: VipsJpeg: Premature end of input file]
When I tried converting the image buffer data into string as suggested in this post, converting it into buffer using Buffer.from
and then passing it, it throwing error: [Error: Input buffer contains unsupported image format]
Edit: There was a limit on image size 5mb, that's why the images greater than that were not getting completely captured in the buffer, hence, this error.
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));