mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-04-11 01:17:50 +02:00
Update:Parse epub cover image uses cover specified in opf meta #3201
This commit is contained in:
parent
88693d73bd
commit
b524cbd1b3
@ -4,7 +4,6 @@ const StreamZip = require('../../libs/nodeStreamZip')
|
|||||||
const parseOpfMetadata = require('./parseOpfMetadata')
|
const parseOpfMetadata = require('./parseOpfMetadata')
|
||||||
const { xmlToJSON } = require('../index')
|
const { xmlToJSON } = require('../index')
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract file from epub and return string content
|
* Extract file from epub and return string content
|
||||||
*
|
*
|
||||||
@ -49,7 +48,10 @@ async function extractXmlToJson(epubPath, xmlFilepath) {
|
|||||||
async function extractCoverImage(epubPath, epubImageFilepath, outputCoverPath) {
|
async function extractCoverImage(epubPath, epubImageFilepath, outputCoverPath) {
|
||||||
const zip = new StreamZip.async({ file: epubPath })
|
const zip = new StreamZip.async({ file: epubPath })
|
||||||
|
|
||||||
const success = await zip.extract(epubImageFilepath, outputCoverPath).then(() => true).catch((error) => {
|
const success = await zip
|
||||||
|
.extract(epubImageFilepath, outputCoverPath)
|
||||||
|
.then(() => true)
|
||||||
|
.catch((error) => {
|
||||||
Logger.error(`[parseEpubMetadata] Failed to extract image ${epubImageFilepath} from epub at "${epubPath}"`, error)
|
Logger.error(`[parseEpubMetadata] Failed to extract image ${epubImageFilepath} from epub at "${epubPath}"`, error)
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
@ -89,7 +91,7 @@ async function parse(ebookFile) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse metadata from package document opf file
|
// Parse metadata from package document opf file
|
||||||
const opfMetadata = parseOpfMetadata.parseOpfMetadataJson(packageJson)
|
const opfMetadata = parseOpfMetadata.parseOpfMetadataJson(structuredClone(packageJson))
|
||||||
if (!opfMetadata) {
|
if (!opfMetadata) {
|
||||||
Logger.error(`Unable to parse metadata in package doc with json`, JSON.stringify(packageJson, null, 2))
|
Logger.error(`Unable to parse metadata in package doc with json`, JSON.stringify(packageJson, null, 2))
|
||||||
return null
|
return null
|
||||||
@ -101,8 +103,23 @@ async function parse(ebookFile) {
|
|||||||
metadata: opfMetadata
|
metadata: opfMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to find filepath to cover image
|
// Attempt to find filepath to cover image:
|
||||||
const manifestFirstImage = packageJson.package?.manifest?.[0]?.item?.find(item => item.$?.['media-type']?.startsWith('image/'))
|
// Metadata may include <meta name="cover" content="id"/> where content is the id of the cover image in the manifest
|
||||||
|
// Otherwise the first image in the manifest is used as the cover image
|
||||||
|
let packageMetadata = packageJson.package?.metadata
|
||||||
|
if (Array.isArray(packageMetadata)) {
|
||||||
|
packageMetadata = packageMetadata[0]
|
||||||
|
}
|
||||||
|
const metaCoverId = packageMetadata?.meta?.find?.((meta) => meta.$?.name === 'cover')?.$?.content
|
||||||
|
|
||||||
|
let manifestFirstImage = null
|
||||||
|
if (metaCoverId) {
|
||||||
|
manifestFirstImage = packageJson.package?.manifest?.[0]?.item?.find((item) => item.$?.id === metaCoverId)
|
||||||
|
}
|
||||||
|
if (!manifestFirstImage) {
|
||||||
|
manifestFirstImage = packageJson.package?.manifest?.[0]?.item?.find((item) => item.$?.['media-type']?.startsWith('image/'))
|
||||||
|
}
|
||||||
|
|
||||||
let coverImagePath = manifestFirstImage?.$?.href
|
let coverImagePath = manifestFirstImage?.$?.href
|
||||||
if (coverImagePath) {
|
if (coverImagePath) {
|
||||||
const packageDirname = Path.dirname(packageDocPath)
|
const packageDirname = Path.dirname(packageDocPath)
|
||||||
|
Loading…
Reference in New Issue
Block a user