mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-11-03 19:07:00 -05:00 
			
		
		
		
	Merge pull request #3422 from mikiher/parse-comic-metadata-try-catch
Catch file extraction errors in parseComicMetadata
This commit is contained in:
		
						commit
						fa0c90de70
					
				@ -13,13 +13,13 @@ const parseComicInfoMetadata = require('./parseComicInfoMetadata')
 | 
			
		||||
 */
 | 
			
		||||
async function getComicFileBuffer(filepath) {
 | 
			
		||||
  if (!(await fs.pathExists(filepath))) {
 | 
			
		||||
    Logger.error(`Comic path does not exist "${filepath}"`)
 | 
			
		||||
    Logger.error(`[parseComicMetadata] Comic path does not exist "${filepath}"`)
 | 
			
		||||
    return null
 | 
			
		||||
  }
 | 
			
		||||
  try {
 | 
			
		||||
    return fs.readFile(filepath)
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    Logger.error(`Failed to read comic at "${filepath}"`, error)
 | 
			
		||||
    Logger.error(`[parseComicMetadata] Failed to read comic at "${filepath}"`, error)
 | 
			
		||||
    return null
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -36,7 +36,9 @@ async function extractCoverImage(comicPath, comicImageFilepath, outputCoverPath)
 | 
			
		||||
  const comicFileBuffer = await getComicFileBuffer(comicPath)
 | 
			
		||||
  if (!comicFileBuffer) return null
 | 
			
		||||
 | 
			
		||||
  const archive = await Archive.open(comicFileBuffer)
 | 
			
		||||
  let archive = null
 | 
			
		||||
  try {
 | 
			
		||||
    archive = await Archive.open(comicFileBuffer)
 | 
			
		||||
    const fileEntry = await archive.extractSingleFile(comicImageFilepath)
 | 
			
		||||
 | 
			
		||||
    if (!fileEntry?.fileData) {
 | 
			
		||||
@ -44,15 +46,15 @@ async function extractCoverImage(comicPath, comicImageFilepath, outputCoverPath)
 | 
			
		||||
      return false
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  try {
 | 
			
		||||
    await fs.writeFile(outputCoverPath, fileEntry.fileData)
 | 
			
		||||
 | 
			
		||||
    return true
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    Logger.error(`[parseComicMetadata] Failed to extract image from comicPath "${comicPath}"`, error)
 | 
			
		||||
    Logger.error(`[parseComicMetadata] Failed to extract image "${comicImageFilepath}" from comicPath "${comicPath}" into "${outputCoverPath}"`, error)
 | 
			
		||||
    return false
 | 
			
		||||
  } finally {
 | 
			
		||||
    // Ensure we free the memory
 | 
			
		||||
    archive.close()
 | 
			
		||||
    archive?.close()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
module.exports.extractCoverImage = extractCoverImage
 | 
			
		||||
@ -70,7 +72,9 @@ async function parse(ebookFile) {
 | 
			
		||||
  const comicFileBuffer = await getComicFileBuffer(comicPath)
 | 
			
		||||
  if (!comicFileBuffer) return null
 | 
			
		||||
 | 
			
		||||
  const archive = await Archive.open(comicFileBuffer)
 | 
			
		||||
  let archive = null
 | 
			
		||||
  try {
 | 
			
		||||
    archive = await Archive.open(comicFileBuffer)
 | 
			
		||||
 | 
			
		||||
    const fileObjects = await archive.getFilesArray()
 | 
			
		||||
 | 
			
		||||
@ -104,12 +108,16 @@ async function parse(ebookFile) {
 | 
			
		||||
    if (firstImage?.file?._path) {
 | 
			
		||||
      payload.ebookCoverPath = firstImage.file._path
 | 
			
		||||
    } else {
 | 
			
		||||
    Logger.warn(`Cover image not found in comic at "${comicPath}"`)
 | 
			
		||||
      Logger.warn(`[parseComicMetadata] Cover image not found in comic at "${comicPath}"`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  // Ensure we close the archive to free memory
 | 
			
		||||
  archive.close()
 | 
			
		||||
 | 
			
		||||
    return payload
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    Logger.error(`[parseComicMetadata] Failed to parse comic metadata at "${comicPath}"`, error)
 | 
			
		||||
    return null
 | 
			
		||||
  } finally {
 | 
			
		||||
    // Ensure we free the memory
 | 
			
		||||
    archive?.close()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
module.exports.parse = parse
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user