mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-19 10:19:37 +00:00
Fix:Setting file ownership for /config and /metadata/logs #584
This commit is contained in:
parent
793cc989de
commit
d130dd6d5e
4 changed files with 43 additions and 23 deletions
|
|
@ -1,5 +1,6 @@
|
|||
const Path = require('path')
|
||||
const fs = require('../libs/fsExtra')
|
||||
const filePerms = require('../utils/filePerms')
|
||||
|
||||
const DailyLog = require('../objects/DailyLog')
|
||||
|
||||
|
|
@ -11,8 +12,8 @@ class LogManager {
|
|||
constructor(db) {
|
||||
this.db = db
|
||||
|
||||
this.logDirPath = Path.join(global.MetadataPath, 'logs')
|
||||
this.dailyLogDirPath = Path.join(this.logDirPath, 'daily')
|
||||
this.DailyLogPath = Path.posix.join(global.MetadataPath, 'logs', 'daily')
|
||||
this.ScanLogPath = Path.posix.join(global.MetadataPath, 'logs', 'scans')
|
||||
|
||||
this.currentDailyLog = null
|
||||
this.dailyLogBuffer = []
|
||||
|
|
@ -27,24 +28,38 @@ class LogManager {
|
|||
return this.serverSettings.loggerDailyLogsToKeep || 7
|
||||
}
|
||||
|
||||
async ensureLogDirs() {
|
||||
await fs.ensureDir(this.DailyLogPath)
|
||||
await fs.ensureDir(this.ScanLogPath)
|
||||
await filePerms.setDefault(Path.posix.join(global.MetadataPath, 'logs'), true)
|
||||
}
|
||||
|
||||
async ensureScanLogDir() {
|
||||
if (!(await fs.pathExists(this.ScanLogPath))) {
|
||||
await fs.mkdir(this.ScanLogPath)
|
||||
await filePerms.setDefault(this.ScanLogPath)
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.ensureLogDirs()
|
||||
|
||||
// Load daily logs
|
||||
await this.scanLogFiles()
|
||||
|
||||
// Check remove extra daily logs
|
||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
var dailyLogFilesCopy = [...this.dailyLogFiles]
|
||||
const dailyLogFilesCopy = [...this.dailyLogFiles]
|
||||
for (let i = 0; i < dailyLogFilesCopy.length - this.loggerDailyLogsToKeep; i++) {
|
||||
var logFileToRemove = dailyLogFilesCopy[i]
|
||||
await this.removeLogFile(logFileToRemove)
|
||||
await this.removeLogFile(dailyLogFilesCopy[i])
|
||||
}
|
||||
}
|
||||
|
||||
var currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
||||
const currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
||||
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
|
||||
|
||||
this.currentDailyLog = new DailyLog()
|
||||
this.currentDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
||||
this.currentDailyLog.setData({ dailyLogDirPath: this.DailyLogPath })
|
||||
|
||||
if (this.dailyLogFiles.includes(currentDailyLogFilename)) {
|
||||
Logger.debug(TAG, `Daily log file already exists - set in Logger`)
|
||||
|
|
@ -63,8 +78,7 @@ class LogManager {
|
|||
}
|
||||
|
||||
async scanLogFiles() {
|
||||
await fs.ensureDir(this.dailyLogDirPath)
|
||||
var dailyFiles = await fs.readdir(this.dailyLogDirPath)
|
||||
const dailyFiles = await fs.readdir(this.DailyLogPath)
|
||||
if (dailyFiles && dailyFiles.length) {
|
||||
dailyFiles.forEach((logFile) => {
|
||||
if (Path.extname(logFile) === '.txt') {
|
||||
|
|
@ -80,13 +94,13 @@ class LogManager {
|
|||
|
||||
async removeOldestLog() {
|
||||
if (!this.dailyLogFiles.length) return
|
||||
var oldestLog = this.dailyLogFiles[0]
|
||||
const oldestLog = this.dailyLogFiles[0]
|
||||
return this.removeLogFile(oldestLog)
|
||||
}
|
||||
|
||||
async removeLogFile(filename) {
|
||||
var fullPath = Path.join(this.dailyLogDirPath, filename)
|
||||
var exists = await fs.pathExists(fullPath)
|
||||
const fullPath = Path.join(this.DailyLogPath, filename)
|
||||
const exists = await fs.pathExists(fullPath)
|
||||
if (!exists) {
|
||||
Logger.error(TAG, 'Invalid log dne ' + fullPath)
|
||||
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf.filename !== filename)
|
||||
|
|
@ -109,8 +123,8 @@ class LogManager {
|
|||
|
||||
// Check log rolls to next day
|
||||
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
|
||||
var newDailyLog = new DailyLog()
|
||||
newDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
||||
const newDailyLog = new DailyLog()
|
||||
newDailyLog.setData({ dailyLogDirPath: this.DailyLogPath })
|
||||
this.currentDailyLog = newDailyLog
|
||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
this.removeOldestLog()
|
||||
|
|
@ -126,7 +140,7 @@ class LogManager {
|
|||
return
|
||||
}
|
||||
|
||||
var lastLogs = this.currentDailyLog.logs.slice(-5000)
|
||||
const lastLogs = this.currentDailyLog.logs.slice(-5000)
|
||||
socket.emit('daily_logs', lastLogs)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue