<commit_before>var db = require.main.require('./src/database');
var nconf = require.main.require('nconf');
var Admin = {};
Admin.init = function(app, cb) {
  // admin routes
  app.router.get('/admin/plugins/expiring-uploads',
                 app.middleware.applyCSRF, app.middleware.admin.buildHeader,
                 Admin.render);
  app.router.get('/api/admin/plugins/expiring-uploads',
                 app.middleware.applyCSRF,
                 Admin.render);
  app.router.post('/api/admin/plugins/expiring-uploads/save',
                  app.middleware.applyCSRF,
                  Admin.saveSettings);
  cb(null, app);
};
Admin.addMenuItem = function(custom_header, cb) {
  custom_header.plugins.push({
    route: '/plugins/expiring-uploads',
    icon: 'fa-clock-o',
    name: 'Expiring Uploads'
  });
  cb(null, custom_header);
};

Admin.render = function(req, res, next) {
  var ExpiringUploads = module.parent.exports;
  // templates.js can only access objects in arrays - I think. :/
  var types = ExpiringUploads.hiddenTypes.map(function(type) {
    return {ftype: type};
  });
  var tplData = {
    csrf: req.csrfToken(),
    storagePath: ExpiringUploads.storage,
    expTstamp: ExpiringUploads.expireAfter / 1000, // js does time in msec...
    hiddenTypes: types,
    customTstamp: ExpiringUploads.customTstamp,
    basePath: nconf.get('base_dir')
  };
  res.render('admin/plugins/expiring-uploads', tplData);
};

Admin.saveSettings = function(req, res, next) {
  var ExpiringUploads = module.parent.exports;
  var storageChange = (ExpiringUploads.storage !== req.body.storage);
  var dbData = {
    storage: req.body.storage,
    expireAfter: parseInt(req.body.expireAfter, 10) * 1000, // ...humans in sec
    hiddenTypes: req.body.hiddenTypes,
    customTstamp: (req.body.customTstamp === 'true')
  };
  db.setObject('settings:expiring-uploads', dbData, function(err) {
    if (err) {
      res.json(500, 'Writing settings to DB failed. ' + err);
    }

    ExpiringUploads.storage = dbData.storage;
    ExpiringUploads.expireAfter = dbData.expireAfter;
    ExpiringUploads.hiddenTypes = dbData.hiddenTypes.split(',');
    ExpiringUploads.customTstamp = dbData.customTstamp;
    if (storageChange) {
      ExpiringUploads.createStorage(function(err) {
        if (err) {
          return res.json(500, 'Creating storage directory failed. ' + err);
        }
        res.json('OK');<commit_msg>I really want that flawless codeclimate :)<commit_after>var db = require.main.require('./src/database');
var nconf = require.main.require('nconf');
var Admin = {};
Admin.init = function(app, cb) {
  // admin routes
  app.router.get('/admin/plugins/expiring-uploads',
                 app.middleware.applyCSRF, app.middleware.admin.buildHeader,
                 Admin.render);
  app.router.get('/api/admin/plugins/expiring-uploads',
                 app.middleware.applyCSRF,
                 Admin.render);
  app.router.post('/api/admin/plugins/expiring-uploads/save',
                  app.middleware.applyCSRF,
                  Admin.saveSettings);
  cb(null, app);
};
Admin.addMenuItem = function(custom_header, cb) {
  custom_header.plugins.push({
    route: '/plugins/expiring-uploads',
    icon: 'fa-clock-o',
    name: 'Expiring Uploads'
  });
  cb(null, custom_header);
};

Admin.render = function(req, res) {
  var ExpiringUploads = module.parent.exports;
  // templates.js can only access objects in arrays - I think. :/
  var types = ExpiringUploads.hiddenTypes.map(function(type) {
    return {ftype: type};
  });
  var tplData = {
    csrf: req.csrfToken(),
    storagePath: ExpiringUploads.storage,
    expTstamp: ExpiringUploads.expireAfter / 1000, // js does time in msec...
    hiddenTypes: types,
    customTstamp: ExpiringUploads.customTstamp,
    basePath: nconf.get('base_dir')
  };
  res.render('admin/plugins/expiring-uploads', tplData);
};

Admin.saveSettings = function(req, res) {
  var ExpiringUploads = module.parent.exports;
  var storageChange = (ExpiringUploads.storage !== req.body.storage);
  var dbData = {
    storage: req.body.storage,
    expireAfter: parseInt(req.body.expireAfter, 10) * 1000, // ...humans in sec
    hiddenTypes: req.body.hiddenTypes,
    customTstamp: (req.body.customTstamp === 'true')
  };
  db.setObject('settings:expiring-uploads', dbData, function(err) {
    if (err) {
      res.json(500, 'Writing settings to DB failed. ' + err);
    }

    ExpiringUploads.storage = dbData.storage;
    ExpiringUploads.expireAfter = dbData.expireAfter;
    ExpiringUploads.hiddenTypes = dbData.hiddenTypes.split(',');
    ExpiringUploads.customTstamp = dbData.customTstamp;
    if (storageChange) {
      ExpiringUploads.createStorage(function(err) {
        if (err) {
          return res.json(500, 'Creating storage directory failed. ' + err);
        }
        res.json('OK');
{'fraction_non_alphanumeric': 0.13840115678578807, 'fraction_numerical': 0.006610204503201818, 'mean_word_length': 3.1278772378516626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4843831', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1547, 'n_words': 287}