Fix a regression that broke reading of covers from HTMLZ and TXTZ files. Fixes #2038778 [Book cover is not shown after converting from FB2 to HTMLZ](https://bugs.launchpad.net/calibre/+bug/2038778)

This commit is contained in:
Kovid Goyal 2023-10-09 20:58:05 +05:30
parent f70c8e3d6b
commit 97825e0467
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -23,17 +23,22 @@ def get_metadata(stream, extract_cover=True):
try: try:
with ZipFile(stream) as zf: with ZipFile(stream) as zf:
opf_name = get_first_opf_name(zf) opf_name = get_first_opf_name(zf)
opf_stream = io.BytesIO(zf.read(opf_name)) with zf.open(opf_name) as opf_stream:
opf = OPF(opf_stream) opf = OPF(opf_stream)
mi = opf.to_book_metadata() mi = opf.to_book_metadata()
if extract_cover: if extract_cover:
cover_href = opf.raster_cover cover_href = opf.raster_cover or opf.guide_raster_cover
if not cover_href: if not cover_href:
for meta in opf.metadata.xpath('//*[local-name()="meta" and @name="cover"]'): for meta in opf.metadata.xpath('//*[local-name()="meta" and @name="cover"]'):
val = meta.get('content') val = meta.get('content')
if val.rpartition('.')[2].lower() in {'jpeg', 'jpg', 'png'}: if val.rpartition('.')[2].lower() in {'jpeg', 'jpg', 'png'}:
cover_href = val cover_href = val
break break
else:
for val in opf.guide_cover_path(opf.root):
if val.rpartition('.')[2].lower() in {'jpeg', 'jpg', 'png'}:
cover_href = val
break
if cover_href: if cover_href:
try: try:
mi.cover_data = (os.path.splitext(cover_href)[1], zf.read(cover_href)) mi.cover_data = (os.path.splitext(cover_href)[1], zf.read(cover_href))