From fcefd8031ae1a16aafa16b8cae00ad8af34b767e Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sat, 2 May 2020 11:24:30 +0200 Subject: [PATCH] Fix #985 (png and webp are extracted as cover files from comic files) --- cps/comic.py | 47 ++++++++++++++++++++++---------- cps/config_sql.py | 2 +- cps/static/js/archive/archive.js | 13 +++++++++ cps/uploader.py | 2 -- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/cps/comic.py b/cps/comic.py index 2ee182c4..a96b732d 100644 --- a/cps/comic.py +++ b/cps/comic.py @@ -18,10 +18,17 @@ from __future__ import division, print_function, unicode_literals import os +import io from . import logger, isoLanguages from .constants import BookMeta +try: + from PIL import Image as PILImage + use_PIL = True +except ImportError as e: + use_PIL = False + log = logger.create() @@ -41,6 +48,27 @@ except ImportError as e: use_rarfile = False use_comic_meta = False +def _cover_processing(tmp_file_name, img, extension): + if use_PIL: + # convert to jpg because calibre only supports jpg + if extension in ('.png', '.webp'): + imgc = PILImage.open(io.BytesIO(img)) + im = imgc.convert('RGB') + tmp_bytesio = io.BytesIO() + im.save(tmp_bytesio, format='JPEG') + img = tmp_bytesio.getvalue() + + prefix = os.path.dirname(tmp_file_name) + if img: + tmp_cover_name = prefix + '/cover.jpg' + image = open(tmp_cover_name, 'wb') + image.write(img) + image.close() + else: + tmp_cover_name = None + return tmp_cover_name + + def _extractCover(tmp_file_name, original_file_extension, rarExceutable): cover_data = extension = None @@ -50,7 +78,7 @@ def _extractCover(tmp_file_name, original_file_extension, rarExceutable): ext = os.path.splitext(name) if len(ext) > 1: extension = ext[1].lower() - if extension == '.jpg' or extension == '.jpeg': + if extension in ('.jpg', '.jpeg', '.png', '.webp'): cover_data = archive.getPage(index) break else: @@ -60,7 +88,7 @@ def _extractCover(tmp_file_name, original_file_extension, rarExceutable): ext = os.path.splitext(name) if len(ext) > 1: extension = ext[1].lower() - if extension == '.jpg' or extension == '.jpeg': + if extension in ('.jpg', '.jpeg', '.png', '.webp'): cover_data = cf.read(name) break elif original_file_extension.upper() == '.CBT': @@ -69,7 +97,7 @@ def _extractCover(tmp_file_name, original_file_extension, rarExceutable): ext = os.path.splitext(name) if len(ext) > 1: extension = ext[1].lower() - if extension == '.jpg' or extension == '.jpeg': + if extension in ('.jpg', '.jpeg', '.png', '.webp'): cover_data = cf.extractfile(name).read() break elif original_file_extension.upper() == '.CBR' and use_rarfile: @@ -80,21 +108,12 @@ def _extractCover(tmp_file_name, original_file_extension, rarExceutable): ext = os.path.splitext(name) if len(ext) > 1: extension = ext[1].lower() - if extension == '.jpg' or extension == '.jpeg': + if extension in ('.jpg', '.jpeg', '.png', '.webp'): cover_data = cf.read(name) break except Exception as e: log.debug('Rarfile failed with error: %s', e) - - prefix = os.path.dirname(tmp_file_name) - if cover_data: - tmp_cover_name = prefix + '/cover' + extension - image = open(tmp_cover_name, 'wb') - image.write(cover_data) - image.close() - else: - tmp_cover_name = None - return tmp_cover_name + return _cover_processing(tmp_file_name, cover_data, extension) def get_comic_info(tmp_file_path, original_file_name, original_file_extension, rarExceutable): diff --git a/cps/config_sql.py b/cps/config_sql.py index 22f57131..d17583d8 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -112,7 +112,7 @@ class _Settings(_Base): config_converterpath = Column(String) config_calibre = Column(String) config_rarfile_location = Column(String) - config_upload_formats = Column(String, default=constants.EXTENSIONS_UPLOAD) + config_upload_formats = Column(String, default=','.join(constants.EXTENSIONS_UPLOAD)) config_updatechannel = Column(Integer, default=constants.UPDATE_STABLE) diff --git a/cps/static/js/archive/archive.js b/cps/static/js/archive/archive.js index cb76321f..06c05624 100644 --- a/cps/static/js/archive/archive.js +++ b/cps/static/js/archive/archive.js @@ -411,6 +411,19 @@ bitjs.archive = bitjs.archive || {}; return "unrar.js"; }; + /** + * Unrarrer5 + * @extends {bitjs.archive.Unarchiver} + * @constructor + */ + bitjs.archive.Unrarrer5 = function(arrayBuffer, optPathToBitJS) { + bitjs.base(this, arrayBuffer, optPathToBitJS); + }; + bitjs.inherits(bitjs.archive.Unrarrer5, bitjs.archive.Unarchiver); + bitjs.archive.Unrarrer5.prototype.getScriptFileName = function() { + return "unrar5.js"; + }; + /** * Untarrer * @extends {bitjs.archive.Unarchiver} diff --git a/cps/uploader.py b/cps/uploader.py index b23177c8..880df20e 100644 --- a/cps/uploader.py +++ b/cps/uploader.py @@ -73,8 +73,6 @@ except ImportError as e: log.debug('cannot import Pillow, using png and webp images as cover will not work: %s', e) use_PIL = False -__author__ = 'lemmsh' - def process(tmp_file_path, original_file_name, original_file_extension, rarExcecutable): meta = None