4
votes

When uploading a converted image (.jpg) to the google storage through Firebase Functions (Node) I set the contentType in the metadata options.

return bucket.file(filePath).download({destination: tempFilePath})
  .then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath])
    })
  .then(() => {
    console.log('Thumbnail created at', tempFilePath);
    // Add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
    const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`);
    // Uploading the thumbnail.

    return bucket.upload(tempFilePath, {  destination: thumbFilePath,
                                          metadata: {
                                              metadata: {
                                                contentType: 'image/jpeg',
                                                firebaseStorageDownloadTokens: uuid
                                              }
                                          }
                                        });

When i then view the file in Firebase storage console, the file Type is however set to the default application/octet-stream. When checking the metadata of the image it does say contentType: 'img/jpeg' in "Other metaData".

screenshot

What is going wrong here?

1

1 Answers

3
votes

I see you are using 'metadata' twice. Try this instead:

return bucket.upload(tempFilePath, {
    destination: thumbFilePath,
    metadata: {
        contentType: 'image/jpeg',
        firebaseStorageDownloadTokens: uuid
    }
});