I'm using sharp to resize an uploaded image in a Node.js
/ Express
application written in Typescript
. After successful resizing, I'd like to delete the original file. For png
and gif
input images, the operation terminates successfully, I have the resized image and the original is deleted. For jpg
and tif
images, the resize is successful, however the unlink
command fails with the following error:
EBUSY: resource busy or locked, unlink '...'
as if the sharp().resize()
would still keep the input file locked, even after the completion of the resize operation.
Here is the code to test the described behavior:
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));
}
What am I missing?