2

After using firebase functions and resizing an image in storage, I have a new image with something like "thum_" attached to the name for the new image.

I'm confused how to replace the original image's download url in database with this resized image so that users would load the resized version. If I don't change the name then I assume my function will be stuck in a loop of resizing because it is "thub_" prefix that prevents the function to go through another time as shown here:

....
//****USING THIS TO MAKE SURE FUNCTION DOESN'T GET INTO A LOOP
if (fileName.startsWith('thumb_')) {
  console.log('Already a Thumbnail; exit.');
  return;
}
....

I am following this sample.

How can I achieve this?

TheBen
  • 3,410
  • 3
  • 26
  • 51

1 Answers1

0

You don't need to overwrite the original, it's okay for the Cloud Functions to save a new image called thumb_.

Once you save the image Cloud Functions will attempt to process the resized image, come across that if statement if (fileName.startsWith('thumb_')) { and exit.

sketchthat
  • 2,678
  • 2
  • 13
  • 22
  • Thanks for the reply; I guess my question has not been clear enough. Question is how to access this newly created image in database since user has already uploaded the image and I have the original file's URL in database but I can't make a connection between database and this new file which has a new URL. – TheBen Jul 31 '17 at 06:27
  • Imagine a user uploads an image, in database I write that somewhere under a key and include bunch of params like url, sender, timeStamp, etc. The image in storage doesn't have any attribute to point me the location it's being referenced to from database so there's no way to update the database when thumbnail is created. Am I missing something? Thx – TheBen Jul 31 '17 at 06:30
  • I wrote the new download URL to db with the help of this post https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase it's a bit tricky to get the new url. Then you just need to write it to the database which, if you dont know how to do it, can be found in the firebase docs for example here https://firebase.google.com/docs/functions/get-started there is an example. – louisvno Jul 31 '17 at 08:08