0

I have the following index.js that's triggered when a thumbnail is generated in cloud storage. It seems to work fine.

I'd like to replace the console.log line with code that adds a field like {"thumbnail_done":true} to the firebase document docid found in the script. I'm not clear on how to do that.

exports.thumbComplete = (event, context) => {
  const fname = event.name;
  let suffix = "_200x200.jpeg"
  if (fname.endsWith(suffix)) {
    let docid = fname.substring(0, fname.length - suffix.length);
    console.log(`thumbnail processing complete: ${docid}`);
  }
};

Thanks!

  • Did you see https://stackoverflow.com/questions/71983842/is-there-a-way-to-get-a-feedback-from-the-firebase-extension-resize-images-onc and https://firebase.google.com/docs/functions/gcp-storage-events? – Frank van Puffelen Sep 28 '22 at 19:10
  • @FrankvanPuffelen Thank you, yes I started with that google.com docs page and created an index.js that was just their example (functions.storage.object().onFinalize) but the cloud console reported errors so I was looking for a complete index.js sample before facing the pain of actually trying to understand this well enough to debug :-) – barcolounger Sep 28 '22 at 21:39

1 Answers1

0

Got it working with the following:

// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {
    const fname = object.name;
    let suffix = "_200x200.jpeg"
    if (fname.endsWith(suffix)) {
        let docid = fname.substring(0, fname.length - suffix.length);
        await admin.firestore().doc(`photo/${docid}`).update({ 'uploaded': true });
    }
});