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