Skip to content

Commit

Permalink
putBuffer
Browse files Browse the repository at this point in the history
Since the buffer is already "Buffered" into memory, this is using
fs.writeFile instead of streams.
  • Loading branch information
wlaurance committed Jul 16, 2013
1 parent 4aedc6c commit 0983c1f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs'),
_ = require('underscore'),
async = require('async'),
mkdirp = require('mkdirp');
stream = require('stream'),
utils = require(__dirname + '/utils');

exports.createClient = function(config){
function Client(config){
Expand Down Expand Up @@ -37,11 +37,7 @@ exports.createClient = function(config){

Client.prototype.putFile = function(from, to, callback){
function checkToPath(cb){
var splitPath = to.split('/');
var dirPath = config.bucket + _.initial(splitPath, 1).join('/');
fs.exists(dirPath, function(exists){
return exists ? cb() : mkdirp(dirPath, cb);
});
utils.checkToPath(config.bucket + to, cb);
}
function checkFromPath(cb){
fs.exists(from, function(exists){
Expand All @@ -63,7 +59,16 @@ exports.createClient = function(config){
r.pipe(w);
});
}
Client.prototype.putBuffer = function(){}
Client.prototype.putBuffer = function(buffer, to, headers, callback){
utils.checkToPath(config.bucket + to, function(){
fs.writeFile(config.bucket + to, buffer, function(err){
if (err) {
return callback(err);
}
return callback(null, {headers:{statusCode:201}});
});
});
}
Client.prototype.deleteFile = function(){}
}
return new Client(config);
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('Faux-Knox', function(){
client.putBuffer(buff, 'from/buffer/land/dev/null.text', {'Content-Type':'text/plain'}, function(err, res){
res.should.have.property('headers');
res.headers.should.have.property('statusCode', 201);
done();
});
});
});
Expand Down
13 changes: 13 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var _ = require('underscore'),
mkdirp = require('mkdirp'),
fs = require('fs');

function checkToPath(to, cb){
var splitPath = to.split('/');
var dirPath = _.initial(splitPath, 1).join('/');
fs.exists(dirPath, function(exists){
return exists ? cb() : mkdirp(dirPath, cb);
});
}

exports.checkToPath = checkToPath;

0 comments on commit 0983c1f

Please sign in to comment.