clean_images.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var logging = require("./logging");
  2. var config = require("./config");
  3. var df = require("node-df");
  4. var fs = require("fs");
  5. var exp = {};
  6. // uses `df` to get the available fisk space
  7. // callback contains error, true|false
  8. function should_clean(callback) {
  9. df({
  10. file: __dirname + "/../" + config.faces_dir,
  11. prefixMultiplier: 'KiB',
  12. isDisplayPrefixMultiplier: false,
  13. precision: 2
  14. }, function (err, response) {
  15. var available = response[0].available;
  16. console.log("ImageCleaner: " + available + "KB available");
  17. callback(err, available < config.cleaning_limit);
  18. });
  19. }
  20. // check if disk limit reached
  21. // then delete images
  22. exp.run = function() {
  23. should_clean(function(err, clean) {
  24. if (err) {
  25. logging.error(err);
  26. } else if (clean) {
  27. logging.warn("ImageCleaner: Disk limit reached! Cleaning images now");
  28. var skindir = __dirname + "/../" + config.faces_dir;
  29. var helmdir = __dirname + "/../" + config.helms_dir;
  30. var files = fs.readdirSync(skindir);
  31. for (var i = 0; i < Math.min(files.length, config.cleaning_amount); i++) {
  32. var filename = files[i];
  33. if (filename[0] != ".") {
  34. fs.unlink(skindir + filename, function(){});
  35. fs.unlink(helmdir + filename, function(){});
  36. }
  37. }
  38. } else {
  39. logging.log("ImageCleaner: Nothing to clean");
  40. }
  41. });
  42. };
  43. module.exports = exp;