{"size": 767, "ext": "js", "max_stars_repo_path": "src/housekeeper.js", "max_stars_repo_name": "madflow/imgdmp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/housekeeper.js", "max_issues_repo_name": "madflow/imgdmp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/housekeeper.js", "max_forks_repo_name": "madflow/imgdmp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.3055555556, "max_line_length": 72, "alphanum_fraction": 0.4511082138}
const fs = require('fs');

function deleteExpired(db) {
  db.find(
    {
      $where: function () {
        return this.expires !== null && this.expires < new Date();
      },
    },
    function (err, docs) {
      if (err) {
        throw err;
      }
      docs.forEach((image) => {
        fs.unlink(image.path, function (err) {
          if (err) {
            console.log(err);
          } else {
            console.log(`DELETED FILE ${image.filename}`);
          }
          db.remove({ _id: image._id }, {}, function (err, numRemoved) {
            if (err) {
              console.log(err);
            }
            console.log(`DELETED DB ENTRY`);
          });
        });
      });
    },
  );
}

module.exports = {
  deleteExpired: deleteExpired,
};
