mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-20 02:48:51 +00:00
With parent (#2872)
* update dir api add with_parents parameter if value of 'p' parameter is '/a/b/c' add with_parents's is 'true' then return dirent list in '/', '/a', '/a/b' and '/a/b/c'. * add try exception logic
This commit is contained in:
@@ -13,7 +13,7 @@ from django.utils.http import urlquote
|
|||||||
|
|
||||||
from seahub.api2.throttling import UserRateThrottle
|
from seahub.api2.throttling import UserRateThrottle
|
||||||
from seahub.api2.authentication import TokenAuthentication
|
from seahub.api2.authentication import TokenAuthentication
|
||||||
from seahub.api2.utils import api_error
|
from seahub.api2.utils import api_error, to_python_boolean
|
||||||
from seahub.api2.views import get_dir_file_recursively
|
from seahub.api2.views import get_dir_file_recursively
|
||||||
|
|
||||||
from seahub.thumbnail.utils import get_thumbnail_src
|
from seahub.thumbnail.utils import get_thumbnail_src
|
||||||
@@ -36,114 +36,19 @@ from pysearpc import SearpcError
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class DirView(APIView):
|
|
||||||
"""
|
|
||||||
Support uniform interface for directory operations, including
|
|
||||||
create/delete/rename/list, etc.
|
|
||||||
"""
|
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
|
||||||
permission_classes = (IsAuthenticated, )
|
|
||||||
throttle_classes = (UserRateThrottle, )
|
|
||||||
|
|
||||||
def get_dir_info(self, repo_id, dir_path):
|
def get_dir_file_info_list(username, request_type, repo_obj, parent_dir,
|
||||||
|
with_thumbnail, thumbnail_size):
|
||||||
dir_obj = seafile_api.get_dirent_by_path(repo_id, dir_path)
|
|
||||||
dir_info = {
|
|
||||||
'type': 'dir',
|
|
||||||
'repo_id': repo_id,
|
|
||||||
'parent_dir': os.path.dirname(dir_path.rstrip('/')),
|
|
||||||
'obj_name': dir_obj.obj_name,
|
|
||||||
'obj_id': dir_obj.obj_id,
|
|
||||||
'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
|
|
||||||
}
|
|
||||||
|
|
||||||
return dir_info
|
|
||||||
|
|
||||||
def get(self, request, repo_id, format=None):
|
|
||||||
""" Get dir info.
|
|
||||||
|
|
||||||
Permission checking:
|
|
||||||
1. user with either 'r' or 'rw' permission.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# argument check
|
|
||||||
recursive = request.GET.get('recursive', '0')
|
|
||||||
if recursive not in ('1', '0'):
|
|
||||||
error_msg = "If you want to get recursive dir entries, you should set 'recursive' argument as '1'."
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
request_type = request.GET.get('t', '')
|
|
||||||
if request_type and request_type not in ('f', 'd'):
|
|
||||||
error_msg = "'t'(type) should be 'f' or 'd'."
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
with_thumbnail = request.GET.get('with_thumbnail', 'false')
|
|
||||||
if with_thumbnail not in ('true', 'false'):
|
|
||||||
error_msg = 'with_thumbnail invalid.'
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
if with_thumbnail == 'true':
|
|
||||||
thumbnail_size = request.GET.get('thumbnail_size', 48)
|
|
||||||
try:
|
|
||||||
thumbnail_size = int(thumbnail_size)
|
|
||||||
except ValueError:
|
|
||||||
error_msg = 'thumbnail_size invalid.'
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
# recource 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)
|
|
||||||
|
|
||||||
parent_dir = request.GET.get('p', '/')
|
|
||||||
parent_dir = normalize_dir_path(parent_dir)
|
|
||||||
|
|
||||||
dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir)
|
|
||||||
if not dir_id:
|
|
||||||
error_msg = 'Folder %s not found.' % parent_dir
|
|
||||||
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
|
||||||
|
|
||||||
# permission check
|
|
||||||
permission = check_folder_permission(request, repo_id, parent_dir)
|
|
||||||
if not permission:
|
|
||||||
error_msg = 'Permission denied.'
|
|
||||||
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
|
||||||
|
|
||||||
# get dir/file list recursively
|
|
||||||
username = request.user.username
|
|
||||||
if recursive == '1':
|
|
||||||
dir_file_info_list = get_dir_file_recursively(username, repo_id,
|
|
||||||
parent_dir, [])
|
|
||||||
|
|
||||||
response_dict = {}
|
|
||||||
response_dict['dirent_list'] = []
|
|
||||||
|
|
||||||
if request_type == 'f':
|
|
||||||
for item in dir_file_info_list:
|
|
||||||
if item['type'] == 'file':
|
|
||||||
response_dict['dirent_list'].append(item)
|
|
||||||
elif request_type == 'd':
|
|
||||||
for item in dir_file_info_list:
|
|
||||||
if item['type'] == 'dir':
|
|
||||||
response_dict['dirent_list'].append(item)
|
|
||||||
else:
|
|
||||||
response_dict['dirent_list'] = dir_file_info_list
|
|
||||||
|
|
||||||
return Response(response_dict)
|
|
||||||
|
|
||||||
# get dirent(folder and file) list
|
|
||||||
try:
|
|
||||||
dir_file_list = seafile_api.list_dir_with_perm(repo_id,
|
|
||||||
parent_dir, dir_id, username, -1, -1)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(e)
|
|
||||||
error_msg = 'Internal Server Error'
|
|
||||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
|
||||||
|
|
||||||
|
repo_id = repo_obj.id
|
||||||
dir_info_list = []
|
dir_info_list = []
|
||||||
file_info_list = []
|
file_info_list = []
|
||||||
|
|
||||||
|
# get dirent(folder and file) list
|
||||||
|
parent_dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir)
|
||||||
|
dir_file_list = seafile_api.list_dir_with_perm(repo_id,
|
||||||
|
parent_dir, parent_dir_id, username, -1, -1)
|
||||||
|
|
||||||
# only get dir info list
|
# only get dir info list
|
||||||
if not request_type or request_type == 'd':
|
if not request_type or request_type == 'd':
|
||||||
dir_list = [dirent for dirent in dir_file_list if stat.S_ISDIR(dirent.mode)]
|
dir_list = [dirent for dirent in dir_file_list if stat.S_ISDIR(dirent.mode)]
|
||||||
@@ -154,6 +59,7 @@ class DirView(APIView):
|
|||||||
dir_info["name"] = dirent.obj_name
|
dir_info["name"] = dirent.obj_name
|
||||||
dir_info["mtime"] = dirent.mtime
|
dir_info["mtime"] = dirent.mtime
|
||||||
dir_info["permission"] = dirent.permission
|
dir_info["permission"] = dirent.permission
|
||||||
|
dir_info["parent_dir"] = parent_dir
|
||||||
dir_info_list.append(dir_info)
|
dir_info_list.append(dir_info)
|
||||||
|
|
||||||
# only get file info list
|
# only get file info list
|
||||||
@@ -192,6 +98,7 @@ class DirView(APIView):
|
|||||||
file_info["name"] = file_name
|
file_info["name"] = file_name
|
||||||
file_info["mtime"] = dirent.mtime
|
file_info["mtime"] = dirent.mtime
|
||||||
file_info["permission"] = dirent.permission
|
file_info["permission"] = dirent.permission
|
||||||
|
file_info["parent_dir"] = parent_dir
|
||||||
file_info["size"] = dirent.size
|
file_info["size"] = dirent.size
|
||||||
|
|
||||||
modifier_email = dirent.modifier
|
modifier_email = dirent.modifier
|
||||||
@@ -227,7 +134,7 @@ class DirView(APIView):
|
|||||||
file_info['file_tags'].append(file_tag)
|
file_info['file_tags'].append(file_tag)
|
||||||
|
|
||||||
# get thumbnail info
|
# get thumbnail info
|
||||||
if with_thumbnail == 'true' and not repo.encrypted:
|
if with_thumbnail and not repo_obj.encrypted:
|
||||||
|
|
||||||
# used for providing a way to determine
|
# used for providing a way to determine
|
||||||
# if send a request to create thumbnail.
|
# if send a request to create thumbnail.
|
||||||
@@ -252,15 +159,158 @@ class DirView(APIView):
|
|||||||
dir_info_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
|
dir_info_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
|
||||||
file_info_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
|
file_info_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
|
||||||
|
|
||||||
|
return dir_info_list, file_info_list
|
||||||
|
|
||||||
|
|
||||||
|
class DirView(APIView):
|
||||||
|
"""
|
||||||
|
Support uniform interface for directory operations, including
|
||||||
|
create/delete/rename/list, etc.
|
||||||
|
"""
|
||||||
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||||
|
permission_classes = (IsAuthenticated, )
|
||||||
|
throttle_classes = (UserRateThrottle, )
|
||||||
|
|
||||||
|
def get_dir_info(self, repo_id, dir_path):
|
||||||
|
|
||||||
|
dir_obj = seafile_api.get_dirent_by_path(repo_id, dir_path)
|
||||||
|
dir_info = {
|
||||||
|
'type': 'dir',
|
||||||
|
'repo_id': repo_id,
|
||||||
|
'parent_dir': os.path.dirname(dir_path.rstrip('/')),
|
||||||
|
'obj_name': dir_obj.obj_name,
|
||||||
|
'obj_id': dir_obj.obj_id,
|
||||||
|
'mtime': timestamp_to_isoformat_timestr(dir_obj.mtime),
|
||||||
|
}
|
||||||
|
|
||||||
|
return dir_info
|
||||||
|
|
||||||
|
def get(self, request, repo_id, format=None):
|
||||||
|
""" Get sub dirent list info.
|
||||||
|
|
||||||
|
Permission checking:
|
||||||
|
1. user with either 'r' or 'rw' permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# argument check
|
||||||
|
recursive = request.GET.get('recursive', '0')
|
||||||
|
if recursive not in ('1', '0'):
|
||||||
|
error_msg = "If you want to get recursive dir entries, you should set 'recursive' argument as '1'."
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
request_type = request.GET.get('t', '')
|
||||||
|
if request_type and request_type not in ('f', 'd'):
|
||||||
|
error_msg = "'t'(type) should be 'f' or 'd'."
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
with_thumbnail = request.GET.get('with_thumbnail', 'false')
|
||||||
|
if with_thumbnail not in ('true', 'false'):
|
||||||
|
error_msg = 'with_thumbnail invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
with_thumbnail = to_python_boolean(with_thumbnail)
|
||||||
|
thumbnail_size = request.GET.get('thumbnail_size', 48)
|
||||||
|
try:
|
||||||
|
thumbnail_size = int(thumbnail_size)
|
||||||
|
except ValueError:
|
||||||
|
error_msg = 'thumbnail_size invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
with_parents = request.GET.get('with_parents', 'false')
|
||||||
|
if with_parents not in ('true', 'false'):
|
||||||
|
error_msg = 'with_parents invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
with_parents = to_python_boolean(with_parents)
|
||||||
|
|
||||||
|
# recource 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)
|
||||||
|
|
||||||
|
parent_dir = request.GET.get('p', '/')
|
||||||
|
parent_dir = normalize_dir_path(parent_dir)
|
||||||
|
|
||||||
|
dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir)
|
||||||
|
if not dir_id:
|
||||||
|
error_msg = 'Folder %s not found.' % parent_dir
|
||||||
|
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
||||||
|
|
||||||
|
# permission check
|
||||||
|
permission = check_folder_permission(request, repo_id, parent_dir)
|
||||||
|
if not permission:
|
||||||
|
error_msg = 'Permission denied.'
|
||||||
|
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
||||||
|
|
||||||
|
# get dir/file list recursively
|
||||||
|
username = request.user.username
|
||||||
|
if recursive == '1':
|
||||||
|
|
||||||
|
try:
|
||||||
|
dir_file_info_list = get_dir_file_recursively(username, repo_id,
|
||||||
|
parent_dir, [])
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
error_msg = 'Internal Server Error'
|
||||||
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
|
response_dict = {}
|
||||||
|
response_dict['dirent_list'] = []
|
||||||
|
|
||||||
|
if request_type == 'f':
|
||||||
|
for item in dir_file_info_list:
|
||||||
|
if item['type'] == 'file':
|
||||||
|
response_dict['dirent_list'].append(item)
|
||||||
|
elif request_type == 'd':
|
||||||
|
for item in dir_file_info_list:
|
||||||
|
if item['type'] == 'dir':
|
||||||
|
response_dict['dirent_list'].append(item)
|
||||||
|
else:
|
||||||
|
response_dict['dirent_list'] = dir_file_info_list
|
||||||
|
|
||||||
|
return Response(response_dict)
|
||||||
|
|
||||||
|
parent_dir_list = []
|
||||||
|
if not with_parents:
|
||||||
|
# only return dirent list in current parent folder
|
||||||
|
parent_dir_list.append(parent_dir)
|
||||||
|
else:
|
||||||
|
# if value of 'p' parameter is '/a/b/c' add with_parents's is 'true'
|
||||||
|
# then return dirent list in '/', '/a', '/a/b' and '/a/b/c'.
|
||||||
|
if parent_dir == '/':
|
||||||
|
parent_dir_list.append(parent_dir)
|
||||||
|
else:
|
||||||
|
tmp_parent_dir = '/'
|
||||||
|
parent_dir_list.append(tmp_parent_dir)
|
||||||
|
for folder_name in parent_dir.strip('/').split('/'):
|
||||||
|
tmp_parent_dir = posixpath.join(tmp_parent_dir, folder_name)
|
||||||
|
parent_dir_list.append(tmp_parent_dir)
|
||||||
|
|
||||||
|
all_dir_info_list = []
|
||||||
|
all_file_info_list = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
for parent_dir in parent_dir_list:
|
||||||
|
# get dir file info list
|
||||||
|
dir_info_list, file_info_list = get_dir_file_info_list(username,
|
||||||
|
request_type, repo, parent_dir, with_thumbnail, thumbnail_size)
|
||||||
|
all_dir_info_list.extend(dir_info_list)
|
||||||
|
all_file_info_list.extend(file_info_list)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
error_msg = 'Internal Server Error'
|
||||||
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
response_dict = {}
|
response_dict = {}
|
||||||
response_dict["user_perm"] = permission
|
response_dict["user_perm"] = permission
|
||||||
|
|
||||||
if request_type == 'f':
|
if request_type == 'f':
|
||||||
response_dict['dirent_list'] = file_info_list
|
response_dict['dirent_list'] = all_file_info_list
|
||||||
elif request_type == 'd':
|
elif request_type == 'd':
|
||||||
response_dict['dirent_list'] = dir_info_list
|
response_dict['dirent_list'] = all_dir_info_list
|
||||||
else:
|
else:
|
||||||
response_dict['dirent_list'] = dir_info_list + file_info_list
|
response_dict['dirent_list'] = all_dir_info_list + all_file_info_list
|
||||||
|
|
||||||
return Response(response_dict)
|
return Response(response_dict)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user