def share_file(request, rel_path): t = loader.get_template('file_server/share_file.html') if request.method != 'POST': form = share_file_form_t() else: form = share_file_form_t(request.POST) if form.is_valid(): base_dir = get_base_dir(request.user) #focus_index = request.POST.get('focus_index', 0) db_entry = '' abs_path = os.path.join(base_dir, rel_path) db_entry = rel_path check_read_access(abs_path, request.user) if not os.path.isfile(abs_path): raise Http404 public_link = public_link_t( account=request.user, rel_path=db_entry, inode_num = get_inode_num(abs_path), recipient=form.cleaned_data['recipient']) public_link.create() if form.cleaned_data['send_mail']: url = settings.SITE_URL + reverse('public_file', args=[public_link.key]) message = form.cleaned_data['message'] + '\n' + url subject = form.cleaned_data['subject'] send_mail(subject, message, request.user.email, [public_link.recipient], fail_silently=False) return HttpResponseRedirect(reverse('file_list', args=[os.path.split(rel_path)[0]])) c = RequestContext(request, { 'rel_path':rel_path, 'request_path': request.get_full_path(), 'form': form }) return HttpResponse(t.render(c))
def upload_file(request): if request.method != 'POST': raise Http404 form = upload_file_form_t(request.POST, request.FILES) if form.is_valid(): try: rel_path = form.cleaned_data['rel_path'] abs_path = os.path.normpath( os.path.join(os.path.join(get_base_dir(request.user), rel_path), request.FILES['upload_file'].name)) check_write_access(abs_path, request.user) uploaded_file = request.FILES['upload_file'] if os.path.exists(abs_path): return file_already_exists_dialog(request, request.FILES['upload_file'].name, reverse('file_list', args=[rel_path])) request.user.get_profile().create_file(uploaded_file.size) handle_uploaded_file(uploaded_file, abs_path) return HttpResponseRedirect(reverse('file_list', args=[rel_path])) except disk_quota_exceeded_t as e: return no_more_disk_space_dialog(request, e, rel_path) except inode_quota_exceeded_t as e: return no_more_inodes_dialog(request, e, rel_path) # in case of form errors t = loader.get_template('file_server/upload_form.html') c = RequestContext(request, { 'form' : form }) return HttpResponse(t.render(c))
def upload_to_public_folder(request): if request.method != 'POST': raise Http404 form = upload_file_form_t(request.POST, request.FILES) if form.is_valid(): try: key = form.cleaned_data['rel_path'] public_link = get_object_or_404(public_link_t, key=key, is_directory=True, write_access=True) rel_path = public_link.rel_path abs_path = os.path.normpath( os.path.join(os.path.join(get_base_dir(public_link.account), rel_path), request.FILES['upload_file'].name)) check_write_access(abs_path, public_link.account) if os.path.exists(abs_path): next_url = reverse('public_dir', args=[key]) return file_already_exists_dialog(request, request.FILES['upload_file'].name, next_url) uploaded_file = request.FILES['upload_file'] public_link.account.get_profile().create_file(uploaded_file.size) handle_uploaded_file(uploaded_file, abs_path) return HttpResponseRedirect(reverse('public_dir', args=[key])) except disk_quota_exceeded_t as e: return no_more_disk_space_dialog(request, e, rel_path) except inode_quota_exceeded_t as e: return no_more_inodes_dialog(request, e, rel_path) # in case of form errors t = loader.get_template('file_server/upload_form.html') c = RequestContext(request, { 'form' : form, 'public' : True }) return HttpResponse(t.render(c))
def unshare_file(request, key): base_dir = get_base_dir(request.user) focus_index = request.GET.get('focus', 0) public_link = get_object_or_404(public_link_t, key=key) abs_path = os.path.join(base_dir, public_link.rel_path) check_read_access(abs_path, request.user) if public_link.inode_num != get_inode_num(abs_path): raise Http404 rel_path = os.path.split(public_link.rel_path)[0] public_link.delete() return HttpResponseRedirect(reverse('file_list', args=[rel_path])+'?focus='+focus_index)
def handle(self, *args, **options): link_list = public_link_t.objects.all() del_count = 0 for link in link_list: try: abs_path = os.path.join(get_base_dir(link.account), link.rel_path) inode_num = get_inode_num(abs_path) if inode_num != link.inode_num: link.delete() del_count += 1 except (IOError, OSError): link.delete() del_count += 1 print "deleted %d entries" % del_count
def send_file(request, rel_path): try: base_dir = get_base_dir(request.user) abs_path = os.path.join(base_dir, rel_path) check_read_access(abs_path, request.user) filename = os.path.split(rel_path)[1] mime_type = mimetypes.guess_type(filename) wrapper = FileWrapper(file(abs_path)) response = HttpResponse(wrapper, content_type=mime_type) response['Content-Disposition'] = 'attachment; filename=' + filename response['Content-Length'] = os.path.getsize(abs_path) return response except IOError: raise Http404
def file_list(request, rel_path): focus_index = request.GET.get('focus', 0) file_list_view = reverse('file_list', current_app='file_server', args=['']) base_dir = get_base_dir(request.user) directory = os.path.normpath(os.path.join(base_dir, rel_path)) #@ReservedAssignment if (not os.path.isdir(directory)): raise Http404 check_read_access(directory, request.user) path = '' if (rel_path): path = rel_path + '/' parent = '' if len(directory) >= len(base_dir): parent = file_list_view + os.path.split(rel_path)[0] dir_content = os.listdir(directory) file_list = [] for f in dir_content: abs_path = os.path.join(directory, f) if os.path.isdir(abs_path): rel_path_to_dir = os.path.join(rel_path, f) public_links = public_link_t.objects.filter(account=request.user, rel_path=rel_path_to_dir, is_directory=True, inode_num=get_inode_num(abs_path)) size = len(os.listdir(abs_path)) file_list.append(file_entry_t(f, public_links, size, True)) for f in dir_content: rel_path_to_file = os.path.join(rel_path, f) abs_path = os.path.join(directory, f) #ctime = get_ctime(abs_path) if os.path.isfile(abs_path): public_links = public_link_t.objects.filter( account=request.user, rel_path=rel_path_to_file, is_directory=False, inode_num=get_inode_num(abs_path)) file_size = os.path.getsize(abs_path) file_list.append(file_entry_t(f, public_links, file_size)) t = loader.get_template('file_server/file_list.html') upload_form = upload_file_form_t(initial={'rel_path':rel_path}) user_profile = get_user_profile(request.user) c = RequestContext(request, { 'file_list': file_list, 'rel_path': rel_path, 'path': path, 'file_list_view': file_list_view, 'parent': parent, 'upload_form': upload_form, 'focus_index': focus_index, 'quota': user_profile }) return HttpResponse(t.render(c))
def delete_file(request, rel_path): quota = request.user.get_profile() base_dir = get_base_dir(request.user) abs_path = os.path.join(base_dir, rel_path) check_write_access(abs_path, request.user) if os.path.isfile(abs_path): size = os.path.getsize(abs_path) os.unlink(abs_path) quota.delete_file(size) elif os.path.isdir(abs_path) and os.listdir(abs_path) != 0: os.rmdir(abs_path) quota.delete_dir() else: raise Http404 return HttpResponseRedirect(reverse( 'file_list', args=[os.path.split(rel_path)[0]],current_app='file_server'))
def send_public_file(request, key, filename): public_link = get_object_or_404(public_link_t, key=key, is_directory=True) try: base_dir = get_base_dir(public_link.account) directory = os.path.join(base_dir, public_link.rel_path) abs_path = os.path.join(directory, filename) # XXX check if file is in shared directory!!! check_read_access(abs_path, public_link.account) if not is_direct_silbling(directory, filename): raise Http404 mime_type = mimetypes.guess_type(filename) wrapper = FileWrapper(file(abs_path)) response = HttpResponse(wrapper, content_type=mime_type) response['Content-Disposition'] = 'attachment; filename=' + filename response['Content-Length'] = os.path.getsize(abs_path) return response except IOError: raise Http404
def public_file(request, key): public_link = get_object_or_404(public_link_t, key=key, is_directory=False) try: base_dir = get_base_dir(public_link.account) abs_path = os.path.join(base_dir, public_link.rel_path) filename = os.path.split(public_link.rel_path)[1] mime_type = mimetypes.guess_type(filename) if public_link.inode_num != get_inode_num(abs_path): raise Http404 wrapper = FileWrapper(file(abs_path)) response = HttpResponse(wrapper, content_type=mime_type) response['Content-Disposition'] = 'attachment; filename=' + filename response['Content-Length'] = os.path.getsize(abs_path) public_link.download_count += 1 public_link.save() return response except IOError: raise Http404
def delete_file_dialog(request, rel_path): base_dir = get_base_dir(request.user) abs_path = os.path.join(base_dir, rel_path) check_write_access(abs_path, request.user) parent = os.path.split(rel_path)[0] is_dir = False if os.path.isdir(abs_path): is_dir = True if len(os.listdir(abs_path)) != 0: # directory not empty return error_dialog(request,_("directory is not empty"),\ reverse("file_list", args=[os.path.split(rel_path)[0]])) elif not os.path.isfile(abs_path): raise Http404 t = loader.get_template('file_server/delete_file.html') c = Context({ 'filename': os.path.split(abs_path)[1], 'rel_path': rel_path, 'parent': parent, 'is_dir': is_dir }) return HttpResponse(t.render(c))
def public_directory(request, key): public_link = get_object_or_404(public_link_t, key=key, is_directory=True) try: base_dir = get_base_dir(public_link.account) directory = os.path.join(base_dir, public_link.rel_path) if public_link.inode_num != get_inode_num(directory): raise Http404 check_read_access(directory, public_link.account) dir_content = os.listdir(directory) file_list = [] for f in dir_content: rel_path_to_file = os.path.join(directory, f) abs_path = os.path.join(directory, f) if os.path.isfile(abs_path): file_size = os.path.getsize(abs_path) file_list.append(file_entry_t(f, [], file_size)) t = loader.get_template('file_server/public_dir.html') #upload_form = upload_file_form_t(initial={'rel_path':rel_path}) user_profile = get_user_profile(public_link.account) upload_form = None if public_link.write_access: upload_form = upload_file_form_t(initial={'rel_path':key}) public_link.download_count += 1 public_link.save() c = RequestContext(request, { 'file_list': file_list, 'quota': user_profile, 'path' : public_link.rel_path, 'key' : key, 'upload_form' : upload_form }) return HttpResponse(t.render(c)) except IOError: raise Http404
def create_directory(request, rel_path): form = None if request.method != 'POST': form = create_directory_form_t() else: form = create_directory_form_t(request.POST) if form.is_valid(): # try to create the directory abs_path = os.path.normpath(os.path.join(os.path.join(\ get_base_dir(request.user), \ rel_path), form.cleaned_data['dir_name'])) check_write_access(abs_path, request.user) try: quota = request.user.get_profile() quota.create_dir() os.mkdir(abs_path) return HttpResponseRedirect(reverse( 'file_list', args=[rel_path])) except inode_quota_exceeded_t as e: return no_more_inodes_dialog(request, e, os.path.split(rel_path)[0]) except EnvironmentError, e: msg = _('cannot create directory') + ": " + e.strerror form.add_error(msg)
def check_read_access(abs_path, user): base_dir = get_base_dir(user) if (os.path.normpath(abs_path).find(base_dir) != 0): raise Http404