Skip to content

Commit

Permalink
fixed clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlumbcgov committed Nov 28, 2023
1 parent 6b4fb4b commit ecb446e
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions backend/src/routes/download-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@ const jsonExport = require('jsonexport');
const fs = require('fs');
const path = require('path');
const {isSafeFilePath} = require("../components/utils")
const { listCache, fileCache } = require("../components/cache");
const { listCache, fileCache, schoolCache } = require("../components/cache");
const FILE_STORAGE_DIR = path.join(__dirname, '../..', 'public');

router.get('/csv/*', checkToken, getDownload, createCSVFile, getCSVDownload);
router.get('/clear-files/:token', clearCSVFiles);
router.get('/flush-cache/:token', flushFileCache);

async function clearCSVFiles(req, res) {
async function flushFileCache(req, res) {
try {
const providedToken = req.params.token;
const configuredToken = config.get('server:clearFilesKey');

if (providedToken !== configuredToken) {
return res.status(403).send('Invalid token');
}
const directoryPath = FILE_STORAGE_DIR ;
// Read all files in the directory
fs.readdirSync(directoryPath).forEach((file) => {
const filePath = path.join(directoryPath, file);

// Delete each file
fs.unlinkSync(filePath);
});
fileCache.flushAll();
schoolCache.flushAll();
res.status(200).send('All files in the directory deleted successfully.');
} catch (error) {
console.error(error);
Expand Down

0 comments on commit ecb446e

Please sign in to comment.