From 9642fd129db8526c4e39d31cb6ad1cc84e59d010 Mon Sep 17 00:00:00 2001 From: lian Date: Wed, 2 Mar 2016 10:04:59 +0800 Subject: [PATCH 1/2] [api2] add new owa file api --- seahub/api2/urls.py | 1 + seahub/api2/views.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/seahub/api2/urls.py b/seahub/api2/urls.py index 62be47da33..43e5ac5475 100644 --- a/seahub/api2/urls.py +++ b/seahub/api2/urls.py @@ -36,6 +36,7 @@ urlpatterns = patterns('', url(r'^repos/(?P[-0-9a-f]{36})/update-link/$', UpdateLinkView.as_view()), url(r'^repos/(?P[-0-9a-f]{36})/upload-blks-link/$', UploadBlksLinkView.as_view()), url(r'^repos/(?P[-0-9a-f]{36})/update-blks-link/$', UpdateBlksLinkView.as_view()), + url(r'^repos/(?P[-0-9-a-f]{36})/owa-file/$', OwaFileView.as_view(), name='api2-owa-file-view'), url(r'^repos/(?P[-0-9-a-f]{36})/file/$', FileView.as_view(), name='FileView'), url(r'^repos/(?P[-0-9a-f]{36})/files/(?P[0-9a-f]{40})/blks/(?P[0-9a-f]{40})/download-link/$', FileBlockDownloadLinkView.as_view()), url(r'^repos/(?P[-0-9-a-f]{36})/file/detail/$', FileDetailView.as_view()), diff --git a/seahub/api2/views.py b/seahub/api2/views.py index c938d933f8..804507f278 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -105,6 +105,16 @@ try: except ImportError: ORG_MEMBER_QUOTA_DEFAULT = None +try: + from seahub.settings import ENABLE_OFFICE_WEB_APP +except ImportError: + ENABLE_OFFICE_WEB_APP = False + +try: + from seahub.settings import OFFICE_WEB_APP_FILE_EXTENSION +except ImportError: + OFFICE_WEB_APP_FILE_EXTENSION = () + from pysearpc import SearpcError, SearpcObjEncoder import seaserv from seaserv import seafserv_rpc, seafserv_threaded_rpc, \ @@ -1643,6 +1653,71 @@ class StarredFileView(APIView): unstar_file(request.user.username, repo_id, path) return Response('success', status=status.HTTP_200_OK) +class OwaFileView(APIView): + + authentication_classes = (TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated, ) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, repo_id, format=None): + """ Get action url and access token when view file through Office Web App + """ + + # check args + repo = 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) + + path = request.GET.get('path', None) + if not path: + error_msg = 'path invalid.' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + try: + file_id = seafile_api.get_file_id_by_path(repo_id, path) + except SearpcError as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + + if not file_id: + error_msg = 'File %s not found.' % path + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + # check permission + if not is_pro_version(): + error_msg = 'Office Web App feature only supported in professional edition.' + return api_error(status.HTTP_403_FORBIDDEN, error_msg) + + if check_folder_permission(request, repo_id, path) is None: + error_msg = 'Permission denied.' + return api_error(status.HTTP_403_FORBIDDEN, error_msg) + + if repo.encrypted: + error_msg = 'Library encrypted.' + return api_error(status.HTTP_403_FORBIDDEN, error_msg) + + if not ENABLE_OFFICE_WEB_APP: + error_msg = 'Office Web App feature not enabled.' + return api_error(status.HTTP_403_FORBIDDEN, error_msg) + + filename = os.path.basename(path) + filetype, fileext = get_file_type_and_ext(filename) + if fileext not in OFFICE_WEB_APP_FILE_EXTENSION: + error_msg = 'file extension invalid.' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + # get wopi dict + from seahub_extra.wopi.utils import get_wopi_dict + username = request.user.username + wopi_dict = get_wopi_dict(username, repo_id, path) + + # send stats message + send_file_access_msg(request, repo, path, 'api') + return Response(wopi_dict) + + class FileView(APIView): """ Support uniform interface for file related operations, From d750019b84e03fc14fcd2b2ff480b6ca9e627c0c Mon Sep 17 00:00:00 2001 From: lian Date: Wed, 2 Mar 2016 13:39:25 +0800 Subject: [PATCH 2/2] [api2] update err msg of owa api --- seahub/api2/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 804507f278..0bed6f83ec 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -1705,7 +1705,7 @@ class OwaFileView(APIView): filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) if fileext not in OFFICE_WEB_APP_FILE_EXTENSION: - error_msg = 'file extension invalid.' + error_msg = 'path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # get wopi dict