Beispiel #1
0
 def get(self, request, **kwargs):
     file_name = self.request.GET.get('file')
     if request.is_ajax():
         #an uploadedBytes variable is required client-side for resuming uploads
         data = {'size': utils._get_uploaded_bytes(file_name)}
         return HttpResponse(simplejson.dumps(data),
                             mimetype='application/json')
     return super(UploadMedia, self).get(request, **kwargs)
Beispiel #2
0
 def get(self, request, **kwargs):
     file_name = self.request.GET.get('file')
     if request.is_ajax():
         #an uploadedBytes variable is required client-side for resuming uploads
         data = {
             'size': utils._get_uploaded_bytes(file_name)
         }
         return HttpResponse(simplejson.dumps(data), mimetype='application/json')
     return super(UploadMedia, self).get(request, **kwargs)
Beispiel #3
0
    def post(self, request, **kwargs):
        blob_file = request.FILES.get(u'files', None)
        blob_size = blob_file._get_size()
        blob_name = blob_file.name

        #If files are chunked, the original file metadata is stored in extra META headers and the
        #POSTed file is called 'blob'. For files that aren't chunked, use the POSTed file directly.
        expected_file_size = utils.get_filesize_from_request_meta(
            request.META) or blob_size
        expected_file_name = utils.get_filename_from_request_meta(
            request.META) or blob_name

        logging.debug(
            "Blob got: %s (expected: %s), size got: %s (expected: %s)" %
            (blob_name, expected_file_name, blob_size, expected_file_size))

        chunked_file, blobstore_filename, file_finalized = utils.handle_blob_upload(
            blob_file, expected_file_name, expected_file_size)

        if not file_finalized:
            # The file isn't finalized yet so keep adding to it. Dump the uploaded bytes
            # to the client and let it handle resume / retries.
            data = {'size': utils._get_uploaded_bytes(expected_file_name)}
            return HttpResponse(simplejson.dumps(data),
                                mimetype='application/json')

        try:
            mymorsel = MyMorsel(file_name=expected_file_name)
            mymorsel.file_object.save(expected_file_name, chunked_file)
            mymorsel.save()
            return redirect(
                reverse('uploaded_mymorsel', kwargs={'pk': mymorsel.id}))

        except Exception, e:
            logging.exception('UploadHandler failed: %s %s' %
                              (Exception, str(e)))
            #Log the error then dump an error message to the client
            response = [{
                "name": expected_file_name,
                "error": "ERROR SAVING FILE"
            }]
            return HttpResponse(simplejson.dumps(response),
                                mimetype='application/json')
Beispiel #4
0
    def post(self, request, **kwargs):
        blob_file = request.FILES.get(u'files', None)
        blob_size = blob_file._get_size()
        blob_name = blob_file.name

        #If files are chunked, the original file metadata is stored in extra META headers and the
        #POSTed file is called 'blob'. For files that aren't chunked, use the POSTed file directly.
        expected_file_size = utils.get_filesize_from_request_meta(request.META) or blob_size
        expected_file_name = utils.get_filename_from_request_meta(request.META) or blob_name

        logging.debug("Blob got: %s (expected: %s), size got: %s (expected: %s)" % (
            blob_name, expected_file_name, blob_size, expected_file_size))

        chunked_file, blobstore_filename, file_finalized = utils.handle_blob_upload(
            blob_file,
            expected_file_name,
            expected_file_size
        )

        if not file_finalized:
            # The file isn't finalized yet so keep adding to it. Dump the uploaded bytes
            # to the client and let it handle resume / retries.
            data = {
                'size': utils._get_uploaded_bytes(expected_file_name)
            }
            return HttpResponse(simplejson.dumps(data), mimetype='application/json')

        try:
            mymorsel = MyMorsel(file_name=expected_file_name)
            mymorsel.file_object.save(expected_file_name, chunked_file)
            mymorsel.save()
            return redirect(reverse('uploaded_mymorsel', kwargs={'pk': mymorsel.id}))

        except Exception, e:
            logging.exception('UploadHandler failed: %s %s' % (Exception, str(e)))
            #Log the error then dump an error message to the client
            response = [{
                "name": expected_file_name,
                "error": "ERROR SAVING FILE"
            }]
            return HttpResponse(simplejson.dumps(response), mimetype='application/json')