From ecb446e0be86762bd9f9cd598994d7bfe3c99f52 Mon Sep 17 00:00:00 2001 From: Shaun Lum Date: Mon, 27 Nov 2023 17:19:47 -0800 Subject: [PATCH] fixed clear cache --- backend/src/routes/download-router.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/backend/src/routes/download-router.js b/backend/src/routes/download-router.js index 5eacf5c..bf6b907 100644 --- a/backend/src/routes/download-router.js +++ b/backend/src/routes/download-router.js @@ -8,13 +8,13 @@ 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'); @@ -22,14 +22,8 @@ async function clearCSVFiles(req, res) { 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);