Handle invalid or missing container.xml during kobo sync (Fix for #2840)

This commit is contained in:
Ozzie Isaacs 2023-07-29 15:32:35 +02:00
parent 279f0569e4
commit 2f12b2e315
1 changed files with 15 additions and 9 deletions

View File

@ -21,10 +21,11 @@ import zipfile
from lxml import etree from lxml import etree
from . import isoLanguages, cover from . import isoLanguages, cover
from . import config from . import config, logger
from .helper import split_authors from .helper import split_authors
from .constants import BookMeta from .constants import BookMeta
log = logger.create()
def _extract_cover(zip_file, cover_file, cover_path, tmp_file_name): def _extract_cover(zip_file, cover_file, cover_path, tmp_file_name):
if cover_file is None: if cover_file is None:
@ -49,15 +50,20 @@ def get_epub_layout(book, book_data):
} }
file_path = os.path.normpath(os.path.join(config.config_calibre_dir, book.path, book_data.name + "." + book_data.format.lower())) file_path = os.path.normpath(os.path.join(config.config_calibre_dir, book.path, book_data.name + "." + book_data.format.lower()))
epubZip = zipfile.ZipFile(file_path) try:
txt = epubZip.read('META-INF/container.xml') epubZip = zipfile.ZipFile(file_path)
tree = etree.fromstring(txt) txt = epubZip.read('META-INF/container.xml')
cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0] tree = etree.fromstring(txt)
cf = epubZip.read(cfname) cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0]
tree = etree.fromstring(cf) cf = epubZip.read(cfname)
p = tree.xpath('/pkg:package/pkg:metadata', namespaces=ns)[0]
layout = p.xpath('pkg:meta[@property="rendition:layout"]/text()', namespaces=ns) tree = etree.fromstring(cf)
p = tree.xpath('/pkg:package/pkg:metadata', namespaces=ns)[0]
layout = p.xpath('pkg:meta[@property="rendition:layout"]/text()', namespaces=ns)
except (etree.XMLSyntaxError, KeyError) as e:
log.error("Could not parse epub metadata of book {} during kobo sync: {}".format(book.id, e))
layout = []
if len(layout) == 0: if len(layout) == 0:
return None return None