add cache control feature - wikiless can now empty the cached media files automatically

This commit is contained in:
orenom
2021-12-17 18:06:04 +01:00
parent 337332af57
commit 55537a44d0
3 changed files with 37 additions and 0 deletions
+9
View File
@@ -46,6 +46,15 @@ const config = {
* Read more: https://meta.wikimedia.org/wiki/User-Agent_policy
*/
wikimedia_useragent: process.env.wikimedia_useragent || 'Wikiless media proxy bot (https://codeberg.org/orenom/Wikiless)',
/**
* Cache control. Wikiless can automatically remove the cached media files from
* the server. Cache control is on by default.
* 'cache_control_interval' sets the interval for often the cache directory
* is emptied (in hours). Default is every 24 hours.
*/
cache_control: process.env.CACHE_CONTROL !== 'true' || true,
cache_control_interval: process.env.CACHE_CONTROL_INTERVAL || 24,
}
module.exports = config
+25
View File
@@ -0,0 +1,25 @@
module.exports.removeCacheFiles = function() {
const config = require('../config');
async function deleteStatic() {
const fs = require('fs');
const wiki_files = './media/wikipedia/';
fs.rmdir(wiki_files, { recursive: true, force: true }, () => {
console.log('Cleared cached static media files. You can turn this off by setting the config.cache_control to false.');
});
}
if(config.cache_control) {
deleteStatic();
let hours = config.cache_control_interval;
if (hours < 1 || isNaN(hours)) {
hours = 24;
}
setInterval(() => {
deleteStatic();
}, 1000 * 60 * 60 * hours);
}
}
+3
View File
@@ -102,6 +102,9 @@ redis.on('error', (error) => {
}
})
const cacheControl = require('./cache_control.js')
cacheControl.removeCacheFiles()
if(config.https_enabled) {
https.listen(config.ssl_port, config.http_addr, () => console.log(`Wikiless ${config.domain} running on https://${config.http_addr}:${config.ssl_port}`))
}