From 4de65b43692ea3fdd0526d572a17583e3be96755 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Mon, 19 Aug 2024 21:00:16 -0700 Subject: [PATCH 1/2] Autoformat `parseComicMetadata` --- server/utils/parsers/parseComicMetadata.js | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/server/utils/parsers/parseComicMetadata.js b/server/utils/parsers/parseComicMetadata.js index 8d78b12675..3f6c3fb7f0 100644 --- a/server/utils/parsers/parseComicMetadata.js +++ b/server/utils/parsers/parseComicMetadata.js @@ -7,12 +7,12 @@ const { xmlToJSON } = require('../index') const parseComicInfoMetadata = require('./parseComicInfoMetadata') /** - * - * @param {string} filepath + * + * @param {string} filepath * @returns {Promise} */ async function getComicFileBuffer(filepath) { - if (!await fs.pathExists(filepath)) { + if (!(await fs.pathExists(filepath))) { Logger.error(`Comic path does not exist "${filepath}"`) return null } @@ -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} */ async function extractCoverImage(comicPath, comicImageFilepath, outputCoverPath) { @@ -56,8 +56,8 @@ module.exports.extractCoverImage = extractCoverImage /** * Parse metadata from comic - * - * @param {import('../../models/Book').EBookFileObject} ebookFile + * + * @param {import('../../models/Book').EBookFileObject} ebookFile * @returns {Promise} */ async function parse(ebookFile) { @@ -79,7 +79,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) { @@ -97,7 +97,7 @@ 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 { @@ -106,4 +106,4 @@ async function parse(ebookFile) { return payload } -module.exports.parse = parse \ No newline at end of file +module.exports.parse = parse From 9c87c3a095d6c9de30f3b9c1a2daa4bc3e2789b8 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Mon, 19 Aug 2024 22:05:25 -0700 Subject: [PATCH 2/2] Free memory after extracting comic --- server/utils/parsers/parseComicMetadata.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/utils/parsers/parseComicMetadata.js b/server/utils/parsers/parseComicMetadata.js index 3f6c3fb7f0..77e5ec2ac3 100644 --- a/server/utils/parsers/parseComicMetadata.js +++ b/server/utils/parsers/parseComicMetadata.js @@ -50,6 +50,9 @@ 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 @@ -104,6 +107,9 @@ async function parse(ebookFile) { 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