0

so I am trying to add image moderation to my firebase app, using this guide here here is the repo and this also might be useful. I tested it out and it seems to work well, however I wanted to update the same image from storage, not upload it to a separate path. This caused the firebase function to fire recursively and I cost my company a couple of dollars. To solve this I decided whenever an image is uploaded there would be custom metadata saying if it has been blurred or not. The code for this is down below and works.

const metaData = {
           customMetadata: {
              blurred: 'false',
          },
      }
   
const imageBytes = await uploadBytesResumable(storageRef, blobFile, metaData);

Now in my firebase function every time an image is uploaded it will check the object.metaData to see if it is equal to 'true'. Here is the code down below for that.

export const blurOffensiveImages = functions.storage.object().onFinalize(async (object) => {
// Ignore things we've already blurred
if (object.metadata?.customMetadata.blurred === 'true') {
    functions.logger.log(`meta datas are the same, stopping function`);
    return null;
}

If the metadata is equal to false then it will run the normal function and check if the image is worth blurring. If it is then all it does is change the metadata to true since this will stop the recursion.

My problem, I am having type errors saying 'Property "blurred' does not exist on type 'string'". I have played around with this for a while and cant seem to find a solution to get the customMetaData to come out the way I would like. If i remove the .blurred I get no errors but I also know this would not give the correct data when I check if it is equal to 'true'or 'false'. If anyone knows how to fix this that would be really appreciated. OR if someone has a good solution to stop this recurson from happening that would also be great since this has been giving me issues for a few days. Thanks!

seth8656
  • 104
  • 5
  • Does this [gist](https://gist.github.com/katowulf/dca58cedace83f2f65e83a6c1402ba2e) solves your issue ? and also see this [thread](https://stackoverflow.com/questions/72683248/firebase-cloud-storage-upload-with-metadata) – Rohit Kharche Feb 10 '23 at 11:50

0 Answers0