From d0b2f10c74fc9b8ef14aae77c99d09244215eee2 Mon Sep 17 00:00:00 2001 From: Nery Chucuy Date: Fri, 10 Jul 2020 21:45:53 -0600 Subject: [PATCH 1/2] Added limit file size example to Readme --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 471a6d4..2442f98 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,56 @@ http.createServer(function(req, res) { // Done parsing form! ``` +* Limit file size + +```javascript +var http = require('http'), + path = require('path'), + os = require('os'), + fs = require('fs'); + +var Busboy = require('busboy'); + +http.createServer(function(req, res) { + if (req.method === 'POST') { + var busboy = new Busboy({ headers: req.headers , limits: {fileSize : 1024 * 1024 }}); //fileSize is expressed in bytes. 1024*1024 = 1MB + + busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { + var saveTo = path.join(os.tmpdir(), path.basename(fieldname)); + const stream = fs.createWriteStream(saveTo); + file.pipe(stream); + + file.on('limit', function() { + res.writeHead(413, { 'Connection': 'close' }); + res.end(); + }); + + /* + Here is another use case: + + Stream close event will be triggered when file size limit is reached. Busboy's stream will contain a boolean property called truncated: + + stream.on('close', function() { + if (file.truncated){ + ... + } + }); + + */ + + }); + busboy.on('finish', function() { + res.writeHead(200, { 'Connection': 'close' }); + res.end("That's all folks!"); + }); + return req.pipe(busboy); + } + res.writeHead(404); + res.end(); +}).listen(8000, function() { + console.log('Listening for requests'); +}); +``` API === @@ -171,7 +221,7 @@ Busboy (special) events * **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream. * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits). - * If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens. + * If a configured file size limit was reached, `stream` will have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens. * **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found. From 94d0ead70f8fd926c1a03f6004a7cc5a66ed6b15 Mon Sep 17 00:00:00 2001 From: Nery Chucuy Date: Tue, 14 Jul 2020 22:06:54 -0600 Subject: [PATCH 2/2] Reverted incorrect correction --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2442f98..5b2a039 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ Busboy (special) events * **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream. * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits). - * If a configured file size limit was reached, `stream` will have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens. + * If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens. * **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.