mirror of
https://github.com/Metastem/wikiless
synced 2024-12-06 19:16:58 +01:00
add cache control feature - wikiless can now empty the cached media files automatically
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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}`))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user