I have a FileField
Serializer for uploaded base64 audio, I noticed that the Base64 string does not start with data:****. How do I determine the Mime Type of the uploaded file? i_need_the_file_extension_mimetype()
class AudioField(serializers.FileField):
def to_internal_value(self, data):
if isinstance(data, basestring):
data = re.sub(r"^data\:.+base64\,(.+)$", r"\1", data)
# Try to base64 decode the data url.
try:
decoded = base64.b64decode(data)
except TypeError:
raise serializers.ValidationError(_('Not a valid file'))
file_name, file_ext, mime_type = self.i_need_the_file_extension_mimetype(decoded)
data = ContentFile(decoded, name=file_name)
return super(AudioField, self).to_internal_value(data)