def download_attach(request, id):
    # Read file from database
    disk = get_object_or_404(site_models.EncryptedDisk, id=id)
    """
    missing_padding = len(data) % 4
    if missing_padding != 0:
        data += b'=' * (4 - missing_padding)
    decoded_data = str(base64.b64decode(data).decode("utf8"))
    """
    try:
        file_data = disk.file_data.decode()
    except AttributeError:
        file_data = disk.file_data

    inMemFile = StringIO(file_data)
    inMemFile.name = disk.file_name
    inMemFile.mode = 'rb'

    file_content = File(inMemFile)
    content_type, content_encoding = mimetypes.guess_type(disk.file_name)
    content_type = 'application/force-download'
    response = HttpResponse(content=file_content, content_type=content_type)
    response[
        'Content-Disposition'] = 'attachment; filename=%s' % disk.file_name
    if content_encoding:
        response['Content-Encoding'] = content_encoding
    return response
Example #2
0
 def _open(self, name, mode='rb'):
     headers, content = self.connection.get_object(self.container_name,
                                                   name)
     buf = StringIO(content)
     buf.name = os.path.basename(name)
     buf.mode = mode
     return File(buf)
Example #3
0
 def _open(self, name, mode='rb'):
     headers, content = swiftclient.get_object(self.storage_url, self.token,
                                               self.container_name, name,
                                               http_conn=self.http_conn)
     buf = StringIO(content)
     buf.name = os.path.basename(name)
     buf.mode = mode
     return File(buf)
Example #4
0
 def _open(self, name, mode='rb'):
     headers, content = swiftclient.get_object(self.storage_url,
                                               self.token,
                                               self.container_name,
                                               name,
                                               http_conn=self.http_conn)
     buf = StringIO(content)
     buf.name = os.path.basename(name)
     buf.mode = mode
     return File(buf)
Example #5
0
    def _open(self, name, mode='rb'):
        assert mode == 'rb', "DatabaseStorage open mode must be 'rb'."

        try:
            object = Image.objects.get(pk=name)
        except Image.DoesNotExist:
            return None

        inMemFile = StringIO(b64decode(object.data))
        inMemFile.name = object.filename
        inMemFile.mode = mode

        return ImageFile(inMemFile)