diff --git a/README.md b/README.md index ae1c86c4..454d46c6 100644 --- a/README.md +++ b/README.md @@ -357,6 +357,10 @@ See it's defaults in [src/Formidable.js DEFAULT_OPTIONS](./src/Formidable.js) - `options.filename` **{function}** - default `undefined` Use it to control newFilename. Must return a string. Will be joined with options.uploadDir. +- `options.filter` **{function}** - default function that always returns true. + Use it to filter files before they are uploaded. Must return a boolean. + + #### `options.filename` **{function}** function (name, ext, part, form) -> string _**Note:** If this size of combined fields, or size of some file is exceeded, an @@ -372,6 +376,20 @@ form.bytesReceived; form.bytesExpected; ``` +#### `options.filter` **{function}** function ({name, originalFilename, mimetype}) -> boolean + +**Note:** use an outside variable to cancel all uploads upon the first error + +```js +const options { + filter: function ({name, originalFilename, mimetype}) { + // keep only images + return mimetype && mimetype.includes("image"); + } +}; +``` + + ### .parse(request, callback) Parses an incoming Node.js `request` containing form data. If `callback` is diff --git a/examples/with-http.js b/examples/with-http.js index d5d3c7b2..cf715c68 100644 --- a/examples/with-http.js +++ b/examples/with-http.js @@ -10,16 +10,21 @@ const server = http.createServer((req, res) => { multiples: true, uploadDir: `uploads`, keepExtensions: true, - filename(/*name, ext, part, form*/) { - /* name basename of the http originalFilename - ext with the dot ".txt" only if keepExtension is true - */ - // slugify to avoid invalid filenames - // substr to define a maximum length - // return `${slugify(name).${slugify(ext, separator: '')}`.substr(0, 100); - return 'yo.txt'; // or completly different name - // return 'z/yo.txt'; // subdirectory - }, + // filename(/*name, ext, part, form*/) { + // /* name basename of the http originalFilename + // ext with the dot ".txt" only if keepExtension is true + // */ + // // slugify to avoid invalid filenames + // // substr to define a maximum length + // // return `${slugify(name).${slugify(ext, separator: '')}`.substr(0, 100); + // return 'yo.txt'; // or completly different name + // // return 'z/yo.txt'; // subdirectory + // }, + filter: function ({name, originalFilename, mimetype}) { + // keep only images + return mimetype && mimetype.includes("image"); + } + }); form.parse(req, (err, fields, files) => { diff --git a/src/Formidable.js b/src/Formidable.js index 46b4a752..05427008 100644 --- a/src/Formidable.js +++ b/src/Formidable.js @@ -27,6 +27,9 @@ const DEFAULT_OPTIONS = { enabledPlugins: ['octetstream', 'querystring', 'multipart', 'json'], fileWriteStreamHandler: null, defaultInvalidName: 'invalid-name', + filter: function () { + return true; + }, }; const PersistentFile = require('./PersistentFile'); @@ -316,6 +319,10 @@ class IncomingForm extends EventEmitter { return; } + if (!this.options.filter(part)) { + return; + } + this._flushing += 1; const newFilename = this._getNewName(part);