If the picture is uploaded by egg, it is converted to buffer format.

upload an image from the front end, and at the back end, get the following temporary path:

/ var/folders/5y/3cm_1kws4r18wmx5n2l9ntzm0000gn/T/egg-multipart-tmp/multipart-file-mode-example/2018/10/29/14/5eb8f5b7-a1d5-4b6a-9cf0-4fcc80bab4f0.png

I want to send this image into sharp for compression and other processing, but this path sent an error in sharp,: the input file has a corrupted header, so can I transfer this temporary path to buffer format? If possible, if you convert this path to buffer format? I hope you can give me some advice.

Mar.16,2022

the stream passed from the front end is converted into buffer, and then a sharp object is instantiated with this buffer, and the server is saved by toFile (target path) after a series of operations.
stream to buffer use the following function

const streamToBuffer = stream => {
  return new Promise((resolve, reject) => {
    const buffers = [];
    stream.on('error', reject);
    stream.on('data', data => buffers.push(data));
    stream.on('end', () => resolve(Buffer.concat(buffers)));
  });
};

then call and instantiate sharp such as:

// stream  buffers
const buf = await streamToBuffer(stream);
// sharp
const originImg = sharp(buf);
Menu