diff --git a/server/managers/BackupManager.js b/server/managers/BackupManager.js index 71b8304ce4..54727b6bba 100644 --- a/server/managers/BackupManager.js +++ b/server/managers/BackupManager.js @@ -14,6 +14,7 @@ const fileUtils = require('../utils/fileUtils') const { getFileSize } = require('../utils/fileUtils') const Backup = require('../objects/Backup') +const CacheManager = require('./CacheManager') class BackupManager { constructor(notificationManager) { @@ -230,6 +231,9 @@ class BackupManager { // Reset api cache, set hooks again await apiCacheManager.reset() + // Clear metadata cache + await CacheManager.purgeAll() + res.sendStatus(200) // Triggers browser refresh for all clients diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index c273d38916..8f810a3307 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -16,27 +16,17 @@ class CacheManager { /** * Create cache directory paths if they dont exist */ - async ensureCachePaths() { // Creates cache paths if necessary and sets owner and permissions + async ensureCachePaths() { + // Creates cache paths if necessary and sets owner and permissions this.CachePath = Path.join(global.MetadataPath, 'cache') this.CoverCachePath = Path.join(this.CachePath, 'covers') this.ImageCachePath = Path.join(this.CachePath, 'images') this.ItemCachePath = Path.join(this.CachePath, 'items') - if (!(await fs.pathExists(this.CachePath))) { - await fs.mkdir(this.CachePath) - } - - if (!(await fs.pathExists(this.CoverCachePath))) { - await fs.mkdir(this.CoverCachePath) - } - - if (!(await fs.pathExists(this.ImageCachePath))) { - await fs.mkdir(this.ImageCachePath) - } - - if (!(await fs.pathExists(this.ItemCachePath))) { - await fs.mkdir(this.ItemCachePath) - } + await fs.ensureDir(this.CachePath) + await fs.ensureDir(this.CoverCachePath) + await fs.ensureDir(this.ImageCachePath) + await fs.ensureDir(this.ItemCachePath) } async handleCoverCache(res, libraryItemId, coverPath, options = {}) { @@ -89,23 +79,28 @@ class CacheManager { } async purgeEntityCache(entityId, cachePath) { - return Promise.all((await fs.readdir(cachePath)).reduce((promises, file) => { - if (file.startsWith(entityId)) { - Logger.debug(`[CacheManager] Going to purge ${file}`); - promises.push(this.removeCache(Path.join(cachePath, file))) - } - return promises - }, [])) + return Promise.all( + (await fs.readdir(cachePath)).reduce((promises, file) => { + if (file.startsWith(entityId)) { + Logger.debug(`[CacheManager] Going to purge ${file}`) + promises.push(this.removeCache(Path.join(cachePath, file))) + } + return promises + }, []) + ) } removeCache(path) { if (!path) return false return fs.pathExists(path).then((exists) => { if (!exists) return false - return fs.unlink(path).then(() => true).catch((err) => { - Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err) - return false - }) + return fs + .unlink(path) + .then(() => true) + .catch((err) => { + Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err) + return false + }) }) } @@ -158,4 +153,4 @@ class CacheManager { readStream.pipe(res) } } -module.exports = new CacheManager() \ No newline at end of file +module.exports = new CacheManager()