Catch Imagemagick policy error

This commit is contained in:
Ozzieisaacs 2019-02-09 12:58:43 +01:00
parent 6fc4b318e8
commit d58d792f56

View File

@ -35,6 +35,7 @@ logger = logging.getLogger("book_formats")
try: try:
from wand.image import Image from wand.image import Image
from wand import version as ImageVersion from wand import version as ImageVersion
from wand.exceptions import PolicyError
use_generic_pdf_cover = False use_generic_pdf_cover = False
except (ImportError, RuntimeError) as e: except (ImportError, RuntimeError) as e:
logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work: %s', e) logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work: %s', e)
@ -130,12 +131,18 @@ def pdf_preview(tmp_file_path, tmp_dir):
if use_generic_pdf_cover: if use_generic_pdf_cover:
return None return None
else: else:
cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg" try:
with Image(filename=tmp_file_path + "[0]", resolution=150) as img: cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
img.compression_quality = 88 with Image(filename=tmp_file_path + "[0]", resolution=150) as img:
img.save(filename=os.path.join(tmp_dir, cover_file_name)) img.compression_quality = 88
return cover_file_name img.save(filename=os.path.join(tmp_dir, cover_file_name))
return cover_file_name
except PolicyError as ex:
logger.warning('Pdf extraction forbidden by Imagemagick policy: %s', ex)
return None
except Exception as ex:
logger.warning('Cannot extract cover image, using default: %s', ex)
return None
def get_versions(): def get_versions():
if not use_generic_pdf_cover: if not use_generic_pdf_cover: