Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Base64EncodedImage field serializer including to_representation() method
import six
import uuid
import base64
from django.conf import settings
from django.core.files import File
from django.core.files.base import ContentFile
from rest_framework import serializers
class Base64ImageField(serializers.ImageField):
"""
A Django REST framework field for handling image-uploads through raw post data.
It uses base64 for encoding and decoding the contents of the file.
Heavily based on https://github.com/tomchristie/django-rest-framework/pull/1268
Updated for Django REST Framework 3.
"""
def to_representation(self, data):
file_name = '{}/{}'.format(settings.MEDIA_ROOT, data.name)
with open(file_name, "rb", buffering=0) as file:
encoded_file = base64.b64encode(file.read()).decode("utf-8")
file = {
'encoded_file': encoded_file,
'extension': 'jpg'
}
data = 'data:image/{extension};charset=utf-8;base64, {encoded_file}'.format(**file)
return data
def to_internal_value(self, data):
# Check that the incoming data value is a base64 encoded string:
if isinstance(data, six.string_types):
# Check if the base64 string is in the prefixed "data:" format
if 'data:' in data and ';base64,' in data:
# Break out the header from the base64 content:
header, data = data.split(';charset=utf-8;base64,')
# Try to decode the file. Return validation error if it fails:
try:
decoded_file = base64.b64decode(data)
except TypeError:
self.fail('invalid_image')
name = str(uuid.uuid4())
file = {
'name': name,
'extension': self.get_file_extension(name, decoded_file)
}
complete_file_name = '{name}.{extension}'.format(**file)
data = ContentFile(decoded_file, name=complete_file_name)
return super(Base64ImageField, self).to_internal_value(data)
def get_file_extension(self, filename, decoded_file):
import imghdr
extension = imghdr.what(filename, decoded_file)
extension = "jpg" if extension == "jpeg" else extension
return extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment