How to prevent upload of "Bad images" in Django? (PILLOW, easy-thumbnails)

231 Views Asked by At

Intro

I have been using Django with easy-thumnails for quite a while, and today I stumbled upon one really nasty bug, but because I have always permitted easy-thumbnails to do as it pleases I consider myself a noob at this.

TL;DR

I need to validate if an image can be read by easy-thumbnails or Pillow before it is saved to the django model in an optimal way.

Explanation

When I tried to convert an .svg image, apparently Pillow crashes on the spot. I don't know if it is because of the format, even though according to some other stack issues, by installing libz or zlib1g, there should not be a problem (they are already installed in my system btw).

Crash Error

But the format doesn't matter, if I insert a corrupt file as an image it could make the library crash every time.

I need to be able to validate (inside my django-forms or my drf-serializers) if an image can be read by pillow before saving it and prevent this to ever happen again.

Any ideas to validate the file in an optimal way would be greatly appreciated.

0

There are 0 best solutions below