Fix:File permissions on cache dirs and cache images, Fix:Db delete read stream closing before write stream resulting in deletes sometimes not happening

This commit is contained in:
advplyr 2022-05-15 09:51:08 -05:00
parent c962090c3a
commit 853858825b
3 changed files with 49 additions and 24 deletions

View file

@ -1,6 +1,7 @@
const Path = require('path')
const fs = require('fs-extra')
const stream = require('stream')
const filePerms = require('../utils/filePerms')
const Logger = require('../Logger')
const { resizeImage } = require('../utils/ffmpegHelpers')
@ -9,6 +10,34 @@ class CacheManager {
this.CachePath = Path.join(global.MetadataPath, 'cache')
this.CoverCachePath = Path.join(this.CachePath, 'covers')
this.ImageCachePath = Path.join(this.CachePath, 'images')
this.cachePathsExist = false
}
async ensureCachePaths() { // Creates cache paths if necessary and sets owner and permissions
if (this.cachePathsExist) return
var pathsCreated = false
if (!(await fs.pathExists(this.CachePath))) {
await fs.mkdir(this.CachePath)
pathsCreated = true
}
if (!(await fs.pathExists(this.CoverCachePath))) {
await fs.mkdir(this.CoverCachePath)
pathsCreated = true
}
if (!(await fs.pathExists(this.ImageCachePath))) {
await fs.mkdir(this.ImageCachePath)
pathsCreated = true
}
if (pathsCreated) {
await filePerms.setDefault(this.CachePath)
}
this.cachePathsExist = true
}
async handleCoverCache(res, libraryItem, options = {}) {
@ -34,7 +63,7 @@ class CacheManager {
}
// Write cache
await fs.ensureDir(this.CoverCachePath)
await this.ensureCachePaths()
if (!libraryItem.media.coverPath || !await fs.pathExists(libraryItem.media.coverPath)) {
return res.sendStatus(404)
@ -43,6 +72,9 @@ class CacheManager {
let writtenFile = await resizeImage(libraryItem.media.coverPath, path, width, height)
if (!writtenFile) return res.sendStatus(400)
// Set owner and permissions of cache image
await filePerms.setDefault(path)
var readStream = fs.createReadStream(writtenFile)
readStream.pipe(res)
}
@ -57,7 +89,8 @@ class CacheManager {
async purgeEntityCache(entityId, cachePath) {
// If purgeAll has been called... The cover cache directory no longer exists
await fs.ensureDir(cachePath)
await this.ensureCachePaths()
return Promise.all((await fs.readdir(cachePath)).reduce((promises, file) => {
if (file.startsWith(entityId)) {
Logger.debug(`[CacheManager] Going to purge ${file}`);
@ -109,11 +142,14 @@ class CacheManager {
}
// Write cache
await fs.ensureDir(this.ImageCachePath)
await this.ensureCachePaths()
let writtenFile = await resizeImage(author.imagePath, path, width, height)
if (!writtenFile) return res.sendStatus(400)
// Set owner and permissions of cache image
await filePerms.setDefault(path)
var readStream = fs.createReadStream(writtenFile)
readStream.pipe(res)
}