1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-18 08:16:07 +00:00

Merge pull request #1653 from haiwen/wopi-update

update wopi api
This commit is contained in:
Daniel Pan
2017-06-22 14:36:20 +08:00
committed by GitHub
2 changed files with 25 additions and 13 deletions

View File

@@ -9,7 +9,6 @@ import hashlib
import logging
import uuid
import posixpath
from dateutil.relativedelta import relativedelta
try:
import xml.etree.cElementTree as ET
@@ -20,7 +19,6 @@ from seaserv import seafile_api
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.utils import timezone
from seahub.profile.models import Profile
from seahub.utils import get_site_scheme_and_netloc
@@ -162,10 +160,8 @@ def get_wopi_dict(request_user, repo_id, file_path, action_name='view'):
# access_token_ttl property tells office web app
# when access token expires
expire_sec = WOPI_ACCESS_TOKEN_EXPIRATION
expiration= timezone.now() + relativedelta(seconds=expire_sec)
milliseconds_ttl = time.mktime(expiration.timetuple()) * 1000
access_token_ttl = int(milliseconds_ttl)
utc_timestamp = time.time()
access_token_ttl = int((utc_timestamp + WOPI_ACCESS_TOKEN_EXPIRATION) * 1000)
wopi_dict = {}
wopi_dict['action_url'] = full_action_url

View File

@@ -18,6 +18,7 @@ from django.core.cache import cache
from pysearpc import SearpcError
from seaserv import seafile_api
from seahub.base.accounts import User
from seahub.views import check_file_lock
from seahub.utils import gen_inner_file_get_url, \
gen_file_upload_url, get_file_type_and_ext, is_pro_version
@@ -27,12 +28,14 @@ from seahub.settings import SITE_ROOT
from .utils import get_file_info_by_token
from .settings import WOPI_ACCESS_TOKEN_EXPIRATION, \
ENABLE_OFFICE_WEB_APP_EDIT, OFFICE_WEB_APP_EDIT_FILE_EXTENSION
from .settings import ENABLE_OFFICE_WEB_APP_EDIT, \
OFFICE_WEB_APP_EDIT_FILE_EXTENSION
logger = logging.getLogger(__name__)
json_content_type = 'application/json; charset=utf-8'
WOPI_LOCK_EXPIRATION = 30 * 60
def generate_file_lock_key_value(request):
token = request.GET.get('access_token', None)
request_user, repo_id, file_path = get_file_info_by_token(token)
@@ -54,7 +57,7 @@ def generate_file_lock_key_value(request):
def lock_file(request):
key, value = generate_file_lock_key_value(request)
cache.set(key, value, WOPI_ACCESS_TOKEN_EXPIRATION)
cache.set(key, value, WOPI_LOCK_EXPIRATION)
def unlock_file(request):
key, value = generate_file_lock_key_value(request)
@@ -76,23 +79,36 @@ def access_token_check(func):
def _decorated(view, request, file_id, *args, **kwargs):
token = request.GET.get('access_token', None)
if not token:
logger.error('access_token invalid.')
return HttpResponse(json.dumps({}), status=401,
content_type=json_content_type)
request_user, repo_id, file_path = get_file_info_by_token(token)
if not request_user or not repo_id or not file_path:
logger.error('access_token invalid.')
return HttpResponse(json.dumps({}), status=401,
logger.error('File info invalid, user: %s, repo_id: %s, path: %s.'
% request_user, repo_id, file_path)
return HttpResponse(json.dumps({}), status=404,
content_type=json_content_type)
try:
User.objects.get(email=request_user)
except User.DoesNotExist:
logger.error('User %s not found.' % request_user)
return HttpResponse(json.dumps({}), status=404,
content_type=json_content_type)
repo = seafile_api.get_repo(repo_id)
if not repo:
logger.error('Library %s not found.' % repo_id)
return HttpResponse(json.dumps({}), status=401,
return HttpResponse(json.dumps({}), status=404,
content_type=json_content_type)
obj_id = seafile_api.get_file_id_by_path(repo_id, file_path)
if not obj_id:
logger.error('File %s not found.' % file_path)
return HttpResponse(json.dumps({}), status=401,
return HttpResponse(json.dumps({}), status=404,
content_type=json_content_type)
return func(view, request, file_id, *args, **kwargs)