Skip to content

Commit

Permalink
Merge pull request #3312 from nichwall/close_comics_during_scan
Browse files Browse the repository at this point in the history
Close comics during scan
  • Loading branch information
advplyr authored Aug 21, 2024
2 parents 5f572fa + 9c87c3a commit f66cea9
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions server/utils/parsers/parseComicMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { xmlToJSON } = require('../index')
const parseComicInfoMetadata = require('./parseComicInfoMetadata')

/**
*
* @param {string} filepath
*
* @param {string} filepath
* @returns {Promise<Buffer>}
*/
async function getComicFileBuffer(filepath) {
if (!await fs.pathExists(filepath)) {
if (!(await fs.pathExists(filepath))) {
Logger.error(`Comic path does not exist "${filepath}"`)
return null
}
Expand All @@ -26,10 +26,10 @@ async function getComicFileBuffer(filepath) {

/**
* Extract cover image from comic return true if success
*
* @param {string} comicPath
* @param {string} comicImageFilepath
* @param {string} outputCoverPath
*
* @param {string} comicPath
* @param {string} comicImageFilepath
* @param {string} outputCoverPath
* @returns {Promise<boolean>}
*/
async function extractCoverImage(comicPath, comicImageFilepath, outputCoverPath) {
Expand All @@ -50,14 +50,17 @@ async function extractCoverImage(comicPath, comicImageFilepath, outputCoverPath)
} catch (error) {
Logger.error(`[parseComicMetadata] Failed to extract image from comicPath "${comicPath}"`, error)
return false
} finally {
// Ensure we free the memory
archive.close()
}
}
module.exports.extractCoverImage = extractCoverImage

/**
* Parse metadata from comic
*
* @param {import('../../models/Book').EBookFileObject} ebookFile
*
* @param {import('../../models/Book').EBookFileObject} ebookFile
* @returns {Promise<import('./parseEbookMetadata').EBookFileScanData>}
*/
async function parse(ebookFile) {
Expand All @@ -79,7 +82,7 @@ async function parse(ebookFile) {
})

let metadata = null
const comicInfo = fileObjects.find(fo => fo.file.name === 'ComicInfo.xml')
const comicInfo = fileObjects.find((fo) => fo.file.name === 'ComicInfo.xml')
if (comicInfo) {
const comicInfoEntry = await comicInfo.file.extract()
if (comicInfoEntry?.fileData) {
Expand All @@ -97,13 +100,16 @@ async function parse(ebookFile) {
metadata
}

const firstImage = fileObjects.find(fo => globals.SupportedImageTypes.includes(Path.extname(fo.file.name).toLowerCase().slice(1)))
const firstImage = fileObjects.find((fo) => globals.SupportedImageTypes.includes(Path.extname(fo.file.name).toLowerCase().slice(1)))
if (firstImage?.file?._path) {
payload.ebookCoverPath = firstImage.file._path
} else {
Logger.warn(`Cover image not found in comic at "${comicPath}"`)
}

// Ensure we close the archive to free memory
archive.close()

return payload
}
module.exports.parse = parse
module.exports.parse = parse

0 comments on commit f66cea9

Please sign in to comment.