For my image resize function, I'm using a slightly modified version of the example code by Firebase, https://github.com/firebase/functions-samples/blob/main/generate-thumbnail/functions/index.js. I'm running this code on MacOS
This is my code
export const generateThumbnail = functions.storage.object()
.onFinalize(async (object: ObjectMetadata) => {
const {name: filePath, contentType, metadata} = object;
const fileDir = path.dirname(filePath);
const fileExt = path.extname(filePath)
const fileName = path.basename(filePath, fileExt);
const thumbFilePath = path.normalize(path.join(fileDir, `${fileName}${THUMB_APPEND}${fileExt}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
// Prevent new thumbnails from being handled as well
if (fileName?.endsWith(THUMB_APPEND)) {
return
}
if (contentType === undefined || !contentType.startsWith("image/")) {
return functions.logger.log(`${contentType} is not an image`);
}
const bucket = admin.storage().bucket(object.bucket);
const file = bucket.file(filePath);
await mkdirp(tempLocalDir)
// this validation can be removed if we set the correct storage rules
await file.download({destination: tempLocalFile, validation: false});
functions.logger.log('The file has been downloaded to', tempLocalFile);
await spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {capture: ['stdout', 'stderr']});
functions.logger.log('Thumbnail created at', tempLocalThumbFile);
await bucket.upload(tempLocalThumbFile, {
destination: thumbFilePath, metadata: {
contentType: contentType,
}
});
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
});
The issue I'm having is that the following line of code is throwing an error, which seems like an issue with my tempLocalFile not being found
await file.download({destination: tempLocalFile, validation: false});
This is the error that pops up in my console
Error: ENOENT: no such file or directory, open '/var/folders/4m/cmtdw_0d5nd74dm8w4p84c0w0000gn/T/events/rOSNBCfkiaBjfKCqGfa9/photos/33DVh7IINlJrB2APr6v1GXXK4Su8/004b8f9e-36b3-4e7a-8856-e1fa6f3c8408/004b8f9e-36b3-4e7a-8856-e1fa6f3c8408.jpeg'