From d9f2449ddc3a909e43250d069c295a47a50116ff Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 May 2020 19:13:24 +0530 Subject: [PATCH] Only return a dummy PNG to Qt if the resource being loaded has a raster image extension --- src/calibre/gui2/comments_editor.py | 15 ++++++++------- src/calibre/gui2/widgets2.py | 16 +++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 421a9a207e..7d07fd92ae 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -28,7 +28,7 @@ from calibre.gui2.widgets import LineEditECM from calibre.gui2.widgets2 import to_plain_text from calibre.utils.config import tweaks from calibre.utils.imghdr import what -from polyglot.builtins import filter, iteritems, itervalues, unicode_type, as_bytes +from polyglot.builtins import filter, iteritems, itervalues, unicode_type # Cleanup Qt markup {{{ @@ -764,12 +764,13 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ with lopen(path, 'rb') as f: data = f.read() except EnvironmentError: - return QByteArray(bytearray.fromhex( - '89504e470d0a1a0a0000000d49484452' - '000000010000000108060000001f15c4' - '890000000a49444154789c6300010000' - '0500010d0a2db40000000049454e44ae' - '426082')) + if path.rpartition('.')[-1].lower() in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}: + return QByteArray(bytearray.fromhex( + '89504e470d0a1a0a0000000d49484452' + '000000010000000108060000001f15c4' + '890000000a49444154789c6300010000' + '0500010d0a2db40000000049454e44ae' + '426082')) else: return QByteArray(data) diff --git a/src/calibre/gui2/widgets2.py b/src/calibre/gui2/widgets2.py index 9a02f188c4..4195822d13 100644 --- a/src/calibre/gui2/widgets2.py +++ b/src/calibre/gui2/widgets2.py @@ -489,16 +489,18 @@ class HTMLDisplay(QTextBrowser): with lopen(path, 'rb') as f: data = f.read() except EnvironmentError: - return QByteArray(bytearray.fromhex( - '89504e470d0a1a0a0000000d49484452' - '000000010000000108060000001f15c4' - '890000000a49444154789c6300010000' - '0500010d0a2db40000000049454e44ae' - '426082')) + if path.rpartition('.')[-1].lower() in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}: + return QByteArray(bytearray.fromhex( + '89504e470d0a1a0a0000000d49484452' + '000000010000000108060000001f15c4' + '890000000a49444154789c6300010000' + '0500010d0a2db40000000049454e44ae' + '426082')) else: return QByteArray(data) else: - QTextBrowser.loadResource(self, rtype, qurl) + return QTextBrowser.loadResource(self, rtype, qurl) + class ScrollingTabWidget(QTabWidget):