Upload pictures at the front end. In egg, how do you get the original path of uploading pictures?

after reading the official tutorials and examples of egg, all you can get is the processed path. If the path is passed into sharp, it will report an error and cannot be compressed. Is there any way to get the original path of uploading pictures?

related codes

async upload () {

const { ctx } = this;
const file = ctx.request.files[0];
if (!file) return ctx.throw(404);

const filename = encodeURIComponent(ctx.request.body.name) + path.extname(file.filename).toLowerCase();
const targetPath = path.join(this.config.baseDir, "app/public", filename);
const source = fs.createReadStream(file.filepath);
const target = fs.createWriteStream(targetPath);


// await pump(source, target);
await sharp(target)
  // .rotate()
  .resize(100, 100)
  .toFile("/Users/lixiang/work/examples-master/multipart-file-mode/app/demo/"+ (Math.random() * 100000000).toFixed(0) +".png", err => {
      if (err) throw err;
      console.log("Completed!");
  })
  // .then( data => {
  //     console.log("")
  //     console.log(data)
  // })
  // .catch( err => {
  //     console.log(err)
  // } );
ctx.logger.warn("save %s to %s", file.filepath, targetPath);
// delete tmp file
await fs.unlink(file.filepath);

ctx.redirect("/public/" + filename);

}

what result do you expect? What is the error message actually seen?

Sep.24,2021

the parameter passed into sharp can be Buffer in addition to file path . So you don't need to take the original path .

and this original path
1: if it refers to the path to upload the user: c://xxxx/xxx.png , it's useless to pass it to sharp if you get it, because it can't be accessed.
2: if it is the location of the file, it should be the path of the file uploaded to the server and saved somewhere, but it is a bit superfluous, of course, if the original file is to be saved.

reply to comments (comment code is troublesome):
according to your code, you get stream , and then you convert stream to Buffer. There is a library on npm to do this, but you can also use this function.

function 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)));
   });
}

you can get buffer

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b33408-2b3c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b33408-2b3c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?