1

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'

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • You may refer to a similar [Stackoverflow case](https://stackoverflow.com/questions/53289910/google-cloud-function-error-enoent-no-such-file-or-directory) which is also having the same issue. Let me know if that helps! – Mousumi Roy Dec 28 '21 at 09:14
  • Interesting! I didn't see that one, thanks, I will report back if it works. – Miguel Stevens Dec 28 '21 at 09:22

0 Answers0