mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-06 03:49:40 +00:00
Wrap ensureDir in try-catch blocks
This commit is contained in:
parent
329e3c7179
commit
5f1edcb609
5 changed files with 67 additions and 22 deletions
|
|
@ -48,10 +48,35 @@ class BackupManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
|
// Validate backupPath before attempting to create it
|
||||||
|
if (!this.backupPath || typeof this.backupPath !== 'string' || this.backupPath.length < 2) {
|
||||||
|
Logger.error(`[BackupManager] Invalid backup path configured: "${this.backupPath}". Falling back to default.`)
|
||||||
|
// Reset to default backup path
|
||||||
|
global.ServerSettings.backupPath = Path.join(global.MetadataPath, 'backups')
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const backupsDirExists = await fs.pathExists(this.backupPath)
|
const backupsDirExists = await fs.pathExists(this.backupPath)
|
||||||
if (!backupsDirExists) {
|
if (!backupsDirExists) {
|
||||||
await fs.ensureDir(this.backupPath)
|
await fs.ensureDir(this.backupPath)
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`[BackupManager] Failed to ensure backup directory at "${this.backupPath}": ${error.message}`)
|
||||||
|
// Attempt to fall back to default path
|
||||||
|
const defaultBackupPath = Path.join(global.MetadataPath, 'backups')
|
||||||
|
if (this.backupPath !== defaultBackupPath) {
|
||||||
|
Logger.info(`[BackupManager] Attempting to use default backup path: "${defaultBackupPath}"`)
|
||||||
|
global.ServerSettings.backupPath = defaultBackupPath
|
||||||
|
try {
|
||||||
|
await fs.ensureDir(defaultBackupPath)
|
||||||
|
} catch (fallbackError) {
|
||||||
|
Logger.error(`[BackupManager] Failed to create default backup directory: ${fallbackError.message}`)
|
||||||
|
throw new Error(`[BackupManager] Failed to create default backup directory at "${defaultBackupPath}"`, { cause: fallbackError })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(`[BackupManager] Failed to ensure backup directory at "${this.backupPath}"`, { cause: error })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await this.loadBackups()
|
await this.loadBackups()
|
||||||
this.scheduleCron()
|
this.scheduleCron()
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,15 @@ class CacheManager {
|
||||||
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')
|
||||||
|
|
||||||
|
try {
|
||||||
await fs.ensureDir(this.CachePath)
|
await fs.ensureDir(this.CachePath)
|
||||||
await fs.ensureDir(this.CoverCachePath)
|
await fs.ensureDir(this.CoverCachePath)
|
||||||
await fs.ensureDir(this.ImageCachePath)
|
await fs.ensureDir(this.ImageCachePath)
|
||||||
await fs.ensureDir(this.ItemCachePath)
|
await fs.ensureDir(this.ItemCachePath)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`[CacheManager] Failed to create cache directories at "${this.CachePath}": ${error.message}`)
|
||||||
|
throw new Error(`[CacheManager] Failed to create cache directories at "${this.CachePath}"`, { cause: error })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleCoverCache(res, libraryItemId, options = {}) {
|
async handleCoverCache(res, libraryItemId, options = {}) {
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,13 @@ class LogManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureLogDirs() {
|
async ensureLogDirs() {
|
||||||
|
try {
|
||||||
await fs.ensureDir(this.DailyLogPath)
|
await fs.ensureDir(this.DailyLogPath)
|
||||||
await fs.ensureDir(this.ScanLogPath)
|
await fs.ensureDir(this.ScanLogPath)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[LogManager] Failed to create log directories at "${this.DailyLogPath}": ${error.message}`)
|
||||||
|
throw new Error(`[LogManager] Failed to create log directories at "${this.DailyLogPath}"`, { cause: error })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -110,12 +115,12 @@ class LogManager {
|
||||||
const 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)
|
this.dailyLogFiles = this.dailyLogFiles.filter((dlf) => dlf !== filename)
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await fs.unlink(fullPath)
|
await fs.unlink(fullPath)
|
||||||
Logger.info(TAG, 'Removed daily log: ' + filename)
|
Logger.info(TAG, 'Removed daily log: ' + filename)
|
||||||
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf !== filename)
|
this.dailyLogFiles = this.dailyLogFiles.filter((dlf) => dlf !== filename)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(TAG, 'Failed to unlink log file ' + fullPath)
|
Logger.error(TAG, 'Failed to unlink log file ' + fullPath)
|
||||||
}
|
}
|
||||||
|
|
@ -161,7 +166,7 @@ class LogManager {
|
||||||
const logsDir = Path.join(global.MetadataPath, 'logs')
|
const logsDir = Path.join(global.MetadataPath, 'logs')
|
||||||
await fs.ensureDir(logsDir)
|
await fs.ensureDir(logsDir)
|
||||||
const crashLogPath = Path.join(logsDir, 'crash_logs.txt')
|
const crashLogPath = Path.join(logsDir, 'crash_logs.txt')
|
||||||
return fs.writeFile(crashLogPath, line, { flag: "a+" }).catch((error) => {
|
return fs.writeFile(crashLogPath, line, { flag: 'a+' }).catch((error) => {
|
||||||
console.log('[LogManager] Appended crash log', error)
|
console.log('[LogManager] Appended crash log', error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,12 @@ class MigrationManager {
|
||||||
if (!(await fs.pathExists(this.configPath))) throw new Error(`Config path does not exist: ${this.configPath}`)
|
if (!(await fs.pathExists(this.configPath))) throw new Error(`Config path does not exist: ${this.configPath}`)
|
||||||
|
|
||||||
this.migrationsDir = path.join(this.configPath, 'migrations')
|
this.migrationsDir = path.join(this.configPath, 'migrations')
|
||||||
|
try {
|
||||||
await fs.ensureDir(this.migrationsDir)
|
await fs.ensureDir(this.migrationsDir)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`[MigrationManager] Failed to create migrations directory at "${this.migrationsDir}": ${error.message}`)
|
||||||
|
throw new Error(`[MigrationManager] Failed to create migrations directory at "${this.migrationsDir}"`, { cause: error })
|
||||||
|
}
|
||||||
|
|
||||||
this.serverVersion = this.extractVersionFromTag(serverVersion)
|
this.serverVersion = this.extractVersionFromTag(serverVersion)
|
||||||
if (!this.serverVersion) throw new Error(`Invalid server version: ${serverVersion}. Expected a version tag like v1.2.3.`)
|
if (!this.serverVersion) throw new Error(`Invalid server version: ${serverVersion}. Expected a version tag like v1.2.3.`)
|
||||||
|
|
|
||||||
|
|
@ -459,7 +459,12 @@ class PlaybackSessionManager {
|
||||||
* Remove all stream folders in `/metadata/streams`
|
* Remove all stream folders in `/metadata/streams`
|
||||||
*/
|
*/
|
||||||
async removeOrphanStreams() {
|
async removeOrphanStreams() {
|
||||||
|
try {
|
||||||
await fs.ensureDir(this.StreamsPath)
|
await fs.ensureDir(this.StreamsPath)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`[PlaybackSessionManager] Failed to create streams directory at "${this.StreamsPath}": ${error.message}`)
|
||||||
|
throw new Error(`[PlaybackSessionManager] Failed to create streams directory at "${this.StreamsPath}"`, { cause: error })
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const streamsInPath = await fs.readdir(this.StreamsPath)
|
const streamsInPath = await fs.readdir(this.StreamsPath)
|
||||||
for (const streamId of streamsInPath) {
|
for (const streamId of streamsInPath) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue