After scaling and coloring a JPG with Sharp, I immediately delete the input file. When I upload a new file with the same name, Sharp will output the old file. I'm running NodeJS on Ubuntu 16.04.
Here is the code for editing the file:
sampleFile.mv(__dirname + "/" + name + "." + ext, function(err) {
if (err)
return res.status(500).send(err);
res.send('File uploaded!');
if (ext != "xlsx") {
// This will attempt to resize the images
console.log("Sharpening image")
sharp(__dirname + "/" + name + "." + ext).resize({ height: 27 }).flatten( { background: '#ffffff' } ).toFile("/var/www/my_ip/file.jpg")
.then(function(newFileInfo) {
// newFileInfo holds the output file properties
console.log("Success")
try {
fileSystem.unlinkSync(__dirname + "/" + name + "." + ext)
//file removed
} catch(err) {
console.error(err)
}
})
.catch(function(err) {
console.log("Error occured with file " + name + "." + ext + " | Dir: " + __dirname);
console.log(err)
try {
fileSystem.unlinkSync(__dirname + "/" + name + "." + ext)
//file removed
} catch(err) {
console.error(err)
}
//})
}); //this line errored
}
});
The first time I run it, it works just like intended, however if the file I use for the second run has the same name as the original file, it will somehow remember the old file and output that instead. I'm not sure how it remembers that file, as I immediately delete it. Any ideas on how to fix this?
Edit: To make sure the problem was not related to the first file not being properly deleted, I did a quick test. First, I use the server to edit 1 file, and I get that edited file as output, just as expected. Now, instead of editing a new file with the same name again, I restarted the server, then edited a new file with the same name. It now correctly outputted the new file. I believe the NPM must have some cache that I'm unaware of, currently reading the docs to so if my theory is correct.