示例#1
0
 def upload(self, request, pk):
     files = request.data['files']
     incident = get_object_or_404(Incident, pk=pk)
     files_added = []
     for i, file in enumerate(files):
         try:
             file_obj = base64.b64decode(file['content'])
             file_obj = FileWrapper(BytesIO(file_obj))
         except Exception as e:
             print(str(e))
             file_obj = FileWrapper(StringIO(file['content']))
         file_obj.name = file['filename']
         description = file['description']
         f = handle_uploaded_file(file_obj, description, incident)
         files_added.append(f)
     resp_data = FileSerializer(files_added, many=True, context={'request': request}).data
     return HttpResponse(JSONRenderer().render(resp_data), content_type='application/json')
示例#2
0
def download(request, img_name):
    f = get_object_or_404(Img, name=img_name)
    img_path = settings.MEDIA_ROOT + '/images' + img_name
    img_wrapper = FileWrapper(f.img)
    content_type = mimetypes.guess_type(f.img)
    response = HttpResponse(img_wrapper, content_type=content_type)
    response['X-Sendfile'] = img_path
    response['Content-Disposition'] = 'attachment; filename=%s' % f.name
    response['Content-Length'] = os.stat(img_path).st_size

    return response
示例#3
0
def do_download(request, file_id):
    f = get_object_or_404(File, pk=file_id)

    wrapper = FileWrapper(f.file)
    content_type = mimetypes.guess_type(f.file.name)
    response = HttpResponse(wrapper, content_type=content_type)
    response['Content-Disposition'] = 'attachment; filename=%s' % (
        f.getfilename())
    response['Content-Length'] = os.path.getsize(str(f.file.file))

    return response
示例#4
0
def do_download(request, file_id):
    f = get_object_or_404(File, pk=file_id)
    if not request.user.has_perm('findings.view_findings',
                                 obj=f.get_related()):
        raise PermissionDenied()
    wrapper = FileWrapper(f.file)
    content_type = mimetypes.guess_type(f.file.name)
    response = HttpResponse(wrapper, content_type=content_type)
    response['Content-Disposition'] = 'attachment; filename=%s' % (
        f.getfilename())
    response['Content-Length'] = os.path.getsize(str(f.file.file))

    return response
示例#5
0
def store_temp_file(fd):
    """
    Given a file-like object and dumps it to the temporary directory.

    Returns the temporary file object.
    """
    temp_file = tempfile.NamedTemporaryFile(
        encoding=getattr(fd, 'encoding', None))

    source = FileWrapper(fd)
    for chunk in source.chunks():
        temp_file.write(chunk)

    temp_file.seek(0)

    return temp_file
示例#6
0
文件: views.py 项目: bkoch53/FIR
 def upload(self, request, pk):
     files = request.data['files']
     finding = get_object_or_404(Finding, pk=pk)
     files_added = []
     for i, file in enumerate(files):
         file_obj = FileWrapper(StringIO.StringIO(file['content']))
         file_obj.name = file['filename']
         description = file['description']
         f = handle_uploaded_file(file_obj, description, finding)
         files_added.append(f)
     resp_data = FileSerializer(files_added,
                                many=True,
                                context={
                                    'request': request
                                }).data
     return HttpResponse(JSONRenderer().render(resp_data),
                         content_type='application/json')
示例#7
0
def do_download_archive(request, content_type, object_id):
    object_type = ContentType.objects.get(pk=content_type)
    obj = get_object_or_404(object_type.model_class(), pk=object_id)
    if obj.file_set.count() == 0:
        raise Http404
    temp = BytesIO()
    with zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) as archive:
        media_root = settings.MEDIA_ROOT
        for file in obj.file_set.all():
            path = os.path.join(media_root, file.file.path)
            archive.write(path, os.path.basename(path))
    file_size = temp.tell()
    temp.seek(0)
    wrapper = FileWrapper(temp)

    response = HttpResponse(wrapper, content_type='application/zip')
    response[
        'Content-Disposition'] = 'attachment; filename=archive_%s_%s.zip' % (
            object_type.model, object_id)
    response['Content-Length'] = file_size
    return response