node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.06k stars 682 forks source link

How to stream media file to pkgcloud package. #410

Closed gowridev closed 3 years ago

gowridev commented 7 years ago

How to stream media file to pkgcloud package. Pkgcloud only accepts stream data. Could you please sare an example. How to formidable can used with pkgcloud (for media files)

tunnckoCore commented 7 years ago

@gowridev hi there.

Formidable already returns a streams (erm, sort of - it inherits EventEmitter and has some more methods, but yea i believe it would pass the isStream check). I'm not familiar with pkgcloud so what method you thinking to use?

var form = new formidable.IncomingForm({ some: 'options' })

form.once('error', console.log)
form.parse(req)
xc2 commented 7 years ago

@gowridev Monkey patch will help. An example to upload file to swift:

form.on('fileBegin', function (name, file) { 
  file.on('error', function(err) {
    form._error(err)
  })

  file.open = function () {
    const self = this

    this._writeStream = Swift.upload({
      container: 'private-tmp',
      remote: [require('path').basename(this.path), this.name].join('_'),
      contentType: this.type
    })

    this._writeStream.on('error', function(err) {
      self.emit('error', err)
    })

  }

  file.end = function (cb) {
    var self = this;
    if (self.hash) {
      self.hash = self.hash.digest('hex');
    }

    this._writeStream.on('success', function(pkgcloudFileInstance) {

      // do something to attach pkgcloud file instance properties to formidable file instance
      // 
      // and then
      self.emit('end')
      cb()
    })
    this._writeStream.end()
  }
})
GrosSacASac commented 3 years ago

Use fileWriteStreamHandler option,

see examples/store-files-on-s3.js