我正在使用SharpNode.js
来调整/Express
应用程序中上传的图像的大小Typescript
。成功调整大小后,我想删除原始文件。对于png
和gif
输入图像,操作成功终止,我有调整大小的图像,原始图像被删除。对于jpg
和tif
图像,调整大小是成功的,但是unlink
命令失败并出现以下错误:
EBUSY:资源繁忙或锁定,取消链接'...'
好像sharp().resize()
即使在完成调整大小操作后仍会保持输入文件锁定。
这是测试所描述行为的代码:
import { existsSync, unlinkSync } from "fs";
import { normalize, parse } from "path";
var argv = require("yargs").argv;
var sharp = require("sharp");
var appRoot = require("app-root-path") + "/";
let resizeTest = async function (filename: string): Promise<boolean> {
try {
let nameParts = parse(filename);
let source = appRoot + filename;
let destination = appRoot + nameParts.name + "_resized" + nameParts.ext;
let fileExists = await existsSync(source);
if (!fileExists) {
console.log("Input file not found. Exiting.");
return false;
}
let resizeResult = await sharp(source)
.resize(128, 128)
.toFile(destination);
console.log("Resize operation terminated: ", resizeResult);
await unlinkSync(source);
console.log("unlinkSync operation terminated.");
return true;
} catch (error) {
console.log("An error occured during resizeTest execution: ", error.message);
return false;
}
}
if (argv._.length === 0) {
console.log("Usage: node sharptest.js FILENAME");
} else {
let resizeResult: Promise<boolean> = resizeTest(argv._[0]);
resizeResult.then(result => console.log("Returning from execution with ", result));
}
我错过了什么?