Skip to content

Commit

Permalink
feat: filter upload (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosSacASac committed Mar 18, 2021
1 parent 748f1db commit 8f94fba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 15 additions & 10 deletions examples/with-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
7 changes: 7 additions & 0 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -316,6 +319,10 @@ class IncomingForm extends EventEmitter {
return;
}

if (!this.options.filter(part)) {
return;
}

this._flushing += 1;

const newFilename = this._getNewName(part);
Expand Down

0 comments on commit 8f94fba

Please sign in to comment.