예제 #1
0
파일: views.py 프로젝트: priestc/The-System
def handle_upload(request):
    """
    All uploads are handled by this view. It takes the file, saves it to the
    local filesystem, then adds the S3 upload to the upload queue.
    """
    
    f = request.FILES['file']
    meta = request.POST.get('meta', None)
    album = request.POST.get('album', None)
    artist = request.POST.get('artist', None)
    date = request.POST.get('date', None)
    profile = request.POST.get('profile', None)
    
    album_obj = Album(artist=artist, profile=profile, date=date,
                      album=album, meta=meta, size=(f.size/1073741824.0))
    album_obj.save()
    
    save_location = os.path.join(settings.UPLOAD_PATH, album_obj.filename)
    destination = open(save_location, 'wb+')
    
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()
    
    if not zipfile.is_zipfile(save_location):
        album_obj.delete()
        raise RuntimeError('Not valid zip file')
    
    ua = request.META['HTTP_USER_AGENT']
    
    if not ua.startswith("The Project Command Line Client") and \
    not ua.startswith("The Project GUI Client"):
        print ua
        raise Http404
    
    if not request.POST.get('password', None) == settings.CLIENT_PASS:
        print request.POST.get('password', None)
        raise Http404
        
    upload_to_remote_storage.delay(album_obj.pk, destination.name)
    
    return HttpResponse('%s bytes recieved from client!!!' % f.size,
                        mimetype='text/plain')