-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
58 lines (50 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*!
* Connect - Multipart
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var utils = require('connect/utils'),
formidable = require('./support/formidable');
/**
* Setup multipart with the given `options`.
*
* Options:
*
* - `encoding` Encoding used for incoming forms. Defaults to utf8
* - `uploadDir` Directory to save uploads. Defaults to "/tmp"
* - `keepExtensions` Include original extensions. Defaults to `false`
*
* @param {Object} options
* @return {Function}
* @api public
*/
// TODO: urlencoded too
module.exports = function multipart(options){
options = options || {};
return function(req, res, next){
if (multipartRequest(req)) {
var form = req.form = new formidable.IncomingForm;
utils.merge(form, options);
form.onComplete = function(){};
next();
form.parse(req, form.onComplete);
} else {
next();
}
};
};
/**
* Check if `req` is a multipart request.
*
* @param {IncomingMessage} req
* @return {Boolean}
* @api private
*/
function multipartRequest(req){
return (req.method === 'POST'
|| req.method === 'PUT')
&& req.headers['content-type'].indexOf('multipart/form-data') >= 0;
}