Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 037cd52

Browse files
committed
get base lib updates in
1 parent c0268ef commit 037cd52

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

lib/param-parser.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ function parseFormDataParams (req, res, next) {
5656

5757
if (param.type === "file") {
5858
// Validate the file (min/max size, etc.)
59-
req.files[param.name] = parseParameter(param, req.files[param.name], param);
59+
let file;
60+
if (req.files && req.files[param.name] && req.files[param.name][0]) {
61+
file = req.files[param.name][0];
62+
}
63+
const parsed = parseParameter(param, file, param);
64+
req.files[param.name] = parsed;
6065
}
6166
else {
6267
// Parse the body parameter

lib/request-parser.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,39 @@ const bodyParser = require("body-parser");
77
const cookieParser = require("cookie-parser");
88
const multer = require("multer");
99
const tmp = require("tmp");
10+
const util = require("./helpers/util");
1011

1112
// Clean-up the temp directory, even if the app crashes
1213
tmp.setGracefulCleanup();
1314

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+
1443
/**
1544
* Parses the HTTP request into useful objects.
1645
* This middleware populates {@link Request#params}, {@link Request#headers}, {@link Request#cookies},
@@ -29,37 +58,8 @@ function requestParser (options) {
2958
bodyParser.text(options.text),
3059
bodyParser.urlencoded(options.urlencoded),
3160
bodyParser.raw(options.raw),
32-
multer(options.multipart)
61+
generateMultipartFormDataMiddleware(multer(options.multipart))
3362
];
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-
// }
6363
}
6464

6565
requestParser.defaultOptions = {

0 commit comments

Comments
 (0)