From 79db6e1f751d766583dee6d70e7736b0bac994b6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 17 Oct 2023 05:24:18 +0530 Subject: [PATCH] TXTZ Output plugin: Only keep images if the text format is one that can reference images. Fixes #2039474 [Images should not be saved when converting to TXTZ](https://bugs.launchpad.net/calibre/+bug/2039474) --- .../ebooks/conversion/plugins/txt_output.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/txt_output.py b/src/calibre/ebooks/conversion/plugins/txt_output.py index 8b38997c65..0de608f34a 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_output.py +++ b/src/calibre/ebooks/conversion/plugins/txt_output.py @@ -127,6 +127,9 @@ class TXTZOutput(TXTOutput): from calibre.ebooks.oeb.base import OEB_IMAGES, xml2str from calibre.utils.zipfile import ZipFile + can_reference_images = opts.txt_output_formatting.lower() in ('markdown', 'textile') + can_reference_images = can_reference_images and opts.keep_image_references + with TemporaryDirectory('_txtz_output') as tdir: # TXT txt_name = 'index.txt' @@ -153,11 +156,14 @@ class TXTZOutput(TXTOutput): else: path = os.path.join(tdir, os.path.dirname(item.href)) href = os.path.basename(item.href) - os.makedirs(path, exist_ok=True) - with open(os.path.join(path, href), 'wb') as imgf: - imgf.write(item.data) - if item.href == cover_href: - cover_relhref = os.path.relpath(imgf.name, tdir).replace(os.sep, '/') + ipath = os.path.join(path, href) + is_cover = item.href == cover_href + if can_reference_images or is_cover: + os.makedirs(path, exist_ok=True) + with open(ipath, 'wb') as imgf: + imgf.write(item.data) + if is_cover: + cover_relhref = os.path.relpath(ipath, tdir).replace(os.sep, '/') # Metadata with open(os.path.join(tdir, 'metadata.opf'), 'wb') as mdataf: