From 55537a44d063837a3e34e0a3d42d618554a29c40 Mon Sep 17 00:00:00 2001 From: orenom Date: Fri, 17 Dec 2021 18:06:04 +0100 Subject: [PATCH] add cache control feature - wikiless can now empty the cached media files automatically --- config.js.template | 9 +++++++++ src/cache_control.js | 25 +++++++++++++++++++++++++ src/wikiless.js | 3 +++ 3 files changed, 37 insertions(+) create mode 100644 src/cache_control.js diff --git a/config.js.template b/config.js.template index 326d249..db6c801 100644 --- a/config.js.template +++ b/config.js.template @@ -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 diff --git a/src/cache_control.js b/src/cache_control.js new file mode 100644 index 0000000..a3ed1da --- /dev/null +++ b/src/cache_control.js @@ -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); + } +} diff --git a/src/wikiless.js b/src/wikiless.js index b3e7eb6..7a5f7b2 100644 --- a/src/wikiless.js +++ b/src/wikiless.js @@ -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}`)) }