Skip to content

Commit

Permalink
Merge pull request #9 from upfrontIO/size-on-list
Browse files Browse the repository at this point in the history
Add Size & LastModified to .list() response
  • Loading branch information
respectTheCode committed Jan 21, 2015
2 parents 9b181c4 + 7af0c78 commit 13c000d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
44 changes: 17 additions & 27 deletions index.js
Expand Up @@ -125,35 +125,25 @@ Client.prototype.copyFile = function (from, to, callback) {

Client.prototype.list = function (options, cb) {
var self = this;

async.waterfall([
function (cb) {
if (!options.prefix) {
options.prefix = "/";
}
cb();
},
function (cb) {
utils.checkToPath(self.config.bucket + options.prefix, function () {
cb();
});
},
function (cb) {
var walk = require("walk");
var walker = walk.walk(self.config.bucket + options.prefix);
var files = [];

walker.on("file", function (root, stat, next) {
var directory = relative(self.config.bucket, root);
files.push({Key: join(directory, stat.name)});
next();
var baseDirectory = self.config.bucket + (options.prefix || '/');
utils.checkToPath(baseDirectory, function() {
var walk = require("walk");
var walker = walk.walk(baseDirectory);
var files = [];

walker.on("file", function (root, stat, next) {
files.push({
Key: join(relative(self.config.bucket, root), stat.name),
Size: stat.size,
LastModified: stat.mtime
});
next();
});

walker.on("end", function () {
cb(null, {Contents: files});
});
}
], cb);
walker.on("end", function () {
cb(null, {Contents: files});
});
});
};

module.exports.createClient = function (config) {
Expand Down
24 changes: 15 additions & 9 deletions test.js
Expand Up @@ -144,29 +144,35 @@ describe("Faux-Knox", function () {
var opts = {prefix: "list/"};
client.list(opts, function (err, page) {
if (err) return done(err);
page.Contents.should.eql([
{Key: "list/one"}, {Key: "list/two"}
]);
var files = ['list/one', 'list/two'].map(function(file){
var stats = fs.statSync('./test_files/'+file);
return {Key: file, Size: stats.size, LastModified: stats.mtime};
});
page.Contents.should.eql(files);
done();
});
});
it("should join paths correctly", function (done) {
var opts = {prefix: "list"};
client.list(opts, function (err, page) {
if (err) return done(err);
page.Contents.should.eql([
{Key: "list/one"}, {Key: "list/two"}
]);
var files = ['list/one', 'list/two'].map(function(file){
var stats = fs.statSync('./test_files/'+file);
return {Key: file, Size: stats.size, LastModified: stats.mtime};
});
page.Contents.should.eql(files);
done();
});
});
it("should list recursively", function (done) {
var opts = {prefix: "list_nested"};
client.list(opts, function (err, page) {
if (err) return done(err);
page.Contents.should.eql([
{Key: "list_nested/level/one"}, {Key: "list_nested/level/two"}
]);
var files = ['list_nested/level/one', 'list_nested/level/two'].map(function(file){
var stats = fs.statSync('./test_files/'+file);
return {Key: file, Size: stats.size, LastModified: stats.mtime};
});
page.Contents.should.eql(files);
done();
});
});
Expand Down

0 comments on commit 13c000d

Please sign in to comment.