1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 23:20:51 +00:00

add amdin system library upload api

This commit is contained in:
lian
2017-07-03 14:42:12 +08:00
parent 101a2015ba
commit 46a5473326
9 changed files with 139 additions and 17 deletions

View File

@@ -10,6 +10,7 @@ from rest_framework import status
from seaserv import seafile_api
from seahub.views import get_system_default_repo_id
from seahub.utils import gen_file_upload_url, normalize_dir_path
from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle
@@ -38,3 +39,54 @@ class AdminSystemLibrary(APIView):
result['description'] = repo.desc
return Response(result)
class AdminSystemLibraryUploadLink(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
throttle_classes = (UserRateThrottle,)
permission_classes = (IsAdminUser,)
def get(self, request):
# argument check
req_from = request.GET.get('from', 'web')
if req_from not in ('web', 'api'):
error_msg = 'from invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
# recourse check
try:
repo_id = seafile_api.get_system_default_repo_id()
repo = seafile_api.get_repo(repo_id)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
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('path', '/')
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)
token = seafile_api.get_fileserver_access_token(repo_id,
'dummy', 'upload', 'system', use_onetime=False)
if not token:
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
if req_from == 'api':
url = gen_file_upload_url(token, 'upload-api')
else:
url = gen_file_upload_url(token, 'upload-aj')
result = {}
result['upload_link'] = url
return Response(result)

View File

@@ -55,7 +55,8 @@ from seahub.api2.endpoints.admin.devices import AdminDevices
from seahub.api2.endpoints.admin.device_errors import AdminDeviceErrors
from seahub.api2.endpoints.admin.libraries import AdminLibraries, AdminLibrary
from seahub.api2.endpoints.admin.library_dirents import AdminLibraryDirents, AdminLibraryDirent
from seahub.api2.endpoints.admin.system_library import AdminSystemLibrary
from seahub.api2.endpoints.admin.system_library import AdminSystemLibrary, \
AdminSystemLibraryUploadLink
from seahub.api2.endpoints.admin.default_library import AdminDefaultLibrary
from seahub.api2.endpoints.admin.trash_libraries import AdminTrashLibraries, AdminTrashLibrary
from seahub.api2.endpoints.admin.groups import AdminGroups, AdminGroup
@@ -251,6 +252,7 @@ urlpatterns = patterns(
## admin::system-library
url(r'^api/v2.1/admin/system-library/$', AdminSystemLibrary.as_view(), name='api-v2.1-admin-system-library'),
url(r'^api/v2.1/admin/system-library/upload-link/$', AdminSystemLibraryUploadLink.as_view(), name='api-v2.1-admin-system-library-upload-link'),
## admin::default-library
url(r'^api/v2.1/admin/default-library/$', AdminDefaultLibrary.as_view(), name='api-v2.1-admin-default-library'),

View File

@@ -153,18 +153,25 @@ define([
}
var upload_file = function() {
var ajax_url, ajax_data;
if (dirents.is_system_library) {
ajax_url = Common.getUrl({ name: 'admin-system-library-upload-link', repo_id: dirents.repo_id });
ajax_data = { 'from': 'web', 'path': dirents.path };
} else {
ajax_url = Common.getUrl({ name: 'repo_upload_link', repo_id: dirents.repo_id });
ajax_data = { 'from': 'web', 'p': dirents.path };
}
$.ajax({
url: Common.getUrl({
name: 'repo_upload_link',
repo_id: dirents.repo_id
}),
data: {
'from': 'web',
'p': dirents.path
},
url: ajax_url,
data: ajax_data,
cache: false,
dataType: 'json',
success: function(returned_url) {
if (dirents.is_system_library) {
returned_url = returned_url['upload_link'];
}
if (enable_upload_folder && file.relative_path) { // 'add folder'
var file_path = file.relative_path,
r_path = file_path.substring(0, file_path.lastIndexOf('/') + 1),
@@ -249,7 +256,7 @@ define([
$(files).each(function() {
file_names.push(this.get('obj_name'));
});
if (file_names.indexOf(file.name) != -1) { // file with the same name already exists in the dir
if (!dirents.is_system_library && file_names.indexOf(file.name) != -1) { // file with the same name already exists in the dir
var confirm_title = gettext("Replace file {filename}?")
.replace('{filename}', '<span class="op-target">' + Common.HTMLescape(file.name) + '</span>');
var confirm_popup = $(_this.fileupdateConfirmTemplate({

View File

@@ -181,6 +181,7 @@ define([
case 'admin-group-members': return siteRoot + 'api/v2.1/admin/groups/' + options.group_id + '/members/';
case 'admin-group-member': return siteRoot + 'api/v2.1/admin/groups/' + options.group_id + '/members/' + options.email+ '/';
case 'admin-system-library': return siteRoot + 'api/v2.1/admin/system-library/';
case 'admin-system-library-upload-link': return siteRoot + 'api/v2.1/admin/system-library/upload-link/';
case 'admin-trash-libraries': return siteRoot + 'api/v2.1/admin/trash-libraries/';
case 'admin-trash-library': return siteRoot + 'api/v2.1/admin/trash-libraries/' + options.repo_id + '/';
case 'admin_shares': return siteRoot + 'api/v2.1/admin/shares/';

View File

@@ -3,7 +3,7 @@ from django.core.urlresolvers import reverse
from seahub.test_utils import BaseTestCase
from tests.common.utils import randstring
class LibrariesTest(BaseTestCase):
class AdminLibrariesTest(BaseTestCase):
def setUp(self):
self.libraries_url = reverse('api-v2.1-admin-libraries')
@@ -115,7 +115,7 @@ class LibrariesTest(BaseTestCase):
self.assertEqual(404, resp.status_code)
class LibraryTest(BaseTestCase):
class AdminLibraryTest(BaseTestCase):
def setUp(self):
self.user_name = self.user.username

View File

@@ -0,0 +1,46 @@
import json
from seaserv import seafile_api
from django.core.urlresolvers import reverse
from seahub.test_utils import BaseTestCase
from tests.common.utils import upload_file_test
class AdminSystemLibraryTest(BaseTestCase):
def setUp(self):
self.url = reverse('api-v2.1-admin-system-library')
def test_can_get(self):
self.login_as(self.admin)
resp = self.client.get(self.url)
json_resp = json.loads(resp.content)
assert json_resp['id'] == seafile_api.get_system_default_repo_id()
def test_get_with_invalid_user_permission(self):
self.login_as(self.user)
resp = self.client.get(self.url)
self.assertEqual(403, resp.status_code)
class AdminSystemLibraryUploadLinkTest(BaseTestCase):
def setUp(self):
self.url = reverse('api-v2.1-admin-system-library-upload-link')
def test_can_get(self):
self.login_as(self.admin)
resp = self.client.get(self.url)
json_resp = json.loads(resp.content)
assert '8082' in json_resp['upload_link']
assert 'upload' in json_resp['upload_link']
# test upload file via `upload_link`
upload_file_test(json_resp['upload_link'])
def test_get_with_invalid_user_permission(self):
self.login_as(self.user)
resp = self.client.get(self.url)
self.assertEqual(403, resp.status_code)

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import json
from tests.common.utils import randstring
from tests.common.utils import randstring, upload_file_test
from django.core.urlresolvers import reverse
from seahub.test_utils import BaseTestCase
from seahub.share.models import UploadLinkShare
@@ -97,6 +97,9 @@ class AdminUploadLinkUploadTest(BaseTestCase):
assert '8082' in json_resp['upload_link']
assert 'upload' in json_resp['upload_link']
# test upload file via `upload_link`
upload_file_test(json_resp['upload_link'])
self._remove_upload_link(token)
def test_upload_with_invalid_permission(self):

View File

@@ -1,5 +1,6 @@
import string
import random
import requests
from .common import BASE_URL
@@ -19,3 +20,13 @@ def urljoin(base, *args):
def apiurl(*parts):
return urljoin(BASE_URL, *parts)
def upload_file_test(upload_link):
file_name = randstring(6)
files = {
'file': (file_name, 'Some lines in this file'),
'parent_dir': '/',
}
resp = requests.post(upload_link, files=files)
assert 200 == resp.status_code