Handling of invalid cover files on upload

This commit is contained in:
Ozzie Isaacs 2021-11-20 13:45:41 +01:00
parent 7ad419dc8c
commit 5ede079401
1 changed files with 12 additions and 8 deletions

View File

@ -63,7 +63,7 @@ log = logger.create()
try: try:
from wand.image import Image from wand.image import Image
from wand.exceptions import MissingDelegateError from wand.exceptions import MissingDelegateError, BlobError
use_IM = True use_IM = True
except (ImportError, RuntimeError) as e: except (ImportError, RuntimeError) as e:
log.debug('Cannot import Image, generating covers from non jpg files will not work: %s', e) log.debug('Cannot import Image, generating covers from non jpg files will not work: %s', e)
@ -638,13 +638,17 @@ def save_cover(img, book_path):
return False, _("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 # convert to jpg because calibre only supports jpg
if content_type != 'image/jpg': if content_type != 'image/jpg':
if hasattr(img, 'stream'): try:
imgc = Image(blob=img.stream) if hasattr(img, 'stream'):
else: imgc = Image(blob=img.stream)
imgc = Image(blob=io.BytesIO(img.content)) else:
imgc.format = 'jpeg' imgc = Image(blob=io.BytesIO(img.content))
imgc.transform_colorspace("rgb") imgc.format = 'jpeg'
img = imgc imgc.transform_colorspace("rgb")
img = imgc
except BlobError:
log.error("Invalid cover file content")
return False, _("Invalid cover file content")
else: else:
if content_type not in 'image/jpeg': if content_type not in 'image/jpeg':
log.error("Only jpg/jpeg files are supported as coverfile") log.error("Only jpg/jpeg files are supported as coverfile")