mirror of
https://github.com/Metastem/wikiless
synced 2024-12-06 19:16:58 +01:00
Improved cache_control.js (0.1.3)
Key Changes: Replaced fs with fs/promises: Modern, promise-based approach for better error handling and async flow. Path Handling: Used path.resolve for robust relative path resolution. Error Handling: Added try-catch block in deleteStatic to capture and log errors during file removal. Logging Configuration Issues: Warned about invalid cache_control_interval and logged default fallback behavior. Removed Redundant async: deleteStatic is now a clean asynchronous function. Testing Suggestions: 1. Test with and without the config.cache_control flag enabled. 2. Test with valid and invalid cache_control_interval values. 3. Verify proper cleanup of the specified directory (./media/wikipedia/).
This commit is contained in:
+21
-12
@@ -1,20 +1,29 @@
|
||||
module.exports.removeCacheFiles = function() {
|
||||
const config = require('../config');
|
||||
const fs = require('fs/promises'); // Use promises for better handling
|
||||
const path = require('path');
|
||||
const config = require('../config');
|
||||
|
||||
async function deleteStatic() {
|
||||
const fs = require('fs');
|
||||
const wiki_files = './media/wikipedia/';
|
||||
async function deleteStatic() {
|
||||
const wiki_files = path.resolve(__dirname, '../media/wikipedia'); // Resolve relative path
|
||||
|
||||
fs.rm(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.');
|
||||
});
|
||||
try {
|
||||
await fs.rm(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.'
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`Error clearing cached static media files: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if(config.cache_control) {
|
||||
module.exports.removeCacheFiles = function () {
|
||||
if (config.cache_control) {
|
||||
deleteStatic();
|
||||
|
||||
let hours = config.cache_control_interval;
|
||||
if (hours < 1 || isNaN(hours)) {
|
||||
let hours = parseInt(config.cache_control_interval, 10);
|
||||
if (isNaN(hours) || hours < 1) {
|
||||
console.warn(
|
||||
'Invalid cache_control_interval. Defaulting to 24 hours.'
|
||||
);
|
||||
hours = 24;
|
||||
}
|
||||
|
||||
@@ -22,4 +31,4 @@ module.exports.removeCacheFiles = function() {
|
||||
deleteStatic();
|
||||
}, 1000 * 60 * 60 * hours);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user