Esempio n. 1
0
def ephemeral_keepalive(request, filename):
    oauth_request = oauth_server.request_from_django(request)
    verified = oauth_server.verify_access_request(oauth_request) if oauth_request else False
    if not verified:
        return HttpResponseBadRequest()
    
    # make sure file being updated matches the username in the oauth request 
    filename = '/' + filename
    username = request.GET.get('username')
    if not filename.startswith('/' + username):
        return HttpResponseBadRequest()
    
    ttl_time = int(request.GET.get('ttl', INITIAL_TTL))
    if ttl_time < MIN_TTL or ttl_time > MAX_TTL:
        return HttpResponseBadRequest()
    
    update_ttl(filename, ttl_time)
    
    return HttpResponse("")
Esempio n. 2
0
def api_upload(request):
    result = {}
    if request.method != 'POST':
        result['success'] = False
        result['error'] = 'Invalid request'
    else:
        oauth_request = oauth_server.request_from_django(request)
        verified = oauth_server.verify_access_request(oauth_request) if oauth_request else False
        if not verified:
            result['success'] = False
            result['error'] = 'OAuth Authentication Error'
        else:
            params = ['path', 'main_filename', 'title',
                      'description', 'subfiles', 'ephemeral',
                      'ttl_time', 'labels']
            
            upload_data = dict((param, oauth_request.get(param))
                               for param in params)

            form = APIUpload(data=upload_data,
                             files=request.FILES,
                             file_names=request.FILES.keys())
            if not form.is_valid():
                result['success'] = False
                # Only log these errors if we haven't encountered some other error first.
                if 'error' not in result:
                    errors = []
                    for field in form:
                        if field.errors:
                            errors.append("%s:%s" % (field.name, field.errors))
                    result['error'] = "Invalid form fields.\n" + form.errors.as_text()
                
            else:
                title = form.cleaned_data['title']
                username = oauth_request.get_parameter('username')
                path = "/%s/%s" % (username, form.cleaned_data['path'])
                description = form.cleaned_data['description']
                labels = form.cleaned_data['labels'].split(',')
                labels = [label.strip() for label in labels if len(label.strip()) > 0]
                main_filename = form.cleaned_data['main_filename']
                ephemeral = form.cleaned_data['ephemeral']
                ttl_time = form.cleaned_data['ttl_time']
                ephemeral_subfiles = form.get_subfiles()
    
                main_rowkey = request.FILES[main_filename].row_key
                subfiles = {}
                for fname, fobj in request.FILES.iteritems():
                    if fname != main_filename:
                        subfiles[fname] = fobj.row_key
                dae_choice = ""
                filename = main_filename

                if ephemeral:
                    create_index = False
                else:
                    ttl_time = None
                    create_index = True
                    ephemeral_subfiles = None
    
                task = place_upload.delay(main_rowkey, subfiles, title, path,
                                          description, create_index=create_index,
                                          ephemeral_ttl=ttl_time, ephemeral_subfiles=ephemeral_subfiles)
    
                save_upload_task(username=username,
                                 task_id=task.task_id,
                                 row_key=main_rowkey,
                                 filename=filename,
                                 subfiles=subfiles,
                                 dae_choice="",
                                 task_name="place_upload",
                                 ephemeral=ephemeral)
                
                result['success'] = True
                result['task_id'] = task.task_id
    
    response = HttpResponse(simplejson.dumps(result, default=json_handler, indent=4), mimetype='application/json')
    return response