Skip to content

Commit

Permalink
[api] Added recursive readdir method. #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Jun 10, 2012
1 parent 4043bf1 commit a493b54
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions dbox.js
Expand Up @@ -157,6 +157,54 @@ exports.app = function(config){
})
},

//
// Recursively loads a dropbox folder
//
readdir: function (path, callback) {
var results = [],
REQUEST_CONCURRENCY_DELAY = 200,
callbacks = 0
self = this;
//
// Remark: REQUEST_CONCURRENCY_DELAY represents the millisecond,
// delay between outgoing requests to dropbox
//
function load (path) {
callbacks++;
//
// Give the dropbox API a delay between requests,
// by wrapping each depth level in a setTimeout delay
//
setTimeout(function(){
self.client.metadata(path, function (status, reply) {
//
// If we have found any contents on this level of the folder
//
if (reply.contents) {
reply.contents.forEach(function (item) {
//
// Add the item into our results array
//
results.push(item.path);
//
// If we have encountered another folder, we are going to recurse on it
//
if (item.is_dir) {
load(item.path);
}
});
}
callbacks--;
if (callbacks === 0) {
callback(status, results);
}
});
}, REQUEST_CONCURRENCY_DELAY)
}
console.log('warn: recursively loading data from dropbox...this may take some time');
load(path, results);
},

revisions: function(path, args, cb){
if(cb == null){
cb = args
Expand Down

0 comments on commit a493b54

Please sign in to comment.