Using content-type in Firebase Cloud Functions

Here's an improvement for the “Generate Thumbnail” Cloud Function sample. This cloud function listens for new image uploads and adds a thumbnail file at the same location.

An issue I noticed with the generated thumbnail was that it had no contentType metadata parameter. Firebase provides this at the Content-Type header when the file is downloaded.

To add this, you can use event.data.contentType and provide a default value of application/octet-stream (the most generic type).

exports.generateThumbnail = functions.storage.object().onChange(event => { 
  // ...
    return bucket.upload(tempLocalThumbFile, {
        destination: thumbFilePath,
        metadata: { 
            contentType: event.data.contentType || 'application/octet-stream' 
        }
    }).then(() => {
  // ...

View the original sample code here

Have a lovely day.
© 2023, Graham Macphee.