diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2020-09-09 12:59:41 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-09 12:59:41 -0400 |
commit | b312769c0ee2c40b1a26a6ed39ea1c8a462d4349 (patch) | |
tree | a95ead1bc09492d00fa7ce748d81de5cca4cb4b1 /synapse/rest/media/v1/thumbnailer.py | |
parent | Remove some unused distributor signals (#8216) (diff) | |
download | synapse-b312769c0ee2c40b1a26a6ed39ea1c8a462d4349.tar.xz |
Do not error when thumbnailing invalid files (#8236)
If a file cannot be thumbnailed for some reason (e.g. the file is empty), then catch the exception and convert it to a reasonable error message for the client.
Diffstat (limited to 'synapse/rest/media/v1/thumbnailer.py')
-rw-r--r-- | synapse/rest/media/v1/thumbnailer.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/synapse/rest/media/v1/thumbnailer.py b/synapse/rest/media/v1/thumbnailer.py index d681bf7bf0..457ad6031c 100644 --- a/synapse/rest/media/v1/thumbnailer.py +++ b/synapse/rest/media/v1/thumbnailer.py @@ -15,7 +15,7 @@ import logging from io import BytesIO -from PIL import Image as Image +from PIL import Image logger = logging.getLogger(__name__) @@ -31,12 +31,22 @@ EXIF_TRANSPOSE_MAPPINGS = { } +class ThumbnailError(Exception): + """An error occurred generating a thumbnail.""" + + class Thumbnailer: FORMATS = {"image/jpeg": "JPEG", "image/png": "PNG"} def __init__(self, input_path): - self.image = Image.open(input_path) + try: + self.image = Image.open(input_path) + except OSError as e: + # If an error occurs opening the image, a thumbnail won't be able to + # be generated. + raise ThumbnailError from e + self.width, self.height = self.image.size self.transpose_method = None try: |