mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-07 09:51:26 +00:00
rm unused dirent download api
This commit is contained in:
@@ -1,107 +0,0 @@
|
|||||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
|
||||||
import stat
|
|
||||||
import logging
|
|
||||||
import json
|
|
||||||
import posixpath
|
|
||||||
|
|
||||||
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 django.utils.translation import ugettext as _
|
|
||||||
|
|
||||||
from seahub.api2.throttling import UserRateThrottle
|
|
||||||
from seahub.api2.authentication import TokenAuthentication
|
|
||||||
from seahub.api2.utils import api_error
|
|
||||||
|
|
||||||
from seahub.utils import string2list, get_fileserver_root
|
|
||||||
from seahub.views import check_folder_permission
|
|
||||||
from seahub.views.file import send_file_access_msg
|
|
||||||
|
|
||||||
import seaserv
|
|
||||||
from seaserv import seafile_api
|
|
||||||
from pysearpc import SearpcError
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
class DirentsDownloadLinkView(APIView):
|
|
||||||
"""
|
|
||||||
Download multi files/dirs.
|
|
||||||
"""
|
|
||||||
|
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
|
||||||
permission_classes = (IsAuthenticated, )
|
|
||||||
throttle_classes = (UserRateThrottle, )
|
|
||||||
|
|
||||||
def get(self, request, repo_id, format=None):
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
# argument checking
|
|
||||||
parent_dir = request.GET.get('parent_dir', None)
|
|
||||||
dirent_name_string = request.GET.get('dirents', None)
|
|
||||||
|
|
||||||
if not parent_dir:
|
|
||||||
error_msg = 'parent_dir invalid.'
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
if not dirent_name_string:
|
|
||||||
error_msg = 'dirents invalid.'
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
# folder exist checking
|
|
||||||
if not seafile_api.get_dir_id_by_path(repo_id, parent_dir):
|
|
||||||
error_msg = 'Folder %s not found.' % parent_dir
|
|
||||||
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
|
||||||
|
|
||||||
# permission checking
|
|
||||||
if check_folder_permission(request, repo_id, parent_dir) is None:
|
|
||||||
error_msg = 'Permission denied.'
|
|
||||||
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
|
||||||
|
|
||||||
dirent_name_list = string2list(dirent_name_string)
|
|
||||||
dirent_list = []
|
|
||||||
total_size = 0
|
|
||||||
for dirent_name in dirent_name_list:
|
|
||||||
dirent_name = dirent_name.strip('/')
|
|
||||||
dirent_list.append(dirent_name)
|
|
||||||
|
|
||||||
full_dirent_path = posixpath.join(parent_dir, dirent_name)
|
|
||||||
current_dirent = seafile_api.get_dirent_by_path(repo_id, full_dirent_path)
|
|
||||||
if stat.S_ISDIR(current_dirent.mode):
|
|
||||||
total_size += seafile_api.get_dir_size(repo.store_id,
|
|
||||||
repo.version, current_dirent.obj_id)
|
|
||||||
else:
|
|
||||||
total_size += current_dirent.size
|
|
||||||
|
|
||||||
if total_size > seaserv.MAX_DOWNLOAD_DIR_SIZE:
|
|
||||||
error_msg = _('Total size exceeds limit.')
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
||||||
|
|
||||||
fake_obj_id = {}
|
|
||||||
fake_obj_id['file_list'] = dirent_list
|
|
||||||
fake_obj_id['parent_dir'] = parent_dir
|
|
||||||
|
|
||||||
username = request.user.username
|
|
||||||
try:
|
|
||||||
token = seafile_api.get_fileserver_access_token(repo_id,
|
|
||||||
json.dumps(fake_obj_id), 'download-multi', username, False)
|
|
||||||
except SearpcError as e:
|
|
||||||
logger.error(e)
|
|
||||||
error_msg = 'Internal Server Error'
|
|
||||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
|
||||||
|
|
||||||
if len(dirent_list) > 10:
|
|
||||||
send_file_access_msg(request, repo, parent_dir, 'web')
|
|
||||||
else:
|
|
||||||
for dirent_name in dirent_list:
|
|
||||||
full_dirent_path = posixpath.join(parent_dir, dirent_name)
|
|
||||||
send_file_access_msg(request, repo, full_dirent_path, 'web')
|
|
||||||
|
|
||||||
download_url = '%s/files/%s' % (get_fileserver_root(), token)
|
|
||||||
return Response({'url': download_url})
|
|
@@ -64,7 +64,6 @@ urlpatterns = patterns('',
|
|||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/$', DirView.as_view(), name='DirView'),
|
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/$', DirView.as_view(), name='DirView'),
|
||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/sub_repo/$', DirSubRepoView.as_view(), name="api2-dir-sub-repo"),
|
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/sub_repo/$', DirSubRepoView.as_view(), name="api2-dir-sub-repo"),
|
||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/shared_items/$', DirSharedItemsEndpoint.as_view(), name="api2-dir-shared-items"),
|
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/shared_items/$', DirSharedItemsEndpoint.as_view(), name="api2-dir-shared-items"),
|
||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/download/$', DirDownloadView.as_view(), name='api2-dir-download'),
|
|
||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/revert/$', DirRevert.as_view(), name='api2-dir-revert'),
|
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/dir/revert/$', DirRevert.as_view(), name='api2-dir-revert'),
|
||||||
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/thumbnail/$', ThumbnailView.as_view(), name='api2-thumbnail'),
|
url(r'^repos/(?P<repo_id>[-0-9-a-f]{36})/thumbnail/$', ThumbnailView.as_view(), name='api2-thumbnail'),
|
||||||
url(r'^starredfiles/', StarredFileView.as_view(), name='starredfiles'),
|
url(r'^starredfiles/', StarredFileView.as_view(), name='starredfiles'),
|
||||||
|
@@ -2685,70 +2685,6 @@ class DirView(APIView):
|
|||||||
|
|
||||||
return reloaddir_if_necessary(request, repo, parent_dir_utf8)
|
return reloaddir_if_necessary(request, repo, parent_dir_utf8)
|
||||||
|
|
||||||
class DirDownloadView(APIView):
|
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication )
|
|
||||||
permission_classes = (IsAuthenticated,)
|
|
||||||
throttle_classes = (UserRateThrottle, )
|
|
||||||
|
|
||||||
def get(self, request, repo_id, format=None):
|
|
||||||
repo = get_repo(repo_id)
|
|
||||||
if not repo:
|
|
||||||
return api_error(status.HTTP_404_NOT_FOUND, 'Library not found.')
|
|
||||||
|
|
||||||
path = request.GET.get('p', None)
|
|
||||||
if path is None:
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, 'Path is missing.')
|
|
||||||
if path[-1] != '/': # Normalize dir path
|
|
||||||
path += '/'
|
|
||||||
|
|
||||||
if len(path) > 1:
|
|
||||||
dirname = os.path.basename(path.rstrip('/'))
|
|
||||||
else:
|
|
||||||
dirname = repo.name
|
|
||||||
|
|
||||||
current_commit = get_commits(repo_id, 0, 1)[0]
|
|
||||||
if not current_commit:
|
|
||||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
||||||
'Failed to get current commit of repo %s.' % repo_id)
|
|
||||||
|
|
||||||
try:
|
|
||||||
dir_id = seafile_api.get_dir_id_by_commit_and_path(current_commit.repo_id,
|
|
||||||
current_commit.id, path)
|
|
||||||
except SearpcError, e:
|
|
||||||
return api_error(HTTP_520_OPERATION_FAILED,
|
|
||||||
"Failed to get dir id by path")
|
|
||||||
|
|
||||||
if not dir_id:
|
|
||||||
return api_error(status.HTTP_404_NOT_FOUND, "Path does not exist")
|
|
||||||
|
|
||||||
try:
|
|
||||||
total_size = seafserv_threaded_rpc.get_dir_size(repo.store_id, repo.version,
|
|
||||||
dir_id)
|
|
||||||
except Exception, e:
|
|
||||||
logger.error(str(e))
|
|
||||||
return api_error(HTTP_520_OPERATION_FAILED, "Internal error")
|
|
||||||
|
|
||||||
if total_size > MAX_DOWNLOAD_DIR_SIZE:
|
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST,
|
|
||||||
'Unable to download directory "%s": size is too large.' % dirname)
|
|
||||||
|
|
||||||
is_windows = 0
|
|
||||||
if is_windows_operating_system(request):
|
|
||||||
is_windows = 1
|
|
||||||
|
|
||||||
fake_obj_id = {
|
|
||||||
'obj_id': dir_id,
|
|
||||||
'dir_name': dirname,
|
|
||||||
'is_windows': is_windows
|
|
||||||
}
|
|
||||||
|
|
||||||
token = seafile_api.get_fileserver_access_token(
|
|
||||||
repo_id, json.dumps(fake_obj_id), 'download-dir', request.user.username)
|
|
||||||
|
|
||||||
redirect_url = gen_file_get_url(token, dirname)
|
|
||||||
return HttpResponse(json.dumps(redirect_url), status=200,
|
|
||||||
content_type=json_content_type)
|
|
||||||
|
|
||||||
class DirRevert(APIView):
|
class DirRevert(APIView):
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||||
permission_classes = (IsAuthenticated,)
|
permission_classes = (IsAuthenticated,)
|
||||||
|
@@ -29,7 +29,6 @@ from seahub.api2.endpoints.upload_links import UploadLinks, UploadLink
|
|||||||
from seahub.api2.endpoints.file import FileView
|
from seahub.api2.endpoints.file import FileView
|
||||||
from seahub.api2.endpoints.dir import DirView
|
from seahub.api2.endpoints.dir import DirView
|
||||||
from seahub.api2.endpoints.repo_set_password import RepoSetPassword
|
from seahub.api2.endpoints.repo_set_password import RepoSetPassword
|
||||||
from seahub.api2.endpoints.dirents_download_link import DirentsDownloadLinkView
|
|
||||||
from seahub.api2.endpoints.zip_task import ZipTaskView
|
from seahub.api2.endpoints.zip_task import ZipTaskView
|
||||||
from seahub.api2.endpoints.share_link_zip_task import ShareLinkZipTaskView
|
from seahub.api2.endpoints.share_link_zip_task import ShareLinkZipTaskView
|
||||||
from seahub.api2.endpoints.query_zip_progress import QueryZipProgressView
|
from seahub.api2.endpoints.query_zip_progress import QueryZipProgressView
|
||||||
@@ -200,7 +199,6 @@ urlpatterns = patterns(
|
|||||||
url(r'^api/v2.1/upload-links/$', UploadLinks.as_view(), name='api-v2.1-upload-links'),
|
url(r'^api/v2.1/upload-links/$', UploadLinks.as_view(), name='api-v2.1-upload-links'),
|
||||||
url(r'^api/v2.1/upload-links/(?P<token>[a-f0-9]{10})/$', UploadLink.as_view(), name='api-v2.1-upload-link'),
|
url(r'^api/v2.1/upload-links/(?P<token>[a-f0-9]{10})/$', UploadLink.as_view(), name='api-v2.1-upload-link'),
|
||||||
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/file/$', FileView.as_view(), name='api-v2.1-file-view'),
|
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/file/$', FileView.as_view(), name='api-v2.1-file-view'),
|
||||||
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/dirents/download-link/$', DirentsDownloadLinkView.as_view(), name='api-v2.1-dirents-download-link-view'),
|
|
||||||
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/zip-task/$', ZipTaskView.as_view(), name='api-v2.1-zip-task'),
|
url(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/zip-task/$', ZipTaskView.as_view(), name='api-v2.1-zip-task'),
|
||||||
url(r'^api/v2.1/share-link-zip-task/$', ShareLinkZipTaskView.as_view(), name='api-v2.1-share-link-zip-task'),
|
url(r'^api/v2.1/share-link-zip-task/$', ShareLinkZipTaskView.as_view(), name='api-v2.1-share-link-zip-task'),
|
||||||
url(r'^api/v2.1/query-zip-progress/$', QueryZipProgressView.as_view(), name='api-v2.1-query-zip-progress'),
|
url(r'^api/v2.1/query-zip-progress/$', QueryZipProgressView.as_view(), name='api-v2.1-query-zip-progress'),
|
||||||
|
@@ -1,61 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse
|
|
||||||
|
|
||||||
from seahub.test_utils import BaseTestCase
|
|
||||||
|
|
||||||
|
|
||||||
class DirentsDownloadLinkViewTest(BaseTestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.repo_id = self.repo.id
|
|
||||||
|
|
||||||
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_path)
|
|
||||||
|
|
||||||
self.url = reverse('api-v2.1-dirents-download-link-view', args=[self.repo_id])
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.remove_repo()
|
|
||||||
|
|
||||||
def test_can_get_download_url(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
parent_dir = '/'
|
|
||||||
dirents = self.file_name + ',' + self.folder_name
|
|
||||||
url = self.url + '?parent_dir=%s&dirents=%s' % (parent_dir, dirents)
|
|
||||||
|
|
||||||
resp = self.client.get(url)
|
|
||||||
self.assertEqual(200, resp.status_code)
|
|
||||||
json_resp = json.loads(resp.content)
|
|
||||||
assert '8082' in json_resp['url']
|
|
||||||
|
|
||||||
def test_args_invalid(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
parent_dir = '/'
|
|
||||||
dirents = self.file_name + ',' + self.folder_name
|
|
||||||
|
|
||||||
url = self.url + '?prent_dir=%s&dirents=%s' % (parent_dir, dirents)
|
|
||||||
resp = self.client.get(url)
|
|
||||||
self.assertEqual(400, resp.status_code)
|
|
||||||
|
|
||||||
url = self.url + '?parent_dir=%s&dirent=%s' % (parent_dir, dirents)
|
|
||||||
resp = self.client.get(url)
|
|
||||||
self.assertEqual(400, resp.status_code)
|
|
||||||
|
|
||||||
url = self.url + '?parent_dir=%s&dirents=%s' % (parent_dir+'invalid', dirents)
|
|
||||||
resp = self.client.get(url)
|
|
||||||
self.assertEqual(404, resp.status_code)
|
|
||||||
|
|
||||||
def test_permission_invalid(self):
|
|
||||||
self.login_as(self.admin)
|
|
||||||
parent_dir = '/'
|
|
||||||
dirents = self.file_name + ',' + self.folder_name
|
|
||||||
|
|
||||||
url = self.url + '?parent_dir=%s&dirents=%s' % (parent_dir, dirents)
|
|
||||||
resp = self.client.get(url)
|
|
||||||
self.assertEqual(403, resp.status_code)
|
|
@@ -1,55 +0,0 @@
|
|||||||
"""seahub/api2/views.py::Repo api tests.
|
|
||||||
"""
|
|
||||||
import json
|
|
||||||
from tests.common.utils import randstring
|
|
||||||
from django.core.urlresolvers import reverse
|
|
||||||
from seaserv import seafile_api
|
|
||||||
from seahub.test_utils import BaseTestCase
|
|
||||||
try:
|
|
||||||
from seahub.settings import LOCAL_PRO_DEV_ENV
|
|
||||||
except ImportError:
|
|
||||||
LOCAL_PRO_DEV_ENV = False
|
|
||||||
|
|
||||||
class DirDownloadTest(BaseTestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.folder_path = self.folder
|
|
||||||
self.user2 = self.create_user('test2@test.com')
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.remove_repo()
|
|
||||||
self.remove_user(self.user.username)
|
|
||||||
self.remove_user(self.user2.username)
|
|
||||||
|
|
||||||
def test_can_download(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
|
|
||||||
dl_url = reverse('api2-dir-download', args=[self.repo.id]) + '?p=' + self.folder_path
|
|
||||||
resp = self.client.get(dl_url)
|
|
||||||
self.assertEqual(200, resp.status_code)
|
|
||||||
assert '8082/files/' in resp.content
|
|
||||||
|
|
||||||
def test_library_not_found(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
invalid_repo_id = self.repo.id[:-4] + '1234'
|
|
||||||
|
|
||||||
dl_url = reverse('api2-dir-download', args=[invalid_repo_id]) + '?p=' + self.folder_path
|
|
||||||
resp = self.client.get(dl_url)
|
|
||||||
self.assertEqual(404, resp.status_code)
|
|
||||||
|
|
||||||
def test_path_is_missing(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
|
|
||||||
dl_url = reverse('api2-dir-download', args=[self.repo.id])
|
|
||||||
resp = self.client.get(dl_url)
|
|
||||||
self.assertEqual(400, resp.status_code)
|
|
||||||
|
|
||||||
dl_url = reverse('api2-dir-download', args=[self.repo.id]) + '?pa=' + self.folder_path
|
|
||||||
resp = self.client.get(dl_url)
|
|
||||||
self.assertEqual(400, resp.status_code)
|
|
||||||
|
|
||||||
def test_wrong_path(self):
|
|
||||||
self.login_as(self.user)
|
|
||||||
|
|
||||||
dl_url = reverse('api2-dir-download', args=[self.repo.id]) + '?p=' + self.folder_path + '/asf/'
|
|
||||||
resp = self.client.get(dl_url)
|
|
||||||
self.assertEqual(404, resp.status_code)
|
|
@@ -295,15 +295,6 @@ class FilesApiTest(ApiTestBase):
|
|||||||
self.assertEqual(res.text, u'"success"')
|
self.assertEqual(res.text, u'"success"')
|
||||||
self.get(durl, expected=404)
|
self.get(durl, expected=404)
|
||||||
|
|
||||||
def test_download_dir(self):
|
|
||||||
with self.get_tmp_repo() as repo:
|
|
||||||
dpath, _ = self.create_dir(repo)
|
|
||||||
query = '?p=%s' % quote(dpath)
|
|
||||||
ddurl = urljoin(repo.dir_url, 'download') + query
|
|
||||||
res = self.get(ddurl)
|
|
||||||
self.assertRegexpMatches(res.text,
|
|
||||||
r'"http(.*)/files/[^/]+/%s"' % quote(dpath[1:]))
|
|
||||||
|
|
||||||
@pytest.mark.xfail
|
@pytest.mark.xfail
|
||||||
def test_create_dir_with_parents(self):
|
def test_create_dir_with_parents(self):
|
||||||
with self.get_tmp_repo() as repo:
|
with self.get_tmp_repo() as repo:
|
||||||
|
Reference in New Issue
Block a user