mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Merge pull request #3311 from nichwall/backup_restore_clear_cache
Backup restore clear cache
This commit is contained in:
commit
5f572face5
@ -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
|
||||
|
@ -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()
|
||||
module.exports = new CacheManager()
|
||||
|
Loading…
Reference in New Issue
Block a user