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.
This commit is contained in:
Patrick Cloke 2020-09-09 12:59:41 -04:00 committed by GitHub
parent 2ea1c68249
commit b312769c0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 107 additions and 23 deletions

View file

@ -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: