I'm trying to process two different images with the same name (and this is not exchangeable) with cloud functions. The function is triggered to process only a image and the same happend for the second image. The problem is I'm not able to delete temporary files, so the second image cannot be saved because it has the same path and the same name. I use fs.unlinkSync()
to clear temp folder but this not works.
This is the code:
exports.thumb= functions.database.ref('/{uid}/upload')
.onUpdate(async (change, context) => {
const fileName = "RawImage.jpg";
const userId = context.auth.uid;
const bucket = storage.bucket("-----");
const workingDir = path.join(os.tmpdir(), "thumbs");
const tempFilePath = path.join(workingDir, fileName);
const filePath = path.join(userId,fileName);
await fs.ensureDir(workingDir);
await bucket.file(filePath).download({destination: tempFilePath});
const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(userId, thumbFileName);
const out = path.join(workingDir, thumbFileName);
const uploadPromises = async () => {
await sharp(tempFilePath)
.resize(300, 200)
.grayscale()
.toFile(out);
return await bucket.upload(out, {
destination: thumbFilePath,
});
}
const v = await uploadPromises();
return fs.unlinkSync(workingDir);
});
The last line is assigned to clear the working directory where temp files are stored but this not work (processing the second image, returns always the first image). I tried even to fs.unlincSync()
the sigle files but not works.