def _decode(self, data): if is_text(data) and data.startswith('data:'): # base64 encoded file - decode format, datastr = data.split(';base64,') # format ~= data:image/X, ext = format.split('/')[-1] # guess file extension if ext[:3] == 'svg': ext = 'svg' data = ContentFile(base64.b64decode(datastr), name='{}.{}'.format(uuid.uuid4(), ext)) elif is_text(data) and data.startswith('http'): raise SkipField() return data
def from_native(self, data): if isinstance(data, basestring) and data.startswith('data:application/pdf'): pdfformat, pdfstr = data.split(';base64,') # format ~= data:image/X, ext = pdfformat.split('/')[-1] # guess file extension data = ContentFile(base64.b64decode(pdfstr), name=str(uuid.uuid4()) + "." + ext) if isinstance(data, basestring) and data.startswith('data:image'): # base64 encoded image - decode format, imgstr = data.split(';base64,') # format ~= data:image/X, ext = format.split('/')[-1] # guess file extension raw_data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext) img = Image.open(raw_data) output = StringIO.StringIO() #temporarily mess w/ image in memory t_dim = (1600,1600) i_dim = img.size compare_image_to_thumb = [(i_dim > t_dim) for i_dim, t_dim in zip(i_dim,t_dim)] if True in compare_image_to_thumb: t_dim = (1600,1600) #TODO: Remove stupid repeat variable img.thumbnail(t_dim, Image.ANTIALIAS) # try to get image exif and flip here on the server try: for orientation in ExifTags.TAGS.keys() : if ExifTags.TAGS[orientation] == 'Orientation' : break imgExif = img._getexif() if imgExif is not None : exif = dict(imgExif.items()) if exif[orientation] == 3 : img = img.rotate(180, expand=True) elif exif[orientation] == 6 : img = img.rotate(-90, expand=True) elif exif[orientation] == 8 : img = img.rotate(90, expand=True) except: traceback.print_exc() img.save(output,'png') output.seek(0) converted_data = output.read() data = ContentFile(converted_data, name=str(uuid.uuid4()) + "." + ext) return super(Base64FileField, self).from_native(data)
def _decode(self, data): if isinstance(data, str) and data.startswith("data:"): # base64 encoded file - decode # format ~= data:image/X, format, datastr = data.split(";base64,") ext = format.split("/")[-1] # guess file extension if ext[:3] == "svg": ext = "svg" data = ContentFile( base64.b64decode(datastr), name="{}.{}".format(uuid.uuid4(), ext) ) elif isinstance(data, str) and data.startswith("http"): raise SkipField() return data
def _decode(self, data): if isinstance(data, text_type) and data.startswith('data:'): # base64 encoded file - decode format, datastr = data.split(';base64,') # format ~= data:image/X, ext = format.split('/')[-1] # guess file extension if ext[:3] == 'svg': ext = 'svg' data = ContentFile(base64.b64decode(datastr), name='{}.{}'.format(uuid.uuid4(), ext)) logger.debug("Data as string?") logger.debug(data[0:10]) elif isinstance(data, text_type) and data.startswith('http'): logger.debug("FAIL") raise SkipField() elif isinstance(data, text_type): logger.debug('WTF - last resort') data = ContentFile(base64.b64decode(data), name='{}.{}'.format(uuid.uuid4(), 'm4a')) return data