mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-23 07:00:32 -04:00
Fix:Setting file ownership for /config and /metadata/logs #584
This commit is contained in:
parent
793cc989de
commit
d130dd6d5e
@ -2,6 +2,7 @@ const Path = require('path')
|
|||||||
const njodb = require('./libs/njodb')
|
const njodb = require('./libs/njodb')
|
||||||
const Logger = require('./Logger')
|
const Logger = require('./Logger')
|
||||||
const { version } = require('../package.json')
|
const { version } = require('../package.json')
|
||||||
|
const filePerms = require('./utils/filePerms')
|
||||||
const LibraryItem = require('./objects/LibraryItem')
|
const LibraryItem = require('./objects/LibraryItem')
|
||||||
const User = require('./objects/user/User')
|
const User = require('./objects/user/User')
|
||||||
const Collection = require('./objects/Collection')
|
const Collection = require('./objects/Collection')
|
||||||
@ -131,6 +132,9 @@ class Db {
|
|||||||
async init() {
|
async init() {
|
||||||
await this.load()
|
await this.load()
|
||||||
|
|
||||||
|
// Set file ownership for all files created by db
|
||||||
|
await filePerms.setDefault(global.ConfigPath, true)
|
||||||
|
|
||||||
if (!this.serverSettings) { // Create first load server settings
|
if (!this.serverSettings) { // Create first load server settings
|
||||||
this.serverSettings = new ServerSettings()
|
this.serverSettings = new ServerSettings()
|
||||||
await this.insertEntity('settings', this.serverSettings)
|
await this.insertEntity('settings', this.serverSettings)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const Path = require('path')
|
const Path = require('path')
|
||||||
const fs = require('../libs/fsExtra')
|
const fs = require('../libs/fsExtra')
|
||||||
|
const filePerms = require('../utils/filePerms')
|
||||||
|
|
||||||
const DailyLog = require('../objects/DailyLog')
|
const DailyLog = require('../objects/DailyLog')
|
||||||
|
|
||||||
@ -11,8 +12,8 @@ class LogManager {
|
|||||||
constructor(db) {
|
constructor(db) {
|
||||||
this.db = db
|
this.db = db
|
||||||
|
|
||||||
this.logDirPath = Path.join(global.MetadataPath, 'logs')
|
this.DailyLogPath = Path.posix.join(global.MetadataPath, 'logs', 'daily')
|
||||||
this.dailyLogDirPath = Path.join(this.logDirPath, 'daily')
|
this.ScanLogPath = Path.posix.join(global.MetadataPath, 'logs', 'scans')
|
||||||
|
|
||||||
this.currentDailyLog = null
|
this.currentDailyLog = null
|
||||||
this.dailyLogBuffer = []
|
this.dailyLogBuffer = []
|
||||||
@ -27,24 +28,38 @@ class LogManager {
|
|||||||
return this.serverSettings.loggerDailyLogsToKeep || 7
|
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() {
|
async init() {
|
||||||
|
await this.ensureLogDirs()
|
||||||
|
|
||||||
// Load daily logs
|
// Load daily logs
|
||||||
await this.scanLogFiles()
|
await this.scanLogFiles()
|
||||||
|
|
||||||
// Check remove extra daily logs
|
// Check remove extra daily logs
|
||||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||||
var dailyLogFilesCopy = [...this.dailyLogFiles]
|
const dailyLogFilesCopy = [...this.dailyLogFiles]
|
||||||
for (let i = 0; i < dailyLogFilesCopy.length - this.loggerDailyLogsToKeep; i++) {
|
for (let i = 0; i < dailyLogFilesCopy.length - this.loggerDailyLogsToKeep; i++) {
|
||||||
var logFileToRemove = dailyLogFilesCopy[i]
|
await this.removeLogFile(dailyLogFilesCopy[i])
|
||||||
await this.removeLogFile(logFileToRemove)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
const currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
||||||
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
|
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
|
||||||
|
|
||||||
this.currentDailyLog = new DailyLog()
|
this.currentDailyLog = new DailyLog()
|
||||||
this.currentDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
this.currentDailyLog.setData({ dailyLogDirPath: this.DailyLogPath })
|
||||||
|
|
||||||
if (this.dailyLogFiles.includes(currentDailyLogFilename)) {
|
if (this.dailyLogFiles.includes(currentDailyLogFilename)) {
|
||||||
Logger.debug(TAG, `Daily log file already exists - set in Logger`)
|
Logger.debug(TAG, `Daily log file already exists - set in Logger`)
|
||||||
@ -63,8 +78,7 @@ class LogManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async scanLogFiles() {
|
async scanLogFiles() {
|
||||||
await fs.ensureDir(this.dailyLogDirPath)
|
const dailyFiles = await fs.readdir(this.DailyLogPath)
|
||||||
var dailyFiles = await fs.readdir(this.dailyLogDirPath)
|
|
||||||
if (dailyFiles && dailyFiles.length) {
|
if (dailyFiles && dailyFiles.length) {
|
||||||
dailyFiles.forEach((logFile) => {
|
dailyFiles.forEach((logFile) => {
|
||||||
if (Path.extname(logFile) === '.txt') {
|
if (Path.extname(logFile) === '.txt') {
|
||||||
@ -80,13 +94,13 @@ class LogManager {
|
|||||||
|
|
||||||
async removeOldestLog() {
|
async removeOldestLog() {
|
||||||
if (!this.dailyLogFiles.length) return
|
if (!this.dailyLogFiles.length) return
|
||||||
var oldestLog = this.dailyLogFiles[0]
|
const oldestLog = this.dailyLogFiles[0]
|
||||||
return this.removeLogFile(oldestLog)
|
return this.removeLogFile(oldestLog)
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeLogFile(filename) {
|
async removeLogFile(filename) {
|
||||||
var fullPath = Path.join(this.dailyLogDirPath, filename)
|
const fullPath = Path.join(this.DailyLogPath, filename)
|
||||||
var exists = await fs.pathExists(fullPath)
|
const exists = await fs.pathExists(fullPath)
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
Logger.error(TAG, 'Invalid log dne ' + fullPath)
|
Logger.error(TAG, 'Invalid log dne ' + fullPath)
|
||||||
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf.filename !== filename)
|
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf.filename !== filename)
|
||||||
@ -109,8 +123,8 @@ class LogManager {
|
|||||||
|
|
||||||
// Check log rolls to next day
|
// Check log rolls to next day
|
||||||
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
|
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
|
||||||
var newDailyLog = new DailyLog()
|
const newDailyLog = new DailyLog()
|
||||||
newDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
newDailyLog.setData({ dailyLogDirPath: this.DailyLogPath })
|
||||||
this.currentDailyLog = newDailyLog
|
this.currentDailyLog = newDailyLog
|
||||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||||
this.removeOldestLog()
|
this.removeOldestLog()
|
||||||
@ -126,7 +140,7 @@ class LogManager {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastLogs = this.currentDailyLog.logs.slice(-5000)
|
const lastLogs = this.currentDailyLog.logs.slice(-5000)
|
||||||
socket.emit('daily_logs', lastLogs)
|
socket.emit('daily_logs', lastLogs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ const date = require('../libs/dateAndTime')
|
|||||||
const Logger = require('../Logger')
|
const Logger = require('../Logger')
|
||||||
const Folder = require('../objects/Folder')
|
const Folder = require('../objects/Folder')
|
||||||
const { LogLevel } = require('../utils/constants')
|
const { LogLevel } = require('../utils/constants')
|
||||||
|
const filePerms = require('../utils/filePerms')
|
||||||
const { getId, secondsToTimestamp } = require('../utils/index')
|
const { getId, secondsToTimestamp } = require('../utils/index')
|
||||||
|
|
||||||
class LibraryScan {
|
class LibraryScan {
|
||||||
@ -61,7 +62,7 @@ class LibraryScan {
|
|||||||
get totalResults() {
|
get totalResults() {
|
||||||
return this.resultsAdded + this.resultsUpdated + this.resultsMissing
|
return this.resultsAdded + this.resultsUpdated + this.resultsMissing
|
||||||
}
|
}
|
||||||
get getLogFilename() {
|
get logFilename() {
|
||||||
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
|
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,14 +125,17 @@ class LibraryScan {
|
|||||||
this.logs.push(logObj)
|
this.logs.push(logObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveLog(logDir) {
|
async saveLog() {
|
||||||
await fs.ensureDir(logDir)
|
await Logger.logManager.ensureScanLogDir()
|
||||||
var outputPath = Path.join(logDir, this.getLogFilename)
|
|
||||||
var logLines = [JSON.stringify(this.toJSON())]
|
const outputPath = Path.join(logDir, this.logFilename)
|
||||||
|
const logLines = [JSON.stringify(this.toJSON())]
|
||||||
this.logs.forEach(l => {
|
this.logs.forEach(l => {
|
||||||
logLines.push(JSON.stringify(l))
|
logLines.push(JSON.stringify(l))
|
||||||
})
|
})
|
||||||
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
|
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
|
||||||
|
await filePerms.setDefault(outputPath)
|
||||||
|
|
||||||
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,6 @@ const Series = require('../objects/entities/Series')
|
|||||||
|
|
||||||
class Scanner {
|
class Scanner {
|
||||||
constructor(db, coverManager) {
|
constructor(db, coverManager) {
|
||||||
this.ScanLogPath = Path.posix.join(global.MetadataPath, 'logs', 'scans')
|
|
||||||
|
|
||||||
this.db = db
|
this.db = db
|
||||||
this.coverManager = coverManager
|
this.coverManager = coverManager
|
||||||
|
|
||||||
@ -165,7 +163,7 @@ class Scanner {
|
|||||||
SocketAuthority.emitter('scan_complete', libraryScan.getScanEmitData)
|
SocketAuthority.emitter('scan_complete', libraryScan.getScanEmitData)
|
||||||
|
|
||||||
if (libraryScan.totalResults) {
|
if (libraryScan.totalResults) {
|
||||||
libraryScan.saveLog(this.ScanLogPath)
|
libraryScan.saveLog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user