From a493b54d029c6d9ec707374fa9bb15976d17b74c Mon Sep 17 00:00:00 2001 From: Marak Squires Date: Sun, 10 Jun 2012 04:05:37 -0700 Subject: [PATCH] [api] Added recursive `readdir` method. #28 --- dbox.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/dbox.js b/dbox.js index e97f099..e3fa3b0 100644 --- a/dbox.js +++ b/dbox.js @@ -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