mscdex / busboy

A streaming parser for HTML form data for node.js
MIT License
2.86k stars 213 forks source link

My upload image gets stuck on "finish" event. It doesn't upload image on Firebase Storage #208

Closed rades12340 closed 5 years ago

rades12340 commented 5 years ago

I'm having an issue with uploading an image on Firebase Storage with Firebase functions. It all works until it gets to the the finish event of BusBoy. I console logged some arbitral word in finish event, but it doesn't get called.

This is the code:

 exports.uploadImage = (req, res) => {
  const BusBoy = require('busboy')
  const path = require('path')
  const os = require('os')
  const fs = require('fs')

  const busboy = new BusBoy({ headers: req.headers })

  let imageFileName
  let imageToBeUploaded = {}

  busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
      return res.status(400).json({ error: 'Wrong file type submitted' })
    }
    const imageExtension = filename.split('.')[filename.split('.').length - 1]
    const imageFileName = `${Math.round(
      Math.random() * 10000000000000000
    )}.${imageExtension}`

    const filepath = path.join(os.tmpdir(), imageFileName)
    imageToBeUploaded = { filepath, mimetype }
    file.pipe(fs.createWriteStream(filepath))
  })
  busboy.on('finish', () => {
    admin
      .storage()
      .bucket()
      .upload(imageToBeUploaded.filepath, {
        resumable: false,
        metadata: {
          metadata: {
            contentType: imageToBeUploaded.mimetype
          }
        }
      })
      .then(() => {
        const imageUrl = `https://storage.cloud.google.com/${
          config.storageBucket
        }/${imageFileName}?alt=media`
        console.log(imageUrl)
        return db.doc(`/users/${req.user.handle}`).update({ imageUrl })
      })
      .then(() => {
        return res.json({ message: 'Image uploaded successfully' })
      })
      .catch(err => {
        console.log(err)
        return res.status(500).json({ error: err.code })
      })
  })
  busboy.end(req.rawBody)
}

"busboy": "^0.3.1", "dotenv": "^8.0.0", "firebase": "^6.2.3", "firebase-admin": "^8.0.0", "firebase-functions": "^3.0.0"

Link to the GithHub repo: https://github.com/rades12340/socialape/blob/master/functions/handlers/users.js

mscdex commented 5 years ago

Is req.rawBody somehow missing the terminating boundary line?

Does the 'finish' event handler get called if your 'file' event handler simply only does file.resume()?

Neveon commented 5 years ago

I had to rollback firebase-tools.

In functions folder npm rm firebase-tools npm i -g firebase-tools@6.8.0 Then add config.storageBucket to the .bucket() in the 'finish' event handler

I can now sleep in peace knowing I defeated another bug

rades12340 commented 5 years ago

I had to rollback firebase-tools.

In functions folder npm rm firebase-tools npm i -g firebase-tools@6.8.0 Then add config.storageBucket to the .bucket() in the 'finish' event handler

I can now sleep in peace knowing I defeated another bug

This worked. Thanks. I couldn't imagine it was because of this.

soriasayer commented 4 years ago

There is no problem with code neither with firebase versions. Try to remove Content-Type from postman headers that is it.

umerali523 commented 3 years ago

There is no problem with code neither with firebase versions. Try to remove Content-Type from postman headers that is it.

Thank you soriasayer Issue resolved by removing headers