Merge branch 'master' of github.com:deployd/deployd

This commit is contained in:
Ritchie Martori
2012-03-27 09:35:33 -07:00
2 changed files with 64 additions and 27 deletions

View File

@@ -6,6 +6,7 @@ var storage = require('../storage')
, mime = require('./mime')
, path = require('path')
, Stream = require('stream').Stream
, IncomingForm = require('formidable').IncomingForm
, fs = require('fs')
;
@@ -21,7 +22,8 @@ module.exports = function (req, res, next, use) {
var filename = req.references && req.references[0]
, ext = filename && path.extname(filename)
, contentType = ext && mime[ext.split('.')[1]]
, mimeType = ext && mime[ext.split('.')[1]]
, contentType = req.headers['content-type'] || mimeType
;
// list files
@@ -29,24 +31,59 @@ module.exports = function (req, res, next, use) {
req.directory = true;
}
// only allow certain content types
// require a content type
if(filename && !contentType) {
return next({status: 404, error: 'A filename with a valid extension must be provided', valid: mime});
return next({status: 400, error: 'A filename with a valid extension and a proper Content-Type header must be provided', valid: mime});
}
// grab filename from references
filename && (req.query._id = filename);
// when writing, add req.file for storage engine
if(req.method === 'POST' || req.method === 'PUT') {
req.file = req;
if(contentType.match(/multipart/i)) {
// use formidabble's multipart parser
var form = new IncomingForm
, stream = new Stream
, first
;
// resume once storage is ready
stream.resume = function () {
// override default behavior (dont save files)
form.onPart = function (part) {
// only handle files
if(!first && part.filename) {
// store first file
first = part;
// proxy events
part
.on('data', function (data) {
stream.emit('data', data);
})
.on('end', function () {
stream.emit('end');
})
;
}
};
form.parse(req);
req.resume();
};
req.file = stream;
} else {
// otherwise stream the data directly into storage
req.file = req;
}
}
// execute the request against the storage engine
// TODO: should be able to proxy to storage
storage.exec(req, function (err, body) {
if(body && body.file) {
res.header('Content-Type', contentType);
res.header('Content-Type', mimeType);
res.header('Transfer-Encoding', 'chunked');
body.file.stream(true).pipe(res);

View File

@@ -13,13 +13,13 @@ describe('Static', function(){
})
})
// it('should only allow root user access', function(done) {
// var file = require('fs').createReadStream(__dirname + '/support/eg.jpg', {encoding: 'base64'});
// unauthed.use('/avatars/eg.jpg').post(file, function (err) {
// expect(err).to.exist;
// done();
// })
// })
it('should only allow root user access', function(done) {
var file = require('fs').createReadStream(__dirname + '/support/eg.jpg', {encoding: 'base64'});
unauthed.use('/avatars/eg.jpg').post(file, function (err) {
expect(err).to.exist;
done();
})
})
})
@@ -112,20 +112,20 @@ describe('Static Index', function(){
})
})
// describe('GET /eg.jpg', function(){
// it('should return the newly uploaded file', function(done) {
// var fs = require('fs')
// , file = fs.createReadStream(__dirname + '/support/eg.jpg', {encoding: 'base64'})
// , out = fs.createWriteStream(__dirname + '/support/out-eg.jpg')
// ;
//
// client.use('/eg.jpg').post(file, function (err, body, req, res) {
// client.use('/eg.jpg').pipe(out).get(function (err) {
// done(err)
// })
// })
// })
// })
describe('GET /eg.jpg', function(){
it('should return the newly uploaded file', function(done) {
var fs = require('fs')
, file = fs.createReadStream(__dirname + '/support/eg.jpg')
, out = fs.createWriteStream(__dirname + '/support/out-eg.jpg')
;
client.use('/eg.jpg').post(file, function (err, body, req, res) {
client.use('/eg.jpg').pipe(out).get(function (err) {
done(err)
})
})
})
})
describe('PUT /eg.jpg', function(){
it('should replace the file', function(done) {