You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by da...@apache.org on 2015/12/21 11:06:40 UTC
cordova-labs git commit: CB-10080 Rewrite file-transfer server not to
use disk cache
Repository: cordova-labs
Updated Branches:
refs/heads/cordova-filetransfer cf3dc1a46 -> 25493cbf0
CB-10080 Rewrite file-transfer server not to use disk cache
Used [busboy](https://github.com/mscdex/busboy) instead of formidable
Project: http://git-wip-us.apache.org/repos/asf/cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-labs/commit/25493cbf
Tree: http://git-wip-us.apache.org/repos/asf/cordova-labs/tree/25493cbf
Diff: http://git-wip-us.apache.org/repos/asf/cordova-labs/diff/25493cbf
Branch: refs/heads/cordova-filetransfer
Commit: 25493cbf0b16d6a6ced6a9ce4fbad5892ffb6b5f
Parents: cf3dc1a
Author: daserge <v-...@microsoft.com>
Authored: Mon Dec 21 13:00:44 2015 +0300
Committer: daserge <v-...@microsoft.com>
Committed: Mon Dec 21 13:00:44 2015 +0300
----------------------------------------------------------------------
package.json | 4 +--
server.js | 91 +++++++++++++++++++++++++++++++++++++------------------
2 files changed, 64 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/25493cbf/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index 25dbdfc..15cac76 100644
--- a/package.json
+++ b/package.json
@@ -9,8 +9,8 @@
"node": ">=0.10.0"
},
"dependencies": {
- "formidable": "1.0.x",
- "iconv": "2.1.x",
+ "busboy": "0.2.12",
+ "iconv": "2.1.11",
"json-stringify-safe": "5.0.0"
}
}
http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/25493cbf/server.js
----------------------------------------------------------------------
diff --git a/server.js b/server.js
index e4f5764..ba12998 100644
--- a/server.js
+++ b/server.js
@@ -1,8 +1,9 @@
-var formidable = require('formidable'),
- http = require('http'),
+var http = require('http'),
util = require('util'),
port = process.env.PORT || 5000;
- stringify = require('json-stringify-safe');
+ stringify = require('json-stringify-safe'),
+ Busboy = require('busboy'),
+ inspect = require('util').inspect;
var DIRECT_UPLOAD_LIMIT = 85; // bytes
@@ -11,6 +12,61 @@ var LATIN1_SYMBOLS = '¥§©ÆÖÑøøø¼';
var Iconv = require('iconv').Iconv;
var iconv = new Iconv('UTF-8', 'ISO-8859-1');
+function parseMultipartForm(req, res, finishCb) {
+ var fields = {}, files = {};
+ var errorOccured = false;
+
+ var busboy = new Busboy({ headers: req.headers });
+ busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
+ var currentFile = { size: 0 };
+ file.on('data', function(data) {
+ currentFile.name = filename;
+ currentFile.size += data.length;
+ });
+
+ file.on('end', function() {
+ files.file = currentFile;
+ });
+ });
+
+ busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
+ fields[fieldname] = val;
+ });
+
+ busboy.on('finish', function() {
+ console.log(stringify({fields: fields, files: files}));
+
+ // This is needed due to this bug: https://github.com/mscdex/busboy/issues/73
+ if (!errorOccured) {
+ finishCb(req, res, {fields: fields, files: files});
+ }
+ });
+
+ busboy.on('error', function(err) {
+ console.error('error: ' + err + ': ' + JSON.stringify(err));
+ errorOccured = true;
+
+ res.writeHead(400, {'Content-Type': 'text/plain'});
+ res.end("Could not parse multipart form: " + err + "\n");
+ });
+
+ req.pipe(busboy);
+}
+
+function respondWithParsedForm(req, res, parseResultObj) {
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ res.write(stringify(parseResultObj));
+ res.end("\n");
+}
+
+function respondWithParsedFormNonUTF(req, res, parseResultObj) {
+ parseResultObj["latin1Symbols"] = LATIN1_SYMBOLS;
+ var buffer = iconv.convert(stringify(parseResultObj));
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ res.write(buffer);
+ res.end("\n");
+}
+
http.createServer(function (req, res) {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
@@ -51,14 +107,7 @@ http.createServer(function (req, res) {
} else if (req.url == '/upload' && (req.method.toLowerCase() == 'post' || req.method.toLowerCase() == 'put')) {
if(req.headers["content-type"].indexOf("multipart/form-data") === 0) {
console.log("multipart/form upload");
- var form = new formidable.IncomingForm();
- form.parse(req, function(err, fields, files) {
- res.writeHead(200, {'content-type': 'text/plain'});
- console.log(stringify({fields: fields, files: files}));
-
- res.write(stringify({fields: fields, files: files}));
- res.end("\n");
- });
+ parseMultipartForm(req, res, respondWithParsedForm);
} else {
console.log("direct upload");
var body = '';
@@ -81,26 +130,10 @@ http.createServer(function (req, res) {
res.writeHead(401, {'Content-Type': 'text/plain'});
res.end("401\n");
} else {
- var form = new formidable.IncomingForm();
- form.parse(req, function(err, fields, files) {
- res.writeHead(200, {'content-type': 'text/plain'});
- console.log(stringify({fields: fields, files: files}));
-
- res.write(stringify({fields: fields, files: files}));
- res.end("\n");
- });
+ parseMultipartForm(req, res, respondWithParsedForm);
}
} else if (req.url == '/upload_non_utf' && req.method.toLowerCase() == 'post') {
- var form = new formidable.IncomingForm();
- form.parse(req, function(err, fields, files) {
- res.writeHead(200, {'content-type': 'text/plain'});
- console.log(stringify({fields: fields, files: files}));
-
- var buffer = iconv.convert(stringify({fields: fields, files: files, latin1Symbols: LATIN1_SYMBOLS}));
- res.write(buffer);
-
- res.end("\n");
- });
+ parseMultipartForm(req, res, respondWithParsedFormNonUTF);
} else if (req.url.match(/\d{3}/)) {
var matches = req.url.match(/\d{3}/);
status = matches[0];
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org