def add_file(request): if not request.user.is_authenticated() or not request.user.is_active: return HttpResponse("Must be logged in to upload files!") if request.method == 'POST': path = "../uploads/" + str(request.user.id) + "/" + request.POST['path'] #Attempt to create directory try: os.makedirs(path) except: pass key = request.POST['key'] #If a file is uploaded, write to a new file if request.POST.get('filename'): new_file = request.FILES['file'] filename = request.POST['filename'] path = "../uploads/" + str(request.user.id) + "/" + request.POST['path'] full_path = path + filename cfile = open(full_path, "wb+") for chunk in new_file.chunks(): cfile.write(chunk) cfile.close() action = UserAction( user=request.user, action="add", path=request.POST['path'] + filename, key=key, timestamp=getDateTime(key), ip=getClientIp(request) ) action.save() #Otherwise, we've already created a directory else: action = UserAction( user=request.user, action="add", path=request.POST['path'], key=key, timestamp=getDateTime(key), ip=getClientIp(request) ) action.save() return HttpResponse("Great success!") return HttpResponse("Failed to upload anything.")
def delete_file(request): if not request.user.is_authenticated(): return HttpResponse("Must be logged in to upload files!") if request.method == 'POST': try: path = "../uploads/" + str(request.user.id) + "/" + request.POST['path'] if (path[len(path)-1]=="/"): shutil.rmtree(path) else: os.remove(path) new_log = UserAction( user=request.user, action="delete", path=request.POST['path'], key=request.POST['key'], timestamp=getDateTime(request.POST['key']), ip=getClientIp(request) ) new_log.save() return HttpResponse("File at %s deleted" % request.POST['path']) except: return HttpResponse("Failed to delete file at %s" % request.POST['path'])
def share(request): if not request.user.is_authenticated: return HttpResponseRedirect('/') else: if request.method == 'POST': username = request.POST['username'] user = User.objects.get(username=username) path = '../uploads/' + str(request.user.id) + "/" copy_path = '../uploads/' + str(user.id) + "/" #Create a new log element for the designated user for every file in requesting user's filesystem for root, folder, filename in os.walk(path): key_counter = 0 for f in filename: #Get relative path of current file newpath = "/" + os.path.relpath(os.path.join(root, f), path) print newpath key = str(datetime.now()) + str(key_counter) action = UserAction( user=user, action="add", path=newpath, key=key, timestamp=getDateTime(key), ip=getClientIp(request) ) action.save() key_counter + 1 #Copy files from requesting user to designated user's filesystem copyTree(path, copy_path) return HttpResponseRedirect('/myprofile/') if request.method == 'GET': return render(request, 'share.html', { 'username': request.user.username, 'is_admin': isAdmin(request.user.id) })