Instantly share code, notes, and snippets.

@evnm /app.js
Created Oct 30, 2011

Embed
What would you like to do?
A Node.js daemon for achieving TTL behavior of files within a Dropbox directory
var Log = require('log'),
log = new Log('info'),
DropboxClient = require('dropbox').DropboxClient,
dropbox = new DropboxClient("api_key", "api_secret",
"access_token", "access_token_secret");
/**
* Returns a function that checks the mtime of all files within dir
* and deletes any that are older than ttl.
*/
function checkForAndRmExpiredItems(dir, ttl) {
return function() {
dropbox.getMetadata(dir, function(err, data) {
if (err) return console.error(err);
data.contents.forEach(function(elem, index, array) {
var mtime = new Date(elem.modified).getTime();
// If item is older than the ttl, delete it.
if (Date.now() - mtime > ttl) {
log.info("Deleting: " + elem.path);
dropbox.deleteItem(elem.path, function(err, data) {
if (err)
log.error("Error occurred while attempting delete: " + err);
});
}
});
});
}
}
var dirToWatch = "dir-with-expirable-items", // Relative to Dropbox root.
ttl = 604800000, // 7 days
interval = 86400; // 24 hours
log.info("Starting Dropbox TTL daemon watching directory(" + dirToWatch +
") with ttl(" + ttl + "), at interval(" + interval + ")");
setInterval(
checkForAndRmExpiredItems(dirToWatch, ttl),
interval
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment