Skip to content

Commit 9a1c773

Browse files
committed
Fix:Server crash on uploadCover temp file mv failed #3685
1 parent 890b0b9 commit 9a1c773

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

server/managers/CoverManager.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const parseEbookMetadata = require('../utils/parsers/parseEbookMetadata')
1212
const CacheManager = require('../managers/CacheManager')
1313

1414
class CoverManager {
15-
constructor() { }
15+
constructor() {}
1616

1717
getCoverDirectory(libraryItem) {
1818
if (global.ServerSettings.storeCoverWithItem && !libraryItem.isFile) {
@@ -93,10 +93,13 @@ class CoverManager {
9393
const coverFullPath = Path.posix.join(coverDirPath, `cover${extname}`)
9494

9595
// Move cover from temp upload dir to destination
96-
const success = await coverFile.mv(coverFullPath).then(() => true).catch((error) => {
97-
Logger.error('[CoverManager] Failed to move cover file', path, error)
98-
return false
99-
})
96+
const success = await coverFile
97+
.mv(coverFullPath)
98+
.then(() => true)
99+
.catch((error) => {
100+
Logger.error('[CoverManager] Failed to move cover file', coverFullPath, error)
101+
return false
102+
})
100103

101104
if (!success) {
102105
return {
@@ -124,11 +127,13 @@ class CoverManager {
124127
var temppath = Path.posix.join(coverDirPath, 'cover')
125128

126129
let errorMsg = ''
127-
let success = await downloadImageFile(url, temppath).then(() => true).catch((err) => {
128-
errorMsg = err.message || 'Unknown error'
129-
Logger.error(`[CoverManager] Download image file failed for "${url}"`, errorMsg)
130-
return false
131-
})
130+
let success = await downloadImageFile(url, temppath)
131+
.then(() => true)
132+
.catch((err) => {
133+
errorMsg = err.message || 'Unknown error'
134+
Logger.error(`[CoverManager] Download image file failed for "${url}"`, errorMsg)
135+
return false
136+
})
132137
if (!success) {
133138
return {
134139
error: 'Failed to download image from url: ' + errorMsg
@@ -180,15 +185,15 @@ class CoverManager {
180185
}
181186

182187
// Cover path does not exist
183-
if (!await fs.pathExists(coverPath)) {
188+
if (!(await fs.pathExists(coverPath))) {
184189
Logger.error(`[CoverManager] validate cover path does not exist "${coverPath}"`)
185190
return {
186191
error: 'Cover path does not exist'
187192
}
188193
}
189194

190195
// Cover path is not a file
191-
if (!await checkPathIsFile(coverPath)) {
196+
if (!(await checkPathIsFile(coverPath))) {
192197
Logger.error(`[CoverManager] validate cover path is not a file "${coverPath}"`)
193198
return {
194199
error: 'Cover path is not a file'
@@ -211,10 +216,13 @@ class CoverManager {
211216
var newCoverPath = Path.posix.join(coverDirPath, coverFilename)
212217
Logger.debug(`[CoverManager] validate cover path copy cover from "${coverPath}" to "${newCoverPath}"`)
213218

214-
var copySuccess = await fs.copy(coverPath, newCoverPath, { overwrite: true }).then(() => true).catch((error) => {
215-
Logger.error(`[CoverManager] validate cover path failed to copy cover`, error)
216-
return false
217-
})
219+
var copySuccess = await fs
220+
.copy(coverPath, newCoverPath, { overwrite: true })
221+
.then(() => true)
222+
.catch((error) => {
223+
Logger.error(`[CoverManager] validate cover path failed to copy cover`, error)
224+
return false
225+
})
218226
if (!copySuccess) {
219227
return {
220228
error: 'Failed to copy cover to dir'
@@ -236,14 +244,14 @@ class CoverManager {
236244

237245
/**
238246
* Extract cover art from audio file and save for library item
239-
*
240-
* @param {import('../models/Book').AudioFileObject[]} audioFiles
241-
* @param {string} libraryItemId
242-
* @param {string} [libraryItemPath] null for isFile library items
247+
*
248+
* @param {import('../models/Book').AudioFileObject[]} audioFiles
249+
* @param {string} libraryItemId
250+
* @param {string} [libraryItemPath] null for isFile library items
243251
* @returns {Promise<string>} returns cover path
244252
*/
245253
async saveEmbeddedCoverArt(audioFiles, libraryItemId, libraryItemPath) {
246-
let audioFileWithCover = audioFiles.find(af => af.embeddedCoverArt)
254+
let audioFileWithCover = audioFiles.find((af) => af.embeddedCoverArt)
247255
if (!audioFileWithCover) return null
248256

249257
let coverDirPath = null
@@ -273,10 +281,10 @@ class CoverManager {
273281

274282
/**
275283
* Extract cover art from ebook and save for library item
276-
*
277-
* @param {import('../utils/parsers/parseEbookMetadata').EBookFileScanData} ebookFileScanData
278-
* @param {string} libraryItemId
279-
* @param {string} [libraryItemPath] null for isFile library items
284+
*
285+
* @param {import('../utils/parsers/parseEbookMetadata').EBookFileScanData} ebookFileScanData
286+
* @param {string} libraryItemId
287+
* @param {string} [libraryItemPath] null for isFile library items
280288
* @returns {Promise<string>} returns cover path
281289
*/
282290
async saveEbookCoverArt(ebookFileScanData, libraryItemId, libraryItemPath) {
@@ -310,9 +318,9 @@ class CoverManager {
310318
}
311319

312320
/**
313-
*
314-
* @param {string} url
315-
* @param {string} libraryItemId
321+
*
322+
* @param {string} url
323+
* @param {string} libraryItemId
316324
* @param {string} [libraryItemPath] null if library item isFile or is from adding new podcast
317325
* @returns {Promise<{error:string}|{cover:string}>}
318326
*/
@@ -328,10 +336,12 @@ class CoverManager {
328336
await fs.ensureDir(coverDirPath)
329337

330338
const temppath = Path.posix.join(coverDirPath, 'cover')
331-
const success = await downloadImageFile(url, temppath).then(() => true).catch((err) => {
332-
Logger.error(`[CoverManager] Download image file failed for "${url}"`, err)
333-
return false
334-
})
339+
const success = await downloadImageFile(url, temppath)
340+
.then(() => true)
341+
.catch((err) => {
342+
Logger.error(`[CoverManager] Download image file failed for "${url}"`, err)
343+
return false
344+
})
335345
if (!success) {
336346
return {
337347
error: 'Failed to download image from url'
@@ -361,4 +371,4 @@ class CoverManager {
361371
}
362372
}
363373
}
364-
module.exports = new CoverManager()
374+
module.exports = new CoverManager()

0 commit comments

Comments
 (0)