Fix: set downloaded/uploaded cover owner and permissions and if creating intitial config/metadata directories at startup then set owner of those #394

This commit is contained in:
advplyr 2022-04-24 19:12:00 -05:00
parent e253939c1e
commit dcd4f69383
3 changed files with 34 additions and 3 deletions

View File

@ -10,6 +10,7 @@ const { version } = require('../package.json')
// Utils
const dbMigration = require('./utils/dbMigration')
const filePerms = require('./utils/filePerms')
const Logger = require('./Logger')
// Classes
@ -46,9 +47,18 @@ class Server {
global.MetadataPath = global.MetadataPath.replace(/\\/g, '/')
}
fs.ensureDirSync(global.ConfigPath, 0o774)
fs.ensureDirSync(global.MetadataPath, 0o774)
fs.ensureDirSync(global.AudiobookPath, 0o774)
if (!fs.pathExistsSync(global.ConfigPath)) {
fs.mkdirSync(global.ConfigPath)
filePerms.setDefaultDirSync(global.ConfigPath, false)
}
if (!fs.pathExistsSync(global.MetadataPath)) {
fs.mkdirSync(global.MetadataPath)
filePerms.setDefaultDirSync(global.MetadataPath, false)
}
if (!fs.pathExistsSync(global.AudiobookPath)) {
fs.mkdirSync(global.AudiobookPath)
filePerms.setDefaultDirSync(global.AudiobookPath, false)
}
this.db = new Db()
this.watcher = new Watcher()

View File

@ -113,6 +113,7 @@ class CoverManager {
Logger.info(`[CoverManager] Uploaded libraryItem cover "${coverFullPath}" for "${libraryItem.media.metadata.title}"`)
await filePerms.setDefault(coverFullPath)
libraryItem.updateMediaCover(coverFullPath)
return {
cover: coverFullPath
@ -151,6 +152,7 @@ class CoverManager {
Logger.info(`[CoverManager] Downloaded libraryItem cover "${coverFullPath}" from url "${url}" for "${libraryItem.media.metadata.title}"`)
await filePerms.setDefault(coverFullPath)
libraryItem.updateMediaCover(coverFullPath)
return {
cover: coverFullPath
@ -250,6 +252,8 @@ class CoverManager {
var success = await extractCoverArt(audioFileWithCover.metadata.path, coverFilePath)
if (success) {
await filePerms.setDefault(coverFilePath)
libraryItem.updateMediaCover(coverFilePath)
return coverFilePath
}

View File

@ -95,3 +95,20 @@ module.exports.setDefault = (path, silent = false) => {
chmodr(path, mode, uid, gid, resolve)
})
}
// Default permissions 0o744 and global Uid/Gid
// Used for setting default permission to initial config/metadata directories
module.exports.setDefaultDirSync = (path, silent = false) => {
const mode = 0o744
const uid = global.Uid
const gid = global.Gid
if (!silent) Logger.debug(`[FilePerms] Setting dir permission "${mode}" for uid ${uid} and gid ${gid} | "${path}"`)
try {
fs.chmodSync(path, mode)
fs.chownSync(path, uid, gid)
return true
} catch (error) {
Logger.error(`[FilePerms] Error setting dir permissions for path "${path}"`, error)
return false
}
}