User file upload: gracefully handle non-images
In some cases user upload may look like an image, but it isn't. The code handling thumbnail generation will fail and abort the upload. This can be handled gracefully allowing upload without thumb generation:
OverflowError: Python int too large to convert to C ssize_t
File "django/core/handlers/exception.py", line 56, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/views/decorators/csrf.py", line 55, in wrapped_view
return view_func(*args, **kwargs)
File "django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "contextlib.py", line 81, in inner
return func(*args, **kwds)
File "baserow/api/decorators.py", line 105, in func_wrapper
return func(*args, **kwargs)
File "baserow/api/user_files/views.py", line 61, in post
user_file = UserFileHandler().upload_user_file(request.user, file.name, file)
File "baserow/core/user_files/handler.py", line 233, in upload_user_file
image = Image.open(stream)
File "PIL/Image.py", line 3277, in open
im = _open_core(fp, filename, prefix, formats)
File "PIL/Image.py", line 3263, in _open_core
im = factory(fp, filename)
File "PIL/ImageFile.py", line 117, in __init__
self._open()
File "PIL/PsdImagePlugin.py", line 127, in _open
self.layers = _layerinfo(_layer_data, size)
File "PIL/PsdImagePlugin.py", line 241, in _layerinfo
t = _maketile(fp, m, bbox, 1)
File "PIL/PsdImagePlugin.py", line 287, in _maketile
file.seek(offset)
KeyError: 'PSD'
File "django/core/handlers/exception.py", line 56, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/views/decorators/csrf.py", line 55, in wrapped_view
return view_func(*args, **kwargs)
File "django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "contextlib.py", line 81, in inner
return func(*args, **kwds)
File "baserow/api/decorators.py", line 105, in func_wrapper
return func(*args, **kwargs)
File "baserow/api/user_files/views.py", line 61, in post
user_file = UserFileHandler().upload_user_file(request.user, file.name, file)
File "baserow/core/user_files/handler.py", line 258, in upload_user_file
self.generate_and_save_image_thumbnails(image, user_file, storage=storage)
File "baserow/core/user_files/handler.py", line 168, in generate_and_save_image_thumbnails
thumbnail.save(thumbnail_stream, image.format)
File "PIL/Image.py", line 2419, in save
save_handler = SAVE[format.upper()]
Also, reported by user: https://community.baserow.io/t/cant-upload-psd-file-to-baserow-is-it-a-bug-or-a-design-limitation/5477
Edited by Cezary Statkiewicz