Added validation method for uploaded file using imghdr library

This commit is contained in:
eeintech 2020-07-21 16:01:17 -05:00
parent af70dd5dfe
commit f386039d5a

View File

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