Permalink
Browse files

putBuffer

Since the buffer is already "Buffered" into memory, this is using
fs.writeFile instead of streams.
  • Loading branch information...
1 parent 4aedc6c commit 0983c1f9c4fb6321f5a844caa97b81c5d553a784 @wlaurance wlaurance committed Jul 16, 2013
Showing with 27 additions and 8 deletions.
  1. +13 −8 index.js
  2. +1 −0 test.js
  3. +13 −0 utils.js
View
@@ -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){
@@ -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){
@@ -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);
View
@@ -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();
});
});
});
View
@@ -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.