m4nuC / async-busboy

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

How to "drain the request stream" #24

Closed karladler closed 7 years ago

karladler commented 7 years ago

Quoting the doc: "When the consumer stream drained the request stream, files will be automatically removed, otherwise the host OS should take care of the cleaning process."

What would be the best practice for reading the files e.g. to write them in a data base. I'm currently using fs.readFileSync(files[0].path) and deleting the file with fs.unlink() function.

Edit: Extending the question, because it's probably related: To get the file size one must use fs.stat(PATH) but that's only possible when you have the path already. But how to skip a file upload that exceeds a certain size?

m4nuC commented 7 years ago

What would be the best practice for reading the files e.g. to write them in a data base. I'm currently using fs.readFileSync(files[0].path) and deleting the file with fs.unlink() function.

This should be fine. Although you probably don't need to use fs.unlink(). The OS will do the clean up.

To get the file size one must use fs.stat(PATH) but that's only possible when you have the path already. But how to skip a file upload that exceeds a certain size?

busboy lets you set a fileSize limit. When calling async-busboy you pass as a second argument an option object that will be relayed to busboy:

const {files, fields} = await asyncBusboy(ctx.req, {limits: fileSize: 1000}});