teddit-reddit-frontend-alte.../cacheControl.js

33 lines
950 B
JavaScript
Raw Normal View History

module.exports.removeCacheFiles = function() {
2021-11-26 21:33:12 +01:00
const config = require('./config');
2021-11-26 21:33:12 +01:00
async function deleteStatic() {
const fs = require('fs');
const pics = './static/pics/';
const vids = './static/vids/';
fs.rmdir(pics, { recursive: true, force: true }, () => {
fs.rmdir(vids, { recursive: true, force: true }, () => {
['pics/thumbs', 'pics/flairs', 'pics/icons', 'vids'].map((d) => `./static/${d}`)
.filter((d) => !fs.existsSync(d))
.forEach((d) => fs.mkdirSync(d, { recursive: true }));
2021-11-26 21:33:12 +01:00
console.log('Cleared cached static media files. You can turn this off by setting the config.cache_control to false.');
2021-11-26 21:33:12 +01:00
});
});
}
2021-11-26 21:33:12 +01:00
if(config.cache_control) {
deleteStatic();
let hours = config.cache_control_interval;
if (hours < 1 || hours > 10000 || isNaN(hours)) {
hours = 24;
}
setInterval(() => {
2021-11-26 21:33:12 +01:00
deleteStatic();
}, 1000 * 60 * 60 * hours);
}
}