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 { getFileSize } = require('../utils/fileUtils')
|
||||||
|
|
||||||
const Backup = require('../objects/Backup')
|
const Backup = require('../objects/Backup')
|
||||||
|
const CacheManager = require('./CacheManager')
|
||||||
|
|
||||||
class BackupManager {
|
class BackupManager {
|
||||||
constructor(notificationManager) {
|
constructor(notificationManager) {
|
||||||
@ -230,6 +231,9 @@ class BackupManager {
|
|||||||
// Reset api cache, set hooks again
|
// Reset api cache, set hooks again
|
||||||
await apiCacheManager.reset()
|
await apiCacheManager.reset()
|
||||||
|
|
||||||
|
// Clear metadata cache
|
||||||
|
await CacheManager.purgeAll()
|
||||||
|
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
|
|
||||||
// Triggers browser refresh for all clients
|
// Triggers browser refresh for all clients
|
||||||
|
@ -16,27 +16,17 @@ class CacheManager {
|
|||||||
/**
|
/**
|
||||||
* Create cache directory paths if they dont exist
|
* 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.CachePath = Path.join(global.MetadataPath, 'cache')
|
||||||
this.CoverCachePath = Path.join(this.CachePath, 'covers')
|
this.CoverCachePath = Path.join(this.CachePath, 'covers')
|
||||||
this.ImageCachePath = Path.join(this.CachePath, 'images')
|
this.ImageCachePath = Path.join(this.CachePath, 'images')
|
||||||
this.ItemCachePath = Path.join(this.CachePath, 'items')
|
this.ItemCachePath = Path.join(this.CachePath, 'items')
|
||||||
|
|
||||||
if (!(await fs.pathExists(this.CachePath))) {
|
await fs.ensureDir(this.CachePath)
|
||||||
await fs.mkdir(this.CachePath)
|
await fs.ensureDir(this.CoverCachePath)
|
||||||
}
|
await fs.ensureDir(this.ImageCachePath)
|
||||||
|
await fs.ensureDir(this.ItemCachePath)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleCoverCache(res, libraryItemId, coverPath, options = {}) {
|
async handleCoverCache(res, libraryItemId, coverPath, options = {}) {
|
||||||
@ -89,23 +79,28 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async purgeEntityCache(entityId, cachePath) {
|
async purgeEntityCache(entityId, cachePath) {
|
||||||
return Promise.all((await fs.readdir(cachePath)).reduce((promises, file) => {
|
return Promise.all(
|
||||||
if (file.startsWith(entityId)) {
|
(await fs.readdir(cachePath)).reduce((promises, file) => {
|
||||||
Logger.debug(`[CacheManager] Going to purge ${file}`);
|
if (file.startsWith(entityId)) {
|
||||||
promises.push(this.removeCache(Path.join(cachePath, file)))
|
Logger.debug(`[CacheManager] Going to purge ${file}`)
|
||||||
}
|
promises.push(this.removeCache(Path.join(cachePath, file)))
|
||||||
return promises
|
}
|
||||||
}, []))
|
return promises
|
||||||
|
}, [])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
removeCache(path) {
|
removeCache(path) {
|
||||||
if (!path) return false
|
if (!path) return false
|
||||||
return fs.pathExists(path).then((exists) => {
|
return fs.pathExists(path).then((exists) => {
|
||||||
if (!exists) return false
|
if (!exists) return false
|
||||||
return fs.unlink(path).then(() => true).catch((err) => {
|
return fs
|
||||||
Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err)
|
.unlink(path)
|
||||||
return false
|
.then(() => true)
|
||||||
})
|
.catch((err) => {
|
||||||
|
Logger.error(`[CacheManager] Failed to remove cache "${path}"`, err)
|
||||||
|
return false
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,4 +153,4 @@ class CacheManager {
|
|||||||
readStream.pipe(res)
|
readStream.pipe(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
module.exports = new CacheManager()
|
module.exports = new CacheManager()
|
||||||
|
Loading…
Reference in New Issue
Block a user