From c8f1e8c3521c2573e11530e32597fd4365cec7aa Mon Sep 17 00:00:00 2001 From: Adam Woods-McCormick Date: Fri, 30 Nov 2018 15:43:18 -0800 Subject: [PATCH 1/3] Make file upload compatible with Busboy and express-fileupload --- lib/expressroutes.js | 34 +++++++++++++++++++++++++++------- package.json | 9 +++++---- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/expressroutes.js b/lib/expressroutes.js index a24ee68..3880cfe 100644 --- a/lib/expressroutes.js +++ b/lib/expressroutes.js @@ -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'); @@ -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.value || val) : val; } }; } diff --git a/package.json b/package.json index 293e5b0..814afcb 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "swaggerize-express", - "version": "4.0.6", + "version": "4.1.0", "author": "Trevor Livingston ", "contributors": [ "Trevor Livingston ", - "Subeesh Chothendavida " + "Subeesh Chothendavida ", + "Adam McCormick " ], "description": "Design-driven apis with swagger 2.0 and express.", "keywords": [ @@ -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" }, From 7546bfd3159a8b0a45a2bfbc3df51762fd54246c Mon Sep 17 00:00:00 2001 From: Adam Woods-McCormick Date: Fri, 30 Nov 2018 16:01:59 -0800 Subject: [PATCH 2/3] Correct for not-required files --- lib/expressroutes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/expressroutes.js b/lib/expressroutes.js index 3880cfe..aa34254 100644 --- a/lib/expressroutes.js +++ b/lib/expressroutes.js @@ -24,6 +24,7 @@ function defaultAccessor(dataLoc) { } function pullFile(req, key) { + console.log('pullFile.files', key,req) if (req.file) { return req.file; } @@ -35,6 +36,7 @@ function pullFile(req, key) { } } if (req.files) { + console.log('pullFile.files', key, Object.keys(req.files)) return req.files[key]; } return null; @@ -74,7 +76,8 @@ function valueAccessor(param, consumes) { var file = pullFile(req, key); if(!file) { - return undefined + console.log('valueAccessor:formData:get:undefined', key, file); + return undefined; } if(file.buffer || file.data) { @@ -89,7 +92,7 @@ function valueAccessor(param, consumes) { return req.body[key]; }, set: function(req, key, val) { - req.body[key] = param.type === 'file' ? (val.value || val) : val; + req.body[key] = param.type === 'file' ? (val && val.value || val) : val; } }; } From b8f83a83727541b750b1d62cc384c90b4a2019e3 Mon Sep 17 00:00:00 2001 From: Adam Woods-McCormick Date: Mon, 3 Dec 2018 16:09:35 -0800 Subject: [PATCH 3/3] Remove console messages --- lib/expressroutes.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/expressroutes.js b/lib/expressroutes.js index aa34254..8c0e57f 100644 --- a/lib/expressroutes.js +++ b/lib/expressroutes.js @@ -24,7 +24,6 @@ function defaultAccessor(dataLoc) { } function pullFile(req, key) { - console.log('pullFile.files', key,req) if (req.file) { return req.file; } @@ -36,7 +35,6 @@ function pullFile(req, key) { } } if (req.files) { - console.log('pullFile.files', key, Object.keys(req.files)) return req.files[key]; } return null; @@ -76,7 +74,6 @@ function valueAccessor(param, consumes) { var file = pullFile(req, key); if(!file) { - console.log('valueAccessor:formData:get:undefined', key, file); return undefined; }