예제 #1
0
def upload(request):
    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        data = {"path": uploaded_file.file.url}
        return HttpResponse(simplejson.dumps(data))
    else:
        return HttpResponseBadRequest(simplejson.dumps({"errors": form.errors}))
예제 #2
0
def upload(request):
    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        data = {
            'path': uploaded_file.file.url,
        }
        return HttpResponse(json.dumps(data))
    else:
        return HttpResponseBadRequest(json.dumps({'errors': form.errors}))
예제 #3
0
def upload(request):
    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        #dfs = hdfs_storage()
        #dfs.create(uploaded_file.file.path, uploaded_file.file.path)
        data = {
            'path': uploaded_file.file.url,
        }
        return HttpResponse(simplejson.dumps(data))
    else:
        return HttpResponseBadRequest(simplejson.dumps({'errors': form.errors}))
예제 #4
0
def upload(request):
    if callable(permission_checker):
        if not permission_checker(request):
            return HttpResponseBadRequest(json.dumps({'errors': 'Upload not permitted'}))
    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        data = {
            'path': uploaded_file.file.url,
        }
        return HttpResponse(json.dumps(data))
    else:
        return HttpResponseBadRequest(json.dumps({'errors': form.errors}))
예제 #5
0
파일: views.py 프로젝트: fsw/django-cadocms
def upload(request):
    #print 'XXXX'
    #log.error("REQUEST: ")
    #log.error(request)
    #log.error("RESPONSE: ")

    content_type = "text/plain";
    if request.META.get('HTTP_ACCEPT'):
        if 'application/json' in request.META.get('HTTP_ACCEPT').split(","):
            content_type = "application/json";
    
    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        data = {
            'path': uploaded_file.file.url,
        }
        #print simplejson.dumps(data)
        #log.error(HttpResponse(json.dumps(data), content_type=content_type))
        return HttpResponse(json.dumps(data), content_type=content_type)
    else:
        #log.error(HttpResponseBadRequest(json.dumps({'errors': form.errors}), content_type=content_type))
        return HttpResponseBadRequest(json.dumps({'errors': form.errors}), content_type=content_type)
예제 #6
0
def upload(request):
    #print 'XXXX'
    #log.error("REQUEST: ")
    #log.error(request)
    #log.error("RESPONSE: ")

    content_type = "text/plain"
    if request.META.get('HTTP_ACCEPT'):
        if 'application/json' in request.META.get('HTTP_ACCEPT').split(","):
            content_type = "application/json"

    form = UploadedFileForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        uploaded_file = form.save()
        data = {
            'path': uploaded_file.file.url,
        }
        #print simplejson.dumps(data)
        #log.error(HttpResponse(json.dumps(data), content_type=content_type))
        return HttpResponse(json.dumps(data), content_type=content_type)
    else:
        #log.error(HttpResponseBadRequest(json.dumps({'errors': form.errors}), content_type=content_type))
        return HttpResponseBadRequest(json.dumps({'errors': form.errors}),
                                      content_type=content_type)
예제 #7
0
def upload(request):
    url = request.POST.get("url", None)
    if not url:
        form = UploadedFileForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            uploaded_file = form.save()
            file_url = uploaded_file.file.url
            try:
                UploadedFile.objects.get(file=uploaded_file.file)
            except UploadedFile.MultipleObjectsReturned:
                uploaded_file.delete()

            data = {
                'path': file_url,
            }
            return HttpResponse(json.dumps(data))
        else:
            return HttpResponseBadRequest(json.dumps({'errors': form.errors}))
    else:
        try:
            # We open the url of the distant file
            distant_file = urllib2.urlopen(url)

            # We check the length of the content (size of the file)
            content_length = int(distant_file.headers.getheader('content-length', settings.FILE_UPLOAD_MAX_MEMORY_SIZE + 1))

            # We get the maximum file upload size
            max_upload_size = getattr(settings, 'AJAX_UPLOAD_MAX_FILESIZE', upload_settings.DEFAULT_MAX_FILESIZE)

            # We check the length of the content
            if 0 < max_upload_size < content_length:
                return HttpResponseBadRequest(json.dumps({'errors': "File too big"}))

            # If it's too big, we store the file on the disk
            if content_length > settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
                handler = TemporaryFileUploadHandler()
            # Else, we put it in memory
            else:
                handler = MemoryFileUploadHandler()
                # Attribute activated needed because of the class implementation
                handler.activated = True

            # try/except needed because of the class implementation
            try:
                # Init the file upload handler
                handler.new_file("url", url.split('/')[-1].split('?')[0],
                    distant_file.headers.getheader('content-type'),
                    content_length
                )
            except StopFutureHandlers:
                pass

            def read_in_chunks(file_object, chunk_size=1024):
                """Lazy function to read a file piece by piece."""
                while True:
                    data = file_object.read(chunk_size)
                    if not data:
                        break
                    yield len(data), data

            # We pass all chunks to the file upload handler
            size = 0
            for read, data in read_in_chunks(distant_file, handler.chunk_size):
                handler.receive_data_chunk(data, None)
                size += read

            # We end the handler and save the file to the model
            uploaded_file = UploadedFile()
            uploaded_file.file.save(handler.file_name, handler.file_complete(size))
            uploaded_file.save()
            file_url = uploaded_file.file.url
            try:
                UploadedFile.objects.get(file=uploaded_file.file)
            except UploadedFile.MultipleObjectsReturned:
                uploaded_file.delete()

            data = {
                'path': file_url,
            }
            return HttpResponse(json.dumps(data))
        except Exception:
            return HttpResponseBadRequest(json.dumps({'errors': "An error occured"}))