1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-02 07:47:32 +00:00

Merge pull request #3590 from haiwen/repo_dir_trash

repo dir trash API
This commit is contained in:
Daniel Pan 2019-06-05 20:58:32 +08:00 committed by GitHub
commit e5d34be5a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,90 @@
# Copyright (c) 2012-2019 Seafile Ltd.
# encoding: utf-8
import stat
import logging
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from seahub.api2.throttling import UserRateThrottle
from seahub.api2.authentication import TokenAuthentication
from seahub.api2.utils import api_error
from seahub.views import check_folder_permission
from seaserv import seafile_api
from seahub.utils import normalize_dir_path
logger = logging.getLogger(__name__)
class RepoCommitDirView(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)
def _get_item_info(self, dirent, path):
# # seahub/seahub/api2/views get_dir_file_recursively
entry = {}
if stat.S_ISDIR(dirent.mode):
entry['type'] = 'dir'
else:
entry['type'] = 'file'
entry['size'] = dirent.size
entry['parent_dir'] = path
entry['obj_id'] = dirent.obj_id
entry['name'] = dirent.obj_name
return entry
def get(self, request, repo_id, commit_id, format=None):
""" List dir by commit
used when get files/dirs in a trash or history dir
Permission checking:
1. all authenticated user can perform this action.
"""
# argument check
path = request.GET.get('path', '/')
path = normalize_dir_path(path)
# resource check
repo = seafile_api.get_repo(repo_id)
if not repo:
error_msg = 'Library %s not found.' % repo_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
commit = seafile_api.get_commit(repo.id, repo.version, commit_id)
if not commit:
error_msg = 'Commit %s not found.' % commit
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
dir_id = seafile_api.get_dir_id_by_commit_and_path(repo_id, commit_id, path)
if not dir_id:
error_msg = 'Folder %s not found.' % path
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
# permission check
if not check_folder_permission(request, repo_id, '/'):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
# # seafile_api.list_dir_by_commit_and_path
# def list_dir_by_commit_and_path(self, repo_id, commit_id, path, offset=-1, limit=-1):
# dir_id = seafserv_threaded_rpc.get_dir_id_by_commit_and_path(repo_id, commit_id, path)
# if dir_id is None:
# return None
# return seafserv_threaded_rpc.list_dir(repo_id, dir_id, offset, limit)
dir_entries = seafile_api.list_dir_by_dir_id(repo_id, dir_id)
items = []
for dirent in dir_entries:
items.append(self._get_item_info(dirent, path))
return Response({'dirent_list': items})

View File

@ -52,6 +52,7 @@ from seahub.api2.endpoints.dir import DirView, DirDetailView
from seahub.api2.endpoints.file_tag import FileTagView
from seahub.api2.endpoints.file_tag import FileTagsView
from seahub.api2.endpoints.repo_trash import RepoTrash
from seahub.api2.endpoints.repo_commit_dir import RepoCommitDirView
from seahub.api2.endpoints.deleted_repos import DeletedRepos
from seahub.api2.endpoints.repo_history import RepoHistory
from seahub.api2.endpoints.repo_set_password import RepoSetPassword
@ -324,6 +325,7 @@ urlpatterns = [
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/file/history/$', FileHistoryView.as_view(), name='api-v2.1-file-history-view'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/file/new_history/$', NewFileHistoryView.as_view(), name='api-v2.1-new-file-history-view'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/dir/$', DirView.as_view(), name='api-v2.1-dir-view'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/commits/(?P<commit_id>[0-9a-f]{40})/dir/$', RepoCommitDirView.as_view(), name='api-v2.1-repo-commit-dir'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/dir/detail/$', DirDetailView.as_view(), name='api-v2.1-dir-detail-view'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/trash/$', RepoTrash.as_view(), name='api-v2.1-repo-trash'),
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/history/$', RepoHistory.as_view(), name='api-v2.1-repo-history'),

View File

@ -0,0 +1,97 @@
import os
import json
from django.core.urlresolvers import reverse
from seaserv import seafile_api
from seahub.test_utils import BaseTestCase
from seahub.utils import normalize_dir_path
from tests.common.utils import randstring
class RepoCommitDirTest(BaseTestCase):
def setUp(self):
self.user_name = self.user.username
self.admin_name = self.admin.username
self.repo_id = self.repo.id
self.repo_name = self.repo.repo_name
self.file_path = self.file
self.file_name = os.path.basename(self.file_path)
self.folder_path = self.folder
self.folder_name = os.path.basename(self.folder.rstrip('/'))
self.inner_file_name = 'inner.txt'
self.inner_file = self.create_file(
repo_id=self.repo.id, parent_dir=self.folder_path,
filename=self.inner_file_name, username=self.user_name)
self.trash_url = reverse('api-v2.1-repo-trash', args=[self.repo_id])
def tearDown(self):
self.remove_repo()
self.remove_group()
def test_get(self):
# delete a folder first
seafile_api.del_file(self.repo_id, '/', self.folder_name, self.user_name)
self.login_as(self.user)
# get commit id
trash_resp = self.client.get(self.trash_url)
self.assertEqual(200, trash_resp.status_code)
trash_json_resp = json.loads(trash_resp.content)
assert trash_json_resp['data'][0]['obj_name'] == self.folder_name
assert trash_json_resp['data'][0]['is_dir']
assert trash_json_resp['data'][0]['commit_id']
commit_id = trash_json_resp['data'][0]['commit_id']
url = reverse('api-v2.1-repo-commit-dir', args=[self.repo_id, commit_id])
# test can get
resp = self.client.get(url + '?path=' + self.folder_path)
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
assert json_resp['dirent_list']
assert json_resp['dirent_list'][0]
assert json_resp['dirent_list'][0]['type'] == 'file'
assert json_resp['dirent_list'][0]['name'] == self.inner_file_name
assert json_resp['dirent_list'][0]['parent_dir'] == normalize_dir_path(self.folder_path)
assert json_resp['dirent_list'][0]['size'] == 0
# test can get without path
resp = self.client.get(url)
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
assert json_resp['dirent_list']
assert json_resp['dirent_list'][0]
assert json_resp['dirent_list'][0]['type'] == 'dir'
assert json_resp['dirent_list'][0]['name'] == self.folder_name
assert json_resp['dirent_list'][0]['parent_dir'] == '/'
assert json_resp['dirent_list'][0]['obj_id']
assert json_resp['dirent_list'][1]
assert json_resp['dirent_list'][1]['type'] == 'file'
assert json_resp['dirent_list'][1]['name'] == self.file_name
assert json_resp['dirent_list'][1]['parent_dir'] == '/'
assert json_resp['dirent_list'][0]['obj_id']
assert json_resp['dirent_list'][1]['size'] == 0
# test_can_not_get_with_invalid_path_parameter
invalid_path = randstring(6)
resp = self.client.get(url + '?path=' + invalid_path)
self.assertEqual(404, resp.status_code)
# test_can_not_get_with_invalid_repo_permission
self.logout()
self.login_as(self.admin)
resp = self.client.get(url + '?path=' + self.folder_path)
self.assertEqual(403, resp.status_code)