From 11152071c6399771a06b323c2e41eaa40f91b13b Mon Sep 17 00:00:00 2001 From: lian Date: Fri, 9 Aug 2019 14:24:06 +0800 Subject: [PATCH] add ONLYOFFICE_FORCE_SAVE setting (#3934) * add ONLYOFFICE_FORCE_SAVE setting * update comment --- seahub/onlyoffice/settings.py | 3 +++ seahub/onlyoffice/utils.py | 23 +++++++++++++++++++--- seahub/onlyoffice/views.py | 15 ++++++++++++++ seahub/templates/view_file_onlyoffice.html | 3 +++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/seahub/onlyoffice/settings.py b/seahub/onlyoffice/settings.py index ace7c88100..c458eae18d 100644 --- a/seahub/onlyoffice/settings.py +++ b/seahub/onlyoffice/settings.py @@ -6,3 +6,6 @@ ONLYOFFICE_APIJS_URL = getattr(settings, 'ONLYOFFICE_APIJS_URL', '') ONLYOFFICE_FILE_EXTENSION = getattr(settings, 'ONLYOFFICE_FILE_EXTENSION', ()) ONLYOFFICE_EDIT_FILE_EXTENSION = getattr(settings, 'ONLYOFFICE_EDIT_FILE_EXTENSION', ()) VERIFY_ONLYOFFICE_CERTIFICATE = getattr(settings, 'VERIFY_ONLYOFFICE_CERTIFICATE', True) + +# if True, file will be saved when user click save btn on file editing page +ONLYOFFICE_FORCE_SAVE = getattr(settings, 'ONLYOFFICE_FORCE_SAVE', False) diff --git a/seahub/onlyoffice/utils.py b/seahub/onlyoffice/utils.py index 7980681a62..a623b11a0a 100644 --- a/seahub/onlyoffice/utils.py +++ b/seahub/onlyoffice/utils.py @@ -11,10 +11,16 @@ from django.utils.encoding import force_bytes from seaserv import seafile_api from seahub.utils import get_file_type_and_ext, gen_file_get_url, \ - get_site_scheme_and_netloc + get_site_scheme_and_netloc, normalize_cache_key from seahub.settings import ENABLE_WATERMARK -from seahub.onlyoffice.settings import ONLYOFFICE_APIJS_URL +from seahub.onlyoffice.settings import ONLYOFFICE_APIJS_URL, \ + ONLYOFFICE_FORCE_SAVE + +def generate_onlyoffice_cache_key(repo_id, file_path): + prefix = "ONLYOFFICE_" + value = "%s_%s" % (repo_id, file_path) + return normalize_cache_key(value, prefix) def get_onlyoffice_dict(username, repo_id, file_path, file_id='', can_edit=False, can_download=True): @@ -47,8 +53,18 @@ def get_onlyoffice_dict(username, repo_id, file_path, else: document_type = 'text' + cache_key = generate_onlyoffice_cache_key(repo_id, file_path) + doc_key = cache.get(cache_key) + + # temporary solution when failed to get data from cache(django_pylibmc) + # when init process for the first time + if not doc_key: + doc_key = cache.get(cache_key) + + if not doc_key: + doc_key = hashlib.md5(force_bytes(origin_repo_id + origin_file_path + file_id)).hexdigest()[:20] + doc_info = json.dumps({'repo_id': repo_id, 'file_path': file_path, 'username': username}) - doc_key = hashlib.md5(force_bytes(origin_repo_id + origin_file_path + file_id)).hexdigest()[:20] cache.set("ONLYOFFICE_%s" % doc_key, doc_info, None) file_name = os.path.basename(file_path.rstrip('/')) @@ -71,6 +87,7 @@ def get_onlyoffice_dict(username, repo_id, file_path, 'can_edit': can_edit, 'can_download': can_download, 'username': username, + 'onlyoffice_force_save': ONLYOFFICE_FORCE_SAVE, 'enable_watermark': ENABLE_WATERMARK and not can_edit, } diff --git a/seahub/onlyoffice/views.py b/seahub/onlyoffice/views.py index fe8d33e036..25309769cb 100644 --- a/seahub/onlyoffice/views.py +++ b/seahub/onlyoffice/views.py @@ -10,6 +10,7 @@ from django.views.decorators.csrf import csrf_exempt from seaserv import seafile_api from seahub.onlyoffice.settings import VERIFY_ONLYOFFICE_CERTIFICATE +from seahub.onlyoffice.utils import generate_onlyoffice_cache_key from seahub.utils import gen_inner_file_upload_url # Get an instance of a logger @@ -73,7 +74,11 @@ def onlyoffice_editor_callback(request): post_data = json.loads(request.body) status = int(post_data.get('status', -1)) + # When forcesave is initiated, document editing service performs request to + # the callback handler with the link to the document as the url parameter and + # with the 6 value for the status parameter. if status in (2, 6): + # Defines the link to the edited document to be saved with the document storage service. # The link is present when the status value is equal to 2 or 3 only. url = post_data.get('url') @@ -90,6 +95,16 @@ def onlyoffice_editor_callback(request): file_path = doc_info['file_path'] username = doc_info['username'] + cache_key = generate_onlyoffice_cache_key(repo_id, file_path) + # cache document key when forcesave + if status == 6: + cache.set(cache_key, doc_key) + + # remove document key from cache when document is ready for saving + # no one is editting + if status == 2: + cache.delete(cache_key) + fake_obj_id = {'online_office_update': True,} update_token = seafile_api.get_fileserver_access_token(repo_id, json.dumps(fake_obj_id), 'update', username) diff --git a/seahub/templates/view_file_onlyoffice.html b/seahub/templates/view_file_onlyoffice.html index 4cd8ccffd1..b7bfb00ed5 100644 --- a/seahub/templates/view_file_onlyoffice.html +++ b/seahub/templates/view_file_onlyoffice.html @@ -39,6 +39,9 @@ html, body { padding:0; margin:0; height:100%; } "callbackUrl": "{{ callback_url }}", "lang": "{{ LANGUAGE_CODE }}", "mode": {% if can_edit %}"edit"{% else %}"view"{% endif %}, + "customization": { + "forcesave": {% if onlyoffice_force_save %}true{% else %}false{% endif %}, + }, "user": { "name": "{{ username|email2nickname|escapejs }}" }