Skip to content

Instantly share code, notes, and snippets.

@klipstein
Created November 22, 2010 12:25
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save klipstein/709890 to your computer and use it in GitHub Desktop.
Save klipstein/709890 to your computer and use it in GitHub Desktop.
Base64 file handling for django-tastypie
import base64
import os
from tastypie.fields import FileField
from django.core.files.uploadedfile import SimpleUploadedFile
class Base64FileField(FileField):
"""
A django-tastypie field for handling file-uploads through raw post data.
It uses base64 for en-/decoding the contents of the file.
Usage:
class MyResource(ModelResource):
file_field = Base64FileField("file_field")
class Meta:
queryset = ModelWithFileField.objects.all()
In the case of multipart for submission, it would also pass the filename.
By using a raw post data stream, we have to pass the filename within our
file_field structure:
file_field = {
"name": "myfile.png",
"file": "longbas64encodedstring",
"content_type": "image/png" # on hydrate optional
}
"""
def dehydrate(self, bundle):
if not bundle.data.has_key(self.instance_name) and hasattr(bundle.obj, self.instance_name):
file_field = getattr(bundle.obj, self.instance_name)
if file_field:
try:
content_type, encoding = mimetypes.guess_type(file_field.file.name)
b64 = open(file_field.file.name, "rb").read().encode("base64")
ret = {
"name": os.path.basename(file_field.file.name),
"file": b64,
"content-type": content_type or "application/octet-stream"
}
return ret
except:
pass
return None
def hydrate(self, obj):
value = super(FileField, self).hydrate(obj)
if value:
value = SimpleUploadedFile(value["name"], base64.b64decode(value["file"]), getattr(value, "content_type", "application/octet-stream"))
return value
@dcale
Copy link

dcale commented Jun 10, 2013

Hey klipstein, thank you for this! In the current tastypie version (django-tastypie==0.9.15) you have to make a little change for this to work, otherwise you'll get an "dehydrate() got an unexpected keyword argument 'for_list'" exception:

change:
def dehydrate(self, bundle):

to:
def dehydrate(self, bundle, for_list):

@yuvadm
Copy link

yuvadm commented Feb 3, 2014

Also missing:

import mimetypes

@th0th
Copy link

th0th commented Sep 28, 2014

Any hints on checking resolution of the incoming file when using this for an image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment