Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make file upload compatible with Busboy and express-fileupload #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions lib/expressroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ function defaultAccessor(dataLoc) {
};
}

function pullFile(req, key) {
if (req.file) {
return req.file;
}
if (Array.isArray(req.files)) {
for (var i = 0; i < req.files.length; ++i) {
if (req.files[i].fieldname === key) {
return req.files[i];
}
}
}
if (req.files) {
return req.files[key];
}
return null;
}

function valueAccessor(param, consumes) {
if (param.in === 'path') {
return defaultAccessor('params');
Expand Down Expand Up @@ -53,23 +70,26 @@ function valueAccessor(param, consumes) {
if (param.in === 'formData') {
return {
get: function(req, key) {
var file = req.file || Array.isArray(req.files) && req.files[0];
if (param.type === 'file' &&
!thing.isNullOrUndefined(file.fieldname) &&
file.fieldname === key) {
if (param.type === 'file') {
var file = pullFile(req, key);

if (file.buffer) {
if(!file) {
return undefined;
}

if(file.buffer || file.data) {
// when using InMemory option you get back a raw Buffer
// convert to binary string so that validator does not fail
// based on type.
return file.buffer.toString('binary');
return (file.buffer || file.data).toString('binary');
}

return file.path;
}
return req.body[key];
},
set: function(req, key, val) {
req.body[key] = param.type === 'file' ? val.value : val;
req.body[key] = param.type === 'file' ? (val && val.value || val) : val;
}
};
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "swaggerize-express",
"version": "4.0.6",
"version": "4.1.0",
"author": "Trevor Livingston <[email protected]>",
"contributors": [
"Trevor Livingston <[email protected]>",
"Subeesh Chothendavida <[email protected]>"
"Subeesh Chothendavida <[email protected]>",
"Adam McCormick <[email protected]>"
],
"description": "Design-driven apis with swagger 2.0 and express.",
"keywords": [
Expand All @@ -24,9 +25,9 @@
"main": "./lib/index",
"repository": {
"type": "git",
"url": "git://github.com/krakenjs/swaggerize-express.git"
"url": "git://github.com/SinclairDigital/swaggerize-express.git"
},
"bugs": "http://github.com/krakenjs/swaggerize-express/issues",
"bugs": "http://github.com/SinclairDigital/swaggerize-express/issues",
"engines": {
"node": "<=4.x"
},
Expand Down