Skip to content

Commit

Permalink
Merge pull request #9 from mbrevoort/enhancement/handleUpload-callback
Browse files Browse the repository at this point in the history
Enhancement: handle upload callback
  • Loading branch information
roycehaynes committed Mar 5, 2014
2 parents de62a6c + 1b77b0e commit 47d0927
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -87,22 +89,41 @@ 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){
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 }));
}
}
else {
var bufs = [];
s3Response.on('data', function (d){ bufs.push(d); });
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 }));
}

});
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
34 changes: 34 additions & 0 deletions test/test.upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.type }));
}
};

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';
Expand Down

0 comments on commit 47d0927

Please sign in to comment.