Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ var async = require("async");
var utils = require(__dirname + "/utils");
var join = require("path").join;
var relative = require("path").relative;
var once = require("once");
var StreamCounter = require("stream-counter");

var s3Response = {
'200': { headers: {}, statusCode: 200 },
'404': { headers: {}, statusCode: 404 }
};

function getHeader(headers, headerNameLowerCase) {
for (var header in headers) {
if (header.toLowerCase() === headerNameLowerCase) {
return headers[header];
}
}
return null;
}

var Client = module.exports = function (config) {
config = this.config = config || {};
Expand Down Expand Up @@ -65,10 +81,10 @@ Client.prototype.putFile = function (from, to, headers, callback) {
var w = fs.createWriteStream(self.config.bucket + to);

w.on("finish", function () {
callback(null, {headers:{}, statusCode:200});
callback(null, s3Response['200']);
});
w.on("error", function (e) {
callback(null, {headers:{}, statusCode:404});
callback(null, s3Response['404']);
});
r.pipe(w);
});
Expand All @@ -83,11 +99,45 @@ Client.prototype.putBuffer = function (buffer, to, headers, callback) {
return callback(err);
}

return callback(null, {headers:{}, statusCode:200});
return callback(null, s3Response['200']);
});
});
};

Client.prototype.putStream = function (stream, filename, headers, callback) {
var contentLength = getHeader(headers, 'content-length');
if (contentLength === null) {
process.nextTick(function () {
callback(new Error('You must specify a Content-Length header.'));
});
return;
}

callback = once(callback);
var writeLocation = this.config.bucket + filename;

utils.checkToPathSync(writeLocation);
var writeStream = fs.createWriteStream(writeLocation);
writeStream.on('error', callback);

var counter = new StreamCounter();
counter.on('progress', function () {
writeStream.emit('progress', {
percent: counter.bytes / contentLength * 100 | 0,
written: counter.bytes,
total: contentLength
});
});

stream.pipe(counter)
stream.pipe(writeStream)

writeStream.on('close', function () {
callback(null, s3Response['200']);
});
return writeStream
}

Client.prototype.deleteFile = function (file, callback) {
var self = this;

Expand All @@ -111,7 +161,7 @@ Client.prototype.copyFile = function (from, to, callback) {
return callback(err);
}

return callback(null, {headers:{}, statusCode:200});
return callback(null, s3Response['200']);
};

readStream.on("error", done);
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
"url": "https://github.com/AppPress/node-faux-knox/issues"
},
"devDependencies": {
"mocha": "~1.21.4",
"should": "~4.0.4"
"mocha": "^2.3.2",
"should": "^7.1.0"
},
"dependencies": {
"underscore": "~1.6.0",
"async": "~0.9.0",
"async": "^1.4.2",
"mkdirp": "~0.5.0",
"rimraf": "~2.2.1",
"once": "^1.3.2",
"rimraf": "^2.4.3",
"stream-counter": "^1.0.0",
"underscore": "^1.8.3",
"walk": "^2.3.1"
}
}
14 changes: 13 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("Faux-Knox", function () {
knox.should.have.property("createClient").and.be.a.Function;
});
it("should support methods", function (done) {
var methods = ["getFile", "putFile", "putBuffer", "deleteFile"];
var methods = ["getFile", "putFile", "putBuffer", "putStream", "deleteFile"];
function checker(method, callback) {
client.should.have.property(method).and.be.a.Function;
callback();
Expand Down Expand Up @@ -89,6 +89,18 @@ describe("Faux-Knox", function () {
});
});
});
describe("putStream", function () {
it("should put a stream where I tell it to", function (done) {
var readStream = fs.createReadStream("./test_files/path/to/test.json");
// Faking content-length for now, but should probably test progress events
var headers = {"Content-Length":"100"};
client.putStream(readStream, 'path/to/stream/streamed.json', headers, function (err, res) {
res.should.have.property("headers").and.be.a.Object;
res.should.have.property("statusCode", 200);
done();
});
});
});
describe("deleteFile", function () {
before(function (done) {
client.putFile("./test_files/put/fort_knox_tank.jpg", "to/a/new/path/here/tank.jpg", done);
Expand Down
29 changes: 29 additions & 0 deletions test_files/path/to/stream/streamed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "faux-knox",
"version": "0.0.0",
"description": "Mock requests to knox module using file system",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/wlaurance/faux-knox.git"
},
"keywords": [
"knox",
"mock"
],
"author": "Will S. Laurance",
"license": "BSD",
"readmeFilename": "README.md",
"gitHead": "cf1d54e5cf8ae5e6a6e2142ab42f7eb4a068147a",
"bugs": {
"url": "https://github.com/wlaurance/faux-knox/issues"
},
"devDependencies": {
"mocha": "~1.12.0",
"async": "~0.2.9",
"should": "~1.2.2"
}
}
21 changes: 15 additions & 6 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ var _ = require('underscore');
var mkdirp = require('mkdirp');
var fs = require('fs');

var checkToPath = module.exports.checkToPath = function (to, cb) {
var splitPath = to.split('/');
var dirPath = _.initial(splitPath, 1).join('/');
module.exports.checkToPath = 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);
});
fs.exists(dirPath, function(exists){
return exists ? cb() : mkdirp(dirPath, cb);
});
};

module.exports.checkToPathSync = function checkToPathSync(to) {
var splitPath = to.split('/');
var dirPath = _.initial(splitPath, 1).join('/');

if (!fs.existsSync(dirPath)) {
mkdirp.sync(dirPath);
}
};