From 0395bbd0cd6ced981d2ee53fe99f6ed454cc4acd Mon Sep 17 00:00:00 2001 From: "Haynes, Royce L" Date: Mon, 3 Mar 2014 08:36:55 -0700 Subject: [PATCH 1/4] [WIP] Added callback to handleUpload function, so a client can handle optionally handle response --- index.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index dc6774b..ccc5fd7 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,9 @@ var Framer = module.exports = function Framer(opts) { , prefix = opts.prefix || "" , authHandler = opts.authHandler; - return function (req, res) { + return function (req, res, callback) { + callback = callback === undefined ? null : callback; + var acceptHeader = req.headers.accept; var isTypeHtml = (/text\/html/).test(acceptHeader); var isTypeText = (/text\/text/).test(acceptHeader); @@ -87,14 +89,22 @@ var Framer = module.exports = function Framer(opts) { self._s3Client.putStream(part, destPath, headers, function (err, s3Response) { if (err) { - res.writeHead(500, {'content-type': 'application/json'}); - res.end(JSON.stringify({ statusCode: 500, error: err.toString() })); + if(callback){ + callback(err, s3Response); + } else { + res.writeHead(500, {'content-type': 'application/json'}); + res.end(JSON.stringify({ statusCode: 500, error: err.toString() })); + } return; } res.writeHead(res.statusCode, {'content-type': contentType}); if (s3Response.statusCode === 200) { - res.end(JSON.stringify({ statusCode: 200, uri: prefix + '/raw' + destPath, type: type })); + if(callback){ + callback(null, s3Response); + } else { + res.end(JSON.stringify({ statusCode: 200, uri: prefix + '/raw' + destPath, type: type })); + } } else { var bufs = []; @@ -102,7 +112,13 @@ var Framer = module.exports = function Framer(opts) { s3Response.on('end', function () { // just return the ugly xml body for now var body =Buffer.concat(bufs).toString(); - res.end(JSON.stringify({ statusCode: s3Response.statusCode, error: body })); + + if(callback){ + callback(null, s3Response); + }else { + res.end(JSON.stringify({ statusCode: s3Response.statusCode, error: body })); + } + }); } }); From db3c82a91bde1be584a7ca0c5f919b387c68db76 Mon Sep 17 00:00:00 2001 From: "Haynes, Royce L" Date: Mon, 3 Mar 2014 15:32:05 -0700 Subject: [PATCH 2/4] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e27046b..6e41ec4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "framer", - "version": "0.3.3", + "version": "0.3.4", "description": "A simple dynamic photo resizing http server intended to be behind an http cache that stores files in S3", "main": "index.js", "scripts": { From 45ae2f8852cdf2708f3971865b5ae4bd7c76f5e6 Mon Sep 17 00:00:00 2001 From: "Haynes, Royce L" Date: Tue, 4 Mar 2014 14:56:24 -0700 Subject: [PATCH 3/4] Added key value field to Amazon S3's response, WIP unit test --- index.js | 5 +++++ test/test.upload.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/index.js b/index.js index ccc5fd7..3fc9db8 100644 --- a/index.js +++ b/index.js @@ -101,6 +101,11 @@ var Framer = module.exports = function Framer(opts) { res.writeHead(res.statusCode, {'content-type': contentType}); if (s3Response.statusCode === 200) { if(callback){ + s3Response.custom_uri = { + uri: prefix + '/raw' + destPath, + type: type + }; + callback(null, s3Response); } else { res.end(JSON.stringify({ statusCode: 200, uri: prefix + '/raw' + destPath, type: type })); diff --git a/test/test.upload.js b/test/test.upload.js index 0008a8f..fced7b5 100644 --- a/test/test.upload.js +++ b/test/test.upload.js @@ -61,6 +61,40 @@ describe('test upload', function () { .form().append("filename", fs.createReadStream(path.join(__dirname, "image.jpg"))); }); + it('should take upload and execute callback function', function (done) { + + var framer = new Framer({ + s3: s3Options, + }); + framer._s3Client = s3Client; + var handleUpload = framer.handleUpload({ prefix: '/prefix' }); + + var PORT = Math.ceil(Math.random()*2000 + 1024); + var client = http.createServer(function (req, res) { + + handleUploadHandler = function(err, s3Response){ + if(err){ + res.writeHead(500, {'content-type': 'application/json'}); + res.end(JSON.stringify({ statusCode: 500, error: err.toString() })); + } else { + res.end(JSON.stringify({ statusCode: 200, uri: s3Response.custom_uri.uri, type: s3Response.custom_uri.stype })); + } + }; + + handleUpload(req, res, handleUploadHandler); + }).listen(PORT); + + request + .post('http://127.0.0.1:' + PORT + '/', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(200, res.statusCode); + assert(body.uri.indexOf('/prefix/') === 0, 'uri should be prefixed with the prefix option') + assert.equal('image/jpeg', body.type); + done(); + }) + .form().append("filename", fs.createReadStream(path.join(__dirname, "image.jpg"))); + }); + it('should delegate authoritation', function (done) { var authValue = 'foobarbaz'; From 1b77b0eecc96985da1e133be592010fefa753d71 Mon Sep 17 00:00:00 2001 From: "Haynes, Royce L" Date: Wed, 5 Mar 2014 09:46:28 -0700 Subject: [PATCH 4/4] Fixed typo in new unit test --- test/test.upload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.upload.js b/test/test.upload.js index fced7b5..150ee83 100644 --- a/test/test.upload.js +++ b/test/test.upload.js @@ -77,7 +77,7 @@ describe('test upload', function () { res.writeHead(500, {'content-type': 'application/json'}); res.end(JSON.stringify({ statusCode: 500, error: err.toString() })); } else { - res.end(JSON.stringify({ statusCode: 200, uri: s3Response.custom_uri.uri, type: s3Response.custom_uri.stype })); + res.end(JSON.stringify({ statusCode: 200, uri: s3Response.custom_uri.uri, type: s3Response.custom_uri.type })); } };