@@ -7,10 +7,39 @@ const bodyParser = require("body-parser");
7
7
const cookieParser = require ( "cookie-parser" ) ;
8
8
const multer = require ( "multer" ) ;
9
9
const tmp = require ( "tmp" ) ;
10
+ const util = require ( "./helpers/util" ) ;
10
11
11
12
// Clean-up the temp directory, even if the app crashes
12
13
tmp . setGracefulCleanup ( ) ;
13
14
15
+ /**
16
+ * Generates a middleware that parses multipart/form-data
17
+ */
18
+ function generateMultipartFormDataMiddleware ( uploader ) {
19
+ return function multipartFormData ( req , res , next ) {
20
+ if ( util . isSwaggerRequest ( req ) && req . swagger . params . length > 0 ) {
21
+ let fileFields = [ ] ;
22
+
23
+ // Get all the "file" params
24
+ req . swagger . params . forEach ( ( param ) => {
25
+ if ( param . in === "formData" && param . type === "file" ) {
26
+ fileFields . push ( {
27
+ name : param . name ,
28
+ maxCount : 1
29
+ } ) ;
30
+ }
31
+ } ) ;
32
+
33
+ // Handle the multipart/form-data (even if it doesn't have any file fields)
34
+ let upload = uploader . fields ( fileFields ) ;
35
+ upload ( req , res , next ) ;
36
+ }
37
+ else {
38
+ next ( ) ;
39
+ }
40
+ } ;
41
+ }
42
+
14
43
/**
15
44
* Parses the HTTP request into useful objects.
16
45
* This middleware populates {@link Request#params}, {@link Request#headers}, {@link Request#cookies},
@@ -29,37 +58,8 @@ function requestParser (options) {
29
58
bodyParser . text ( options . text ) ,
30
59
bodyParser . urlencoded ( options . urlencoded ) ,
31
60
bodyParser . raw ( options . raw ) ,
32
- multer ( options . multipart )
61
+ generateMultipartFormDataMiddleware ( multer ( options . multipart ) )
33
62
] ;
34
-
35
- //
36
- // This code is for Multer 1.x. But we're still using Multer 0.x until this bug is fixed:
37
- // https://github.com/expressjs/multer/issues/212
38
- //
39
- // // Create a Multer uploader
40
- // let uploader = multer(options.multipart);
41
- //
42
- // /**
43
- // * Parses multipart/form-data
44
- // */
45
- // function multipartFormData(req, res, next) {
46
- // if (util.isSwaggerRequest(req) && req.swagger.params.length > 0) {
47
- // let fileFields = [];
48
- //
49
- // // Get all the "file" params
50
- // req.swagger.params.forEach(function(param) {
51
- // if (param.in === 'formData' && param.type === 'file') {
52
- // fileFields.push({name: param.name, maxCount: 1});
53
- // }
54
- // });
55
- //
56
- // // Handle the multipart/form-data (even if it doesn't have any file fields)
57
- // let upload = uploader.fields(fileFields);
58
- // upload(req, res, next);
59
- // }
60
- //
61
- // next();
62
- // }
63
63
}
64
64
65
65
requestParser . defaultOptions = {
0 commit comments