From f386039d5a9a26d65a67deb3e5babf9cd2f8f17e Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 21 Jul 2020 16:01:17 -0500 Subject: [PATCH] Added validation method for uploaded file using imghdr library --- InvenTree/part/serializers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index a77b0f1c3f..d460748468 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -1,6 +1,7 @@ """ JSON serializers for Part app """ +import imghdr from rest_framework import serializers @@ -95,6 +96,15 @@ class PartThumbSerializer(serializers.Serializer): class PartThumbSerializerUpdate(InvenTreeModelSerializer): """ Serializer for updating Part thumbnail """ + def validate_image(self, value): + """ + Check that file is an image. + """ + validate = imghdr.what(value) + if not validate: + raise serializers.ValidationError("File is not an image") + return value + image = InvenTreeAttachmentSerializerField(required=True) class Meta: