Skip to content

Commit

Permalink
Onlyoffice pastelink (#6631)
Browse files Browse the repository at this point in the history
* add-only-office-pastelink-feature

* update

* update
  • Loading branch information
r350178982 authored Aug 24, 2024
1 parent 56999ee commit b6b5997
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 8 deletions.
5 changes: 4 additions & 1 deletion seahub/onlyoffice/api_urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2012-2016 Seafile Ltd.
from django.urls import path

from seahub.onlyoffice.views import OnlyofficeConvert
from seahub.onlyoffice.views import OnlyofficeConvert, OnlyofficeGetReferenceData
from seahub.onlyoffice.views import OnlyofficeFileHistory
from seahub.onlyoffice.views import OnlyofficeGetHistoryFileAccessToken

Expand All @@ -11,4 +11,7 @@
path('get-history-file-access-token/',
OnlyofficeGetHistoryFileAccessToken.as_view(),
name='onlyoffice_api_get_history_file_access_token'),
path('get-reference-data/',
OnlyofficeGetReferenceData.as_view(),
name='onlyoffice_api_get_reference_data'),
]
13 changes: 10 additions & 3 deletions seahub/onlyoffice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import urllib.parse
import posixpath

import json
from django.urls import reverse
from django.utils.encoding import force_bytes

Expand Down Expand Up @@ -141,8 +141,9 @@ def get_onlyoffice_dict(request, username, repo_id, file_path, file_id='',
base_url = get_site_scheme_and_netloc()
onlyoffice_editor_callback_url = reverse('onlyoffice_editor_callback')
callback_url = urllib.parse.urljoin(base_url, onlyoffice_editor_callback_url)

obj_id = seafile_api.get_file_id_by_path(repo_id, file_path)
avatar_url, _, _ = api_avatar_url(username, 72)
import jwt
return_dict = {
'repo_id': repo_id,
'path': file_path,
Expand All @@ -160,10 +161,16 @@ def get_onlyoffice_dict(request, username, repo_id, file_path, file_id='',
'onlyoffice_force_save': ONLYOFFICE_FORCE_SAVE,
'enable_watermark': ENABLE_WATERMARK,
'request_from_onlyoffice_desktop_editor': ONLYOFFICE_DESKTOP_EDITOR_HTTP_USER_AGENT in request.headers.get('user-agent', ''),
'file_key': jwt.encode({
'repo_id': origin_repo_id,
'file_path': origin_file_path,

}, ONLYOFFICE_JWT_SECRET),
'instance_id': base_url,
}

if ONLYOFFICE_JWT_SECRET:
import jwt

config = {
"document": {
"fileType": fileext,
Expand Down
79 changes: 76 additions & 3 deletions seahub/onlyoffice/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@

from seaserv import seafile_api

from seahub.onlyoffice.models import OnlyOfficeDocKey
from seahub.onlyoffice.settings import VERIFY_ONLYOFFICE_CERTIFICATE, ONLYOFFICE_JWT_SECRET
from seahub.onlyoffice.utils import get_onlyoffice_dict
from seahub.onlyoffice.utils import get_onlyoffice_dict, get_doc_key_by_repo_id_file_path
from seahub.onlyoffice.utils import delete_doc_key, get_file_info_by_doc_key
from seahub.onlyoffice.converter_utils import get_file_name_without_ext, \
get_file_ext, get_file_type, get_internal_extension
from seahub.onlyoffice.converter import get_converter_uri
from seahub.utils import gen_inner_file_upload_url, is_pro_version, \
normalize_file_path, check_filename_with_rename, \
gen_inner_file_get_url, get_service_url
normalize_file_path, check_filename_with_rename, \
gen_inner_file_get_url, get_service_url, get_file_type_and_ext, gen_file_get_url
from seahub.utils.file_op import if_locked_by_online_office
from seahub.views import check_folder_permission
from seahub.utils.file_types import SPREADSHEET


# Get an instance of a logger
Expand Down Expand Up @@ -434,3 +437,73 @@ def post(self, request):
payload['token'] = jwt_token

return Response({"data": payload})


class OnlyofficeGetReferenceData(APIView):

throttle_classes = (UserRateThrottle, )

def post(self, request):
instance_id = request.data.get('instanceId')
file_key = request.data.get('fileKey')

username = request.user.username

try:
payload = jwt.decode(file_key, ONLYOFFICE_JWT_SECRET, algorithms=['HS256'])
except:
err_msg = 'File key invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, err_msg)

source_repo_id = payload.get('repo_id')
source_file_path = payload.get('file_path')

if not seafile_api.get_repo(source_repo_id):
error_msg = 'Library %s not found.' % source_repo_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

file_id = seafile_api.get_file_id_by_path(source_repo_id, source_file_path)
if not file_id:
error_msg = 'Souce file does not exist'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

doc_key = get_doc_key_by_repo_id_file_path(source_repo_id, source_file_path)
file_name = os.path.basename(source_file_path.rstrip('/'))
parent_dir = os.path.dirname(source_file_path.rstrip('/'))
if check_folder_permission(request, source_repo_id, parent_dir) is None:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

filetype, fileext = get_file_type_and_ext(source_file_path)
if filetype != SPREADSHEET:
err_msg = 'file type invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, err_msg)

file_id = seafile_api.get_file_id_by_path(source_repo_id,
source_file_path)
dl_token = seafile_api.get_fileserver_access_token(
source_repo_id,
file_id,
'download',
username,
use_onetime=False
)

doc_url = gen_file_get_url(dl_token, file_name)
link = "%s%s"% (instance_id, reverse('view_lib_file', args=[
source_repo_id, source_file_path]))

result = {
"fileType": fileext,
"key": doc_key,
"path": file_name,
"referenceData": {
"fileKey": file_key,
"instanceId": instance_id,
},
"url": doc_url,
"link": link
}
result['token'] = jwt.encode(result, ONLYOFFICE_JWT_SECRET)
return Response({'data': result})

31 changes: 30 additions & 1 deletion seahub/templates/view_file_onlyoffice.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,35 @@
var onRequestHistoryClose = function () {
document.location.reload();
};

var onRequestReferenceData = function (event) {
$.ajax({
url: '{% url "onlyoffice_api_get_reference_data" %}',
type: 'POST',
dataType: 'json',
cache: false,
data: event.data.referenceData,
beforeSend: prepareCSRFToken,
success: function(resp) {
docEditor.setReferenceData(resp.data);
},
error: function(xhr, status, error) {
if (xhr.responseText) {
errMsg = JSON.parse(xhr.responseText).error_msg;
} else {
errMsg = "{% trans "Failed. Please check the network." %}";
}
docEditor.setReferenceData({
"error": errMsg
})
}
});
};
{% endif %}
var config = {
{% if onlyoffice_jwt_token %}
"events": {
"onRequestReferenceData": onRequestReferenceData,
"onRequestHistory": onRequestHistory,
"onRequestHistoryData": onRequestHistoryData,
"onRequestHistoryClose": onRequestHistoryClose,
Expand All @@ -95,7 +120,11 @@
"edit": {% if can_edit %}true{% else %}false{% endif %},
"print": {% if can_download %}true{% else %}false{% endif %},
"review": true
}
},
"referenceData": {
"fileKey": '{{ file_key }}',
"instanceId": "{{ instance_id }}",
},
},
"documentType": "{{ document_type }}", // 'text' | 'spreadsheet' | 'presentation',
{% if onlyoffice_jwt_token %}
Expand Down

0 comments on commit b6b5997

Please sign in to comment.