From 35f29ca22b5fc84a49b4d8cc6182bc7db6d824dc Mon Sep 17 00:00:00 2001 From: jmt-gh Date: Mon, 6 Jun 2022 08:12:58 -0700 Subject: [PATCH 1/5] Use ensureDir instead of mkdir to fix 698 This commit updates the mkdir for creating the download location to ensureDir, which is an alias for mkdirs and mkdirp, meaning they will create the entire path of the directory if it does not exist. https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir.md --- server/managers/AbMergeManager.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 5eb59b7f..d01256e0 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -73,9 +73,10 @@ class AbMergeManager { try { - await fs.mkdir(download.dirpath) + await fs.ensureDir(download.dirpath) } catch (error) { Logger.error(`[AbMergeManager] Failed to make directory ${download.dirpath}`) + Logger.debug(`[AbMergeManager] Make directory error: ${error}`) var downloadJson = download.toJSON() this.clientEmitter(user.id, 'abmerge_failed', downloadJson) return From c606a41314019de9b8a13dee1d72881eeb07121a Mon Sep 17 00:00:00 2001 From: jmt-gh Date: Mon, 6 Jun 2022 08:18:15 -0700 Subject: [PATCH 2/5] update Cache folder creation to leverage ensureDirs --- server/managers/CacheManager.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index 3a0e5bf1..c707ed0f 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -19,17 +19,17 @@ class CacheManager { var pathsCreated = false if (!(await fs.pathExists(this.CachePath))) { - await fs.mkdir(this.CachePath) + await fs.ensureDir(this.CachePath) pathsCreated = true } if (!(await fs.pathExists(this.CoverCachePath))) { - await fs.mkdir(this.CoverCachePath) + await fs.ensureDir(this.CoverCachePath) pathsCreated = true } if (!(await fs.pathExists(this.ImageCachePath))) { - await fs.mkdir(this.ImageCachePath) + await fs.ensureDir(this.ImageCachePath) pathsCreated = true } From 853513b92685969d83da769db7c649b69ea9a27b Mon Sep 17 00:00:00 2001 From: jmt-gh Date: Mon, 6 Jun 2022 18:51:08 -0700 Subject: [PATCH 3/5] update approach for ensuring download directory always exists --- server/Server.js | 3 ++- server/managers/AbMergeManager.js | 22 ++++++++++++++++++++-- server/managers/CacheManager.js | 6 +++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/server/Server.js b/server/Server.js index 579d6a79..73fef075 100644 --- a/server/Server.js +++ b/server/Server.js @@ -139,7 +139,8 @@ class Server { await this.checkUserMediaProgress() // Remove invalid user item progress await this.purgeMetadata() // Remove metadata folders without library item await this.cacheManager.ensureCachePaths() - + await this.abMergeManager.ensureDownloadDirPath() + await this.backupManager.init() await this.logManager.init() this.podcastManager.init() diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index d01256e0..5aa40233 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -16,11 +16,28 @@ class AbMergeManager { this.clientEmitter = clientEmitter this.downloadDirPath = Path.join(global.MetadataPath, 'downloads') + this.downloadDirPathExist = false this.pendingDownloads = [] this.downloads = [] } + async ensureDownloadDirPath() { // Creates download path if necessary and sets owner and permissions + if (this.downloadDirPathExist) return + + var pathCreated = false + if (!(await fs.pathExists(this.downloadDirPath))) { + await fs.mkdir(this.downloadDirPath) + pathCreated = true + } + + if (pathCreated) { + await filePerms.setDefault(this.downloadDirPath) + } + + this.downloadDirPathExist = true + } + getDownload(downloadId) { return this.downloads.find(d => d.id === downloadId) } @@ -48,7 +65,7 @@ class AbMergeManager { } catch (error) { return false } - } + }z async startAudiobookMerge(user, libraryItem) { var downloadId = getId('abmerge') @@ -73,7 +90,8 @@ class AbMergeManager { try { - await fs.ensureDir(download.dirpath) + await fs.mkdir(download.dirpath) + Logger.error(`[AbMergeManager] Failed to make directory ${download.dirpath}`) } catch (error) { Logger.error(`[AbMergeManager] Failed to make directory ${download.dirpath}`) Logger.debug(`[AbMergeManager] Make directory error: ${error}`) diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index c707ed0f..3a0e5bf1 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -19,17 +19,17 @@ class CacheManager { var pathsCreated = false if (!(await fs.pathExists(this.CachePath))) { - await fs.ensureDir(this.CachePath) + await fs.mkdir(this.CachePath) pathsCreated = true } if (!(await fs.pathExists(this.CoverCachePath))) { - await fs.ensureDir(this.CoverCachePath) + await fs.mkdir(this.CoverCachePath) pathsCreated = true } if (!(await fs.pathExists(this.ImageCachePath))) { - await fs.ensureDir(this.ImageCachePath) + await fs.mkdir(this.ImageCachePath) pathsCreated = true } From 8d03943acbcdb34de5a98d0e9127cc59dfff6bc1 Mon Sep 17 00:00:00 2001 From: jmt-gh Date: Mon, 6 Jun 2022 18:51:49 -0700 Subject: [PATCH 4/5] remove extra debug log --- server/managers/AbMergeManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 5aa40233..79f1e785 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -91,7 +91,6 @@ class AbMergeManager { try { await fs.mkdir(download.dirpath) - Logger.error(`[AbMergeManager] Failed to make directory ${download.dirpath}`) } catch (error) { Logger.error(`[AbMergeManager] Failed to make directory ${download.dirpath}`) Logger.debug(`[AbMergeManager] Make directory error: ${error}`) From f5aae25cc8bf2f6abde29876c9e50aef84aceda0 Mon Sep 17 00:00:00 2001 From: jmt-gh Date: Mon, 6 Jun 2022 18:52:08 -0700 Subject: [PATCH 5/5] remove random character --- server/managers/AbMergeManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 79f1e785..a0fbcb36 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -65,7 +65,7 @@ class AbMergeManager { } catch (error) { return false } - }z + } async startAudiobookMerge(user, libraryItem) { var downloadId = getId('abmerge')