From 142205f060c04682eb1bb8d2302b8c1cafd39586 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 2 Oct 2022 14:46:48 -0500 Subject: [PATCH] Add:Purge items cache button and api endpoint --- client/components/prompt/Confirm.vue | 2 +- client/pages/config/index.vue | 33 ++++++++++++++++++++++++++-- server/controllers/MiscController.js | 14 ++++++++++-- server/managers/CacheManager.js | 9 ++++++++ server/routers/ApiRouter.js | 5 +++-- 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/client/components/prompt/Confirm.vue b/client/components/prompt/Confirm.vue index 700cf168..a2b61339 100644 --- a/client/components/prompt/Confirm.vue +++ b/client/components/prompt/Confirm.vue @@ -3,7 +3,7 @@
-

{{ message }}

+

Cancel
diff --git a/client/pages/config/index.vue b/client/pages/config/index.vue index ad79caff..4a9c38f3 100644 --- a/client/pages/config/index.vue +++ b/client/pages/config/index.vue @@ -209,8 +209,12 @@
- +
+ + +
+

Report bugs, request features, and contribute on @@ -421,7 +425,7 @@ export default { this.showConfirmPurgeCache = false this.isPurgingCache = true await this.$axios - .$post('/api/purgecache') + .$post('/api/cache/purge') .then(() => { this.$toast.success('Cache Purged!') }) @@ -430,6 +434,31 @@ export default { this.$toast.error('Failed to purge cache') }) this.isPurgingCache = false + }, + purgeItemsCache() { + const payload = { + message: `Warning! This will delete the entire folder at /metadata/cache/items.
Are you sure you want to purge items cache?`, + callback: (confirmed) => { + if (confirmed) { + this.sendPurgeItemsCache() + } + }, + type: 'yesNo' + } + this.$store.commit('globals/setConfirmPrompt', payload) + }, + async sendPurgeItemsCache() { + this.isPurgingCache = true + await this.$axios + .$post('/api/cache/items/purge') + .then(() => { + this.$toast.success('Items Cache Purged!') + }) + .catch((error) => { + console.error('Failed to purge items cache', error) + this.$toast.error('Failed to purge items cache') + }) + this.isPurgingCache = false } }, mounted() { diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index 8cba1acc..2a3029cc 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -158,16 +158,26 @@ class MiscController { }) } - // POST: api/purgecache (admin) + // POST: api/cache/purge (admin) async purgeCache(req, res) { if (!req.user.isAdminOrUp) { return res.sendStatus(403) } - Logger.info(`[ApiRouter] Purging all cache`) + Logger.info(`[MiscController] Purging all cache`) await this.cacheManager.purgeAll() res.sendStatus(200) } + // POST: api/cache/items/purge + async purgeItemsCache(req, res) { + if (!req.user.isAdminOrUp) { + return res.sendStatus(403) + } + Logger.info(`[MiscController] Purging items cache`) + await this.cacheManager.purgeItems() + res.sendStatus(200) + } + async findBooks(req, res) { var provider = req.query.provider || 'google' var title = req.query.title || '' diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index dccd8faa..78543ac9 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -114,6 +114,15 @@ class CacheManager { await this.ensureCachePaths() } + async purgeItems() { + if (await fs.pathExists(this.ItemCachePath)) { + await fs.remove(this.ItemCachePath).catch((error) => { + Logger.error(`[CacheManager] Failed to remove items cache dir "${this.ItemCachePath}"`, error) + }) + } + await this.ensureCachePaths() + } + async handleAuthorCache(res, author, options = {}) { const format = options.format || 'webp' const width = options.width || 400 diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index d5b46feb..e45adc4c 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -227,8 +227,9 @@ class ApiRouter { this.router.get('/encode-m4b/:id', MiscController.encodeM4b.bind(this)) this.router.post('/encode-m4b/:id/cancel', MiscController.cancelM4bEncode.bind(this)) this.router.get('/tasks', MiscController.getTasks.bind(this)) - this.router.patch('/settings', MiscController.updateServerSettings.bind(this)) // Root only - this.router.post('/purgecache', MiscController.purgeCache.bind(this)) // Root only + this.router.patch('/settings', MiscController.updateServerSettings.bind(this)) + this.router.post('/cache/purge', MiscController.purgeCache.bind(this)) + this.router.post('/cache/items/purge', MiscController.purgeItemsCache.bind(this)) this.router.post('/authorize', MiscController.authorize.bind(this)) this.router.get('/search/covers', MiscController.findCovers.bind(this)) this.router.get('/search/books', MiscController.findBooks.bind(this))