Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Storage file Type not set when uploading from Firebase Functions (Node)

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?

like image 970
louisvno Avatar asked Oct 18 '22 13:10

louisvno


1 Answers

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

return bucket.upload(tempFilePath, {
    destination: thumbFilePath,
    metadata: {
        contentType: 'image/jpeg',
        firebaseStorageDownloadTokens: uuid
    }
});
like image 161
Clinton Avatar answered Oct 21 '22 05:10

Clinton