Fix:Server crash on uploadCover temp file mv failed #3685

This commit is contained in:
advplyr 2024-12-06 16:59:34 -06:00
parent 890b0b949e
commit 9a1c773b7a

View File

@ -12,7 +12,7 @@ const parseEbookMetadata = require('../utils/parsers/parseEbookMetadata')
const CacheManager = require('../managers/CacheManager') const CacheManager = require('../managers/CacheManager')
class CoverManager { class CoverManager {
constructor() { } constructor() {}
getCoverDirectory(libraryItem) { getCoverDirectory(libraryItem) {
if (global.ServerSettings.storeCoverWithItem && !libraryItem.isFile) { if (global.ServerSettings.storeCoverWithItem && !libraryItem.isFile) {
@ -93,10 +93,13 @@ class CoverManager {
const coverFullPath = Path.posix.join(coverDirPath, `cover${extname}`) const coverFullPath = Path.posix.join(coverDirPath, `cover${extname}`)
// Move cover from temp upload dir to destination // Move cover from temp upload dir to destination
const success = await coverFile.mv(coverFullPath).then(() => true).catch((error) => { const success = await coverFile
Logger.error('[CoverManager] Failed to move cover file', path, error) .mv(coverFullPath)
return false .then(() => true)
}) .catch((error) => {
Logger.error('[CoverManager] Failed to move cover file', coverFullPath, error)
return false
})
if (!success) { if (!success) {
return { return {
@ -124,11 +127,13 @@ class CoverManager {
var temppath = Path.posix.join(coverDirPath, 'cover') var temppath = Path.posix.join(coverDirPath, 'cover')
let errorMsg = '' let errorMsg = ''
let success = await downloadImageFile(url, temppath).then(() => true).catch((err) => { let success = await downloadImageFile(url, temppath)
errorMsg = err.message || 'Unknown error' .then(() => true)
Logger.error(`[CoverManager] Download image file failed for "${url}"`, errorMsg) .catch((err) => {
return false errorMsg = err.message || 'Unknown error'
}) Logger.error(`[CoverManager] Download image file failed for "${url}"`, errorMsg)
return false
})
if (!success) { if (!success) {
return { return {
error: 'Failed to download image from url: ' + errorMsg error: 'Failed to download image from url: ' + errorMsg
@ -180,7 +185,7 @@ class CoverManager {
} }
// Cover path does not exist // Cover path does not exist
if (!await fs.pathExists(coverPath)) { if (!(await fs.pathExists(coverPath))) {
Logger.error(`[CoverManager] validate cover path does not exist "${coverPath}"`) Logger.error(`[CoverManager] validate cover path does not exist "${coverPath}"`)
return { return {
error: 'Cover path does not exist' error: 'Cover path does not exist'
@ -188,7 +193,7 @@ class CoverManager {
} }
// Cover path is not a file // Cover path is not a file
if (!await checkPathIsFile(coverPath)) { if (!(await checkPathIsFile(coverPath))) {
Logger.error(`[CoverManager] validate cover path is not a file "${coverPath}"`) Logger.error(`[CoverManager] validate cover path is not a file "${coverPath}"`)
return { return {
error: 'Cover path is not a file' error: 'Cover path is not a file'
@ -211,10 +216,13 @@ class CoverManager {
var newCoverPath = Path.posix.join(coverDirPath, coverFilename) var newCoverPath = Path.posix.join(coverDirPath, coverFilename)
Logger.debug(`[CoverManager] validate cover path copy cover from "${coverPath}" to "${newCoverPath}"`) Logger.debug(`[CoverManager] validate cover path copy cover from "${coverPath}" to "${newCoverPath}"`)
var copySuccess = await fs.copy(coverPath, newCoverPath, { overwrite: true }).then(() => true).catch((error) => { var copySuccess = await fs
Logger.error(`[CoverManager] validate cover path failed to copy cover`, error) .copy(coverPath, newCoverPath, { overwrite: true })
return false .then(() => true)
}) .catch((error) => {
Logger.error(`[CoverManager] validate cover path failed to copy cover`, error)
return false
})
if (!copySuccess) { if (!copySuccess) {
return { return {
error: 'Failed to copy cover to dir' error: 'Failed to copy cover to dir'
@ -236,14 +244,14 @@ class CoverManager {
/** /**
* Extract cover art from audio file and save for library item * Extract cover art from audio file and save for library item
* *
* @param {import('../models/Book').AudioFileObject[]} audioFiles * @param {import('../models/Book').AudioFileObject[]} audioFiles
* @param {string} libraryItemId * @param {string} libraryItemId
* @param {string} [libraryItemPath] null for isFile library items * @param {string} [libraryItemPath] null for isFile library items
* @returns {Promise<string>} returns cover path * @returns {Promise<string>} returns cover path
*/ */
async saveEmbeddedCoverArt(audioFiles, libraryItemId, libraryItemPath) { async saveEmbeddedCoverArt(audioFiles, libraryItemId, libraryItemPath) {
let audioFileWithCover = audioFiles.find(af => af.embeddedCoverArt) let audioFileWithCover = audioFiles.find((af) => af.embeddedCoverArt)
if (!audioFileWithCover) return null if (!audioFileWithCover) return null
let coverDirPath = null let coverDirPath = null
@ -273,10 +281,10 @@ class CoverManager {
/** /**
* Extract cover art from ebook and save for library item * Extract cover art from ebook and save for library item
* *
* @param {import('../utils/parsers/parseEbookMetadata').EBookFileScanData} ebookFileScanData * @param {import('../utils/parsers/parseEbookMetadata').EBookFileScanData} ebookFileScanData
* @param {string} libraryItemId * @param {string} libraryItemId
* @param {string} [libraryItemPath] null for isFile library items * @param {string} [libraryItemPath] null for isFile library items
* @returns {Promise<string>} returns cover path * @returns {Promise<string>} returns cover path
*/ */
async saveEbookCoverArt(ebookFileScanData, libraryItemId, libraryItemPath) { async saveEbookCoverArt(ebookFileScanData, libraryItemId, libraryItemPath) {
@ -310,9 +318,9 @@ class CoverManager {
} }
/** /**
* *
* @param {string} url * @param {string} url
* @param {string} libraryItemId * @param {string} libraryItemId
* @param {string} [libraryItemPath] null if library item isFile or is from adding new podcast * @param {string} [libraryItemPath] null if library item isFile or is from adding new podcast
* @returns {Promise<{error:string}|{cover:string}>} * @returns {Promise<{error:string}|{cover:string}>}
*/ */
@ -328,10 +336,12 @@ class CoverManager {
await fs.ensureDir(coverDirPath) await fs.ensureDir(coverDirPath)
const temppath = Path.posix.join(coverDirPath, 'cover') const temppath = Path.posix.join(coverDirPath, 'cover')
const success = await downloadImageFile(url, temppath).then(() => true).catch((err) => { const success = await downloadImageFile(url, temppath)
Logger.error(`[CoverManager] Download image file failed for "${url}"`, err) .then(() => true)
return false .catch((err) => {
}) Logger.error(`[CoverManager] Download image file failed for "${url}"`, err)
return false
})
if (!success) { if (!success) {
return { return {
error: 'Failed to download image from url' error: 'Failed to download image from url'
@ -361,4 +371,4 @@ class CoverManager {
} }
} }
} }
module.exports = new CoverManager() module.exports = new CoverManager()