From 83b99fcb1a6fc60a81023fec835e22883c2a903e Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Tue, 12 Apr 2022 19:33:00 +0200 Subject: [PATCH] Fix cover upload url with spaces at the end Support image/jpg as upload format mimetype, remove redundant check of mimetype --- cps/editbooks.py | 2 +- cps/helper.py | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index c07e5d24..ab2bd55e 100755 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -810,7 +810,7 @@ def edit_book(book_id): if to_save["cover_url"].endswith('/static/generic_cover.jpg'): book.has_cover = 0 else: - result, error = helper.save_cover_from_url(to_save["cover_url"], book.path) + result, error = helper.save_cover_from_url(to_save["cover_url"].strip(), book.path) if result is True: book.has_cover = 1 modify_date = True diff --git a/cps/helper.py b/cps/helper.py index 5d5cc021..69b3e2f8 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -753,6 +753,9 @@ def save_cover_from_url(url, book_path): log.error("python modul advocate is not installed but is needed") return False, _("Python modul 'advocate' is not installed but is needed for cover downloads") img.raise_for_status() + # # cover_processing() + # move_coverfile(meta, db_book) + return save_cover(img, book_path) except (socket.gaierror, requests.exceptions.HTTPError, @@ -802,24 +805,23 @@ def save_cover(img, book_path): content_type = img.headers.get('content-type') if use_IM: - if content_type not in ('image/jpeg', 'image/png', 'image/webp', 'image/bmp'): + if content_type not in ('image/jpeg', 'image/jpg', 'image/png', 'image/webp', 'image/bmp'): log.error("Only jpg/jpeg/png/webp/bmp files are supported as coverfile") return False, _("Only jpg/jpeg/png/webp/bmp files are supported as coverfile") # convert to jpg because calibre only supports jpg - if content_type != 'image/jpg': - try: - if hasattr(img, 'stream'): - imgc = Image(blob=img.stream) - else: - imgc = Image(blob=io.BytesIO(img.content)) - imgc.format = 'jpeg' - imgc.transform_colorspace("rgb") - img = imgc - except (BlobError, MissingDelegateError): - log.error("Invalid cover file content") - return False, _("Invalid cover file content") + try: + if hasattr(img, 'stream'): + imgc = Image(blob=img.stream) + else: + imgc = Image(blob=io.BytesIO(img.content)) + imgc.format = 'jpeg' + imgc.transform_colorspace("rgb") + img = imgc + except (BlobError, MissingDelegateError): + log.error("Invalid cover file content") + return False, _("Invalid cover file content") else: - if content_type not in 'image/jpeg': + if content_type not in ['image/jpeg', 'image/jpg']: log.error("Only jpg/jpeg files are supported as coverfile") return False, _("Only jpg/jpeg files are supported as coverfile")