Created
October 30, 2011 23:48
-
-
Save evnm/1326602 to your computer and use it in GitHub Desktop.
A Node.js daemon for achieving TTL behavior of files within a Dropbox directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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