diff --git a/seahub/api2/endpoints/cancel_zip_task.py b/seahub/api2/endpoints/cancel_zip_task.py new file mode 100644 index 0000000000..e644b5910e --- /dev/null +++ b/seahub/api2/endpoints/cancel_zip_task.py @@ -0,0 +1,36 @@ +# Copyright (c) 2012-2016 Seafile Ltd. +import logging + +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.utils import api_error + +from seaserv import seafile_api + +logger = logging.getLogger(__name__) + + +class CancelZipTaskView(APIView): + + throttle_classes = (UserRateThrottle, ) + + def post(self, request, format=None): + """ stop progress when download dir/multi. + Permission checking: + """ + token = request.POST.get('token', None) + if not token: + error_msg = 'token invalid.' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + try: + process = seafile_api.cancel_zip_task(token) + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + + return Response() diff --git a/seahub/urls.py b/seahub/urls.py index 6ca8a78913..fae5f281c1 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -44,6 +44,7 @@ from seahub.api2.endpoints.repo_set_password import RepoSetPassword from seahub.api2.endpoints.zip_task import ZipTaskView from seahub.api2.endpoints.share_link_zip_task import ShareLinkZipTaskView from seahub.api2.endpoints.query_zip_progress import QueryZipProgressView +from seahub.api2.endpoints.cancel_zip_task import CancelZipTaskView from seahub.api2.endpoints.copy_move_task import CopyMoveTaskView from seahub.api2.endpoints.query_copy_move_progress import QueryCopyMoveProgressView from seahub.api2.endpoints.invitations import InvitationsView, InvitationsBatchView @@ -249,6 +250,7 @@ urlpatterns = [ url(r'^api/v2.1/repos/(?P[-0-9a-f]{36})/file-uploaded-bytes/$', RepoFileUploadedBytesView.as_view(), name='api-v2.1-repo-file-uploaded-bytes'), 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/cancel-zip-task/$', CancelZipTaskView.as_view(), name='api-v2.1-cancel-zip-task'), url(r'^api/v2.1/copy-move-task/$', CopyMoveTaskView.as_view(), name='api-v2.1-copy-move-task'), url(r'^api/v2.1/query-copy-move-progress/$', QueryCopyMoveProgressView.as_view(), name='api-v2.1-query-copy-move-progress'), url(r'^api/v2.1/notifications/$', NotificationsView.as_view(), name='api-v2.1-notifications'), diff --git a/static/scripts/common.js b/static/scripts/common.js index 5c9c9f0a57..5f6d4adb86 100644 --- a/static/scripts/common.js +++ b/static/scripts/common.js @@ -80,6 +80,7 @@ define([ case 'download_dir_zip_url': return fileServerRoot + 'zip/' + options.zip_token; case 'zip_task': return siteRoot + 'api/v2.1/repos/' + options.repo_id + '/zip-task/'; case 'query_zip_progress': return siteRoot + 'api/v2.1/query-zip-progress/'; + case 'cancel_zip_task': return siteRoot + 'api/v2.1/cancel-zip-task/'; case 'copy_move_task': return siteRoot + 'api/v2.1/copy-move-task/'; case 'query_copy_move_progress': return siteRoot + 'api/v2.1/query-copy-move-progress/'; case 'rename_dir': return siteRoot + 'api/v2.1/repos/' + options.repo_id + '/dir/'; @@ -1057,6 +1058,18 @@ define([ }); }; + var cancelZipTask = function(){ + $.ajax({ + url: _this.getUrl({name: 'cancel_zip_task'}) + '?token=' + zip_token, + success: function(date) { + clearInterval(interval); + }, + error: function(xhr) { + _this.ajaxErrorHandler(xhr); + } + }); + }; + $.ajax({ url: _this.getUrl({ name: 'zip_task', @@ -1072,6 +1085,7 @@ define([ zip_token = data['zip_token']; $tip.html(packagingTip).modal(); $('#simplemodal-container').css({'width':'auto'}); + $('.simplemodal-close').click(function(){ cancelZipTask(); }); queryZipProgress(); interval = setInterval(queryZipProgress, 1000); },