示例#1
0
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))
示例#2
0
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))
示例#3
0
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)
示例#4
0
 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
示例#5
0
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
示例#6
0
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