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

Commit

Permalink
Updated the multipart requestBody logic to work with Multer 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger committed Mar 19, 2019
1 parent ab66226 commit b383385
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 91 deletions.
35 changes: 9 additions & 26 deletions lib/request-body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const _ = require("lodash");
const bodyParser = require("body-parser");
const multer = require("multer");
const tmp = require("tmp");
const util = require("./helpers/util");

// Clean-up the temp directory, even if the app crashes
tmp.setGracefulCleanup();
Expand All @@ -23,36 +22,25 @@ function requestBodyParser (options) {
options = _.merge({}, requestBodyParser.defaultOptions, options);

// Create a Multer uploader
let upload = multer(options.multipart);
let multipartParser = multer(options.multipart).any();

return [
bodyParser.json(options.json),
bodyParser.text(options.text),
bodyParser.urlencoded(options.urlencoded),
bodyParser.raw(options.raw),
multipartFormData,
parseMultipart,
];

/**
* Parses multipart/form-data
* Parses multipart request bodies
*/
function multipartFormData (req, res, next) {
if (util.isOpenApiRequest(req) && req.openapi.requestBody) {
let fileFields = [];

// Get all the "file" params
req.openapi.params.forEach((param) => {
if (param.in === "formData" && param.type === "file") {
fileFields.push({ name: param.name, maxCount: 1 });
}
});

// Handle the multipart/form-data (even if it doesn't have any file fields)
let parseFields = upload.fields(fileFields);
parseFields(req, res, next);
}

next();
function parseMultipart (req, res, next) {
multipartParser(req, res, (err) => {
// Group the files by fieldname, for easier access
req.files = _.groupBy(req.files, "fieldname");
next(err);
});
}
}

Expand All @@ -61,7 +49,6 @@ requestBodyParser.defaultOptions = {
* JSON body parser options
*
* @see https://github.com/expressjs/body-parser#bodyparserjsonoptions
*
*/
json: {
limit: "1mb",
Expand All @@ -72,7 +59,6 @@ requestBodyParser.defaultOptions = {
* Plain-text body parser options
*
* @see https://github.com/expressjs/body-parser#bodyparsertextoptions
*
*/
text: {
limit: "1mb",
Expand All @@ -83,7 +69,6 @@ requestBodyParser.defaultOptions = {
* URL-encoded body parser options
*
* @see https://github.com/expressjs/body-parser#bodyparserurlencodedoptions
*
*/
urlencoded: {
extended: true,
Expand All @@ -94,7 +79,6 @@ requestBodyParser.defaultOptions = {
* Raw body parser options
*
* @see https://github.com/expressjs/body-parser#bodyparserrawoptions
*
*/
raw: {
limit: "5mb",
Expand All @@ -105,7 +89,6 @@ requestBodyParser.defaultOptions = {
* Multipart form data parser options
*
* @see https://github.com/expressjs/multer#options
*
*/
multipart: {
// By default, use the system's temp directory, and clean-up when the app exits
Expand Down
104 changes: 39 additions & 65 deletions test/specs/request-body-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe("RequestBodyParser middleware", () => {
});
});

describe.skip("Multipart form data parser", () => {
describe("Multipart form data parser", () => {
it("should parse simple fields", (done) => {
createMiddleware(fixtures.data.petStore, (err, middleware) => {
let express = helper.express(middleware.parseRequest());
Expand All @@ -522,7 +522,7 @@ describe("RequestBodyParser middleware", () => {
expect(req.body).to.deep.equal({
foo: "bar",
biz: ["42", "43", "44"],
baz: ["B", "C", "A"],
baz: ["B", undefined, "C", undefined, undefined, "A"],
bob: {
name: "bob",
age: "42"
Expand All @@ -546,30 +546,26 @@ describe("RequestBodyParser middleware", () => {
express.post("/foo", helper.spy((req, res, next) => {
expect(req.body).to.deep.equal({});
expect(req.files).to.deep.equal({
file1: {
buffer: null,
file1: [{
encoding: "7bit",
extension: "jpg",
fieldname: "file1",
mimetype: "image/jpeg",
name: req.files.file1.name,
filename: req.files.file1[0].filename,
originalname: "1MB.jpg",
path: req.files.file1.path,
path: req.files.file1[0].path,
destination: req.files.file1[0].destination,
size: 683709,
truncated: false
},
file2: {
buffer: null,
}],
file2: [{
encoding: "7bit",
extension: "foobar",
fieldname: "file2",
mimetype: "image/jpeg",
name: req.files.file2.name,
filename: req.files.file2[0].filename,
originalname: "MyFile.foobar",
path: req.files.file2.path,
path: req.files.file2[0].path,
destination: req.files.file2[0].destination,
size: 683709,
truncated: false
}
}]
});
}));
});
Expand All @@ -584,52 +580,36 @@ describe("RequestBodyParser middleware", () => {
.set("Content-Type", "multipart/form-data")
.attach("file1", fixtures.paths.oneMB, "1MB.jpg")
.field("foo", "bar")
.field("biz", "42")
.field("biz", "43")
.field("biz", "44")
.attach("file2", fixtures.paths.oneMB, "MyFile.foobar")
.field("baz[5]", "A")
.field("baz[0]", "B")
.field("baz[2]", "C")
.field("bob[name]", "bob")
.field("bob[age]", "42")
.field("biz", "42")
.end(helper.checkSpyResults(done));

express.post("/foo", helper.spy((req, res, next) => {
expect(req.body).to.deep.equal({
foo: "bar",
biz: ["42", "43", "44"],
baz: ["B", "C", "A"],
bob: {
name: "bob",
age: "42"
}
biz: "42"
});
expect(req.files).to.deep.equal({
file1: {
buffer: null,
file1: [{
encoding: "7bit",
extension: "jpg",
fieldname: "file1",
mimetype: "image/jpeg",
name: req.files.file1.name,
filename: req.files.file1[0].filename,
originalname: "1MB.jpg",
path: req.files.file1.path,
path: req.files.file1[0].path,
destination: req.files.file1[0].destination,
size: 683709,
truncated: false
},
file2: {
buffer: null,
}],
file2: [{
encoding: "7bit",
extension: "foobar",
fieldname: "file2",
mimetype: "image/jpeg",
name: req.files.file2.name,
filename: req.files.file2[0].filename,
originalname: "MyFile.foobar",
path: req.files.file2.path,
path: req.files.file2[0].path,
destination: req.files.file2[0].destination,
size: 683709,
truncated: false
}
}]
});
}));
});
Expand All @@ -650,42 +630,36 @@ describe("RequestBodyParser middleware", () => {
express.post("/foo", helper.spy((req, res, next) => {
expect(req.body).to.deep.equal({});
expect(req.files).to.deep.equal({
file1: {
buffer: null,
file1: [{
encoding: "7bit",
extension: "jpg",
fieldname: "file1",
mimetype: "image/jpeg",
name: req.files.file1.name,
filename: req.files.file1[0].filename,
originalname: "1MB.jpg",
path: req.files.file1.path,
path: req.files.file1[0].path,
destination: req.files.file1[0].destination,
size: 683709,
truncated: false
},
file2: {
buffer: null,
}],
file2: [{
encoding: "7bit",
extension: "jpg",
fieldname: "file2",
mimetype: "image/jpeg",
name: req.files.file2.name,
filename: req.files.file2[0].filename,
originalname: "5MB.jpg",
path: req.files.file2.path,
path: req.files.file2[0].path,
destination: req.files.file2[0].destination,
size: 4573123,
truncated: false
},
file3: {
buffer: null,
}],
file3: [{
encoding: "7bit",
extension: "jpg",
fieldname: "file3",
mimetype: "image/jpeg",
name: req.files.file3.name,
filename: req.files.file3[0].filename,
originalname: "6MB.jpg",
path: req.files.file3.path,
path: req.files.file3[0].path,
destination: req.files.file3[0].destination,
size: 5595095,
truncated: false
}
}]
});
}));
});
Expand Down

0 comments on commit b383385

Please sign in to comment.