m4nuC / async-busboy

Promise based multipart form parser for KoaJS
MIT License
167 stars 58 forks source link

Can I do "file.pip(stream)" like the way co-busboy does? #8

Closed caaatisgood closed 7 years ago

caaatisgood commented 7 years ago

Hi,

After the server receive the formdata and have been successfully parsed by async-busboy you built. I need to save the file to some other directory. How can I make it?

The reason why I didn't use co-busboy is that it act like a middleware. But I expect the saving operations can be processed in a specific route.

Here's the co-busboy example:

var parse = require('co-busboy')

app.use(function* (next) {
  // the body isn't multipart, so busboy can't parse it
  if (!this.request.is('multipart/*')) return yield next

  var parts = parse(this)
  var part
  while (part = yield parts) {
    if (part.length) {
      // arrays are busboy fields
      console.log('key: ' + part[0])
      console.log('value: ' + part[1])
    } else {
      // otherwise, it's a stream
      part.pipe(fs.createWriteStream('some file.txt'))
    }
  }
  console.log('and we are done parsing the form!')
})

What I expect:

const router = require('koa-router)()
router.post('/api/upload', async (ctx, next) => {
  const { fields, files } = await asyncBusboy(ctx.req)
  files.map(file => {
    // saving file
  }
  ...
}

(By printing out the file variable in the map function above and the part variable in the while loop. I notice that file is kind of like a ReadStream object while part is a FileStream object. )

Sorry about asking a question that is not fully related to your repo. I've tried some other parser and methods but it seems like it still cannot work properly. Could you give me some advice about how to solve this problem?

Much appreciated

m4nuC commented 7 years ago

Hi,

Sure you can just pipe() files to whatever stream you wish. The reason files are ReadStreams and not FileStreams is because async-busboy is caching files to disk.

So you can just:

  files.map(file => {
     file.pipe(fs.createWriteStream('output.txt'))
  }
caaatisgood commented 7 years ago

Hi,

So apparently, I've got typo in my code. Thanks for your reply! Now everything works well.

You mentioned that async-busboy is chaching files to disk. Is it because you want to make sure that the files in the request can be read with no error occurs?

m4nuC commented 7 years ago

Ok good, closing this issue then. We write the files to disk so that we can "buffer" them and make them available all at once.

caaatisgood commented 7 years ago

Got it, thanks a ton.