Skip to content

Commit

Permalink
sdoc to docx
Browse files Browse the repository at this point in the history
  • Loading branch information
imwhatiam committed Dec 25, 2023
1 parent 646c6d6 commit 4ac0d7b
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 35 deletions.
14 changes: 14 additions & 0 deletions frontend/src/components/dirent-grid-view/dirent-grid-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ class DirentGridView extends React.Component {
this.props.onItemConvert(currentObject, dstType);
};

exportDocx = () => {
const serviceUrl = window.app.config.serviceURL;
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
let exportToDocxUrl = serviceUrl + '/repo/sdoc_export_to_docx/' + repoID + '/?file_path=' + filePath;
window.location.href = exportToDocxUrl;
};

onMenuItemClick = (operation, currentObject, event) => {
hideMenu();
switch(operation) {
Expand Down Expand Up @@ -150,6 +158,12 @@ class DirentGridView extends React.Component {
case 'Convert to Markdown':
this.onItemConvert(currentObject, event, 'markdown');
break;
case 'Convert to docx':
this.onItemConvert(currentObject, event, 'docx');
break;
case 'Export docx':
this.exportDocx();
break;
case 'Convert to sdoc':
this.onItemConvert(currentObject, event, 'sdoc');
break;
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/components/dirent-list-view/dirent-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ class DirentListItem extends React.Component {
this.setState({isShareDialogShow: !this.state.isShareDialogShow});
};

exportDocx = () => {
const serviceUrl = window.app.config.serviceURL;
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
let exportToDocxUrl = serviceUrl + '/repo/sdoc_export_to_docx/' + repoID + '/?file_path=' + filePath;
window.location.href = exportToDocxUrl;
};

closeSharedDialog = () => {
this.setState({isShareDialogShow: !this.state.isShareDialogShow});
};
Expand Down Expand Up @@ -277,6 +285,12 @@ class DirentListItem extends React.Component {
case 'Convert to Markdown':
this.onItemConvert(event, 'markdown');
break;
case 'Convert to docx':
this.onItemConvert(event, 'docx');
break;
case 'Export docx':
this.exportDocx();
break;
case 'Convert to sdoc':
this.onItemConvert(event, 'sdoc');
break;
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/utils/text-translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const TextTranslation = {
'UNLOCK' : {key : 'Unlock', value : gettext('Unlock')},
'CONVERT_TO_MARKDOWN' : {key : 'Convert to Markdown', value : gettext('Convert to Markdown')},
'CONVERT_TO_SDOC' : {key : 'Convert to sdoc', value : gettext('Convert to sdoc')},
'CONVERT_TO_DOCX' : {key : 'Convert to docx', value : gettext('Convert to docx')},
'EXPORT_DOCX' : {key : 'Export docx', value : gettext('Export docx')},
'MARK_AS_DRAFT' : {key : 'Mark as draft', value : gettext('Mark as draft')},
'UNMARK_AS_DRAFT' : {key : 'Unmark as draft', value : gettext('Unmark as draft')},
'HISTORY' : {key : 'History', value : gettext('History')},
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export const Utils = {
getFileOperationList: function(isRepoOwner, currentRepoInfo, dirent, isContextmenu) {
let list = [];
const { SHARE, DOWNLOAD, DELETE, RENAME, MOVE, COPY, TAGS, UNLOCK, LOCK, FREEZE_DOCUMENT,
HISTORY, ACCESS_LOG, PROPERTIES, OPEN_VIA_CLIENT, ONLYOFFICE_CONVERT, CONVERT_TO_MARKDOWN, CONVERT_TO_SDOC } = TextTranslation;
HISTORY, ACCESS_LOG, PROPERTIES, OPEN_VIA_CLIENT, ONLYOFFICE_CONVERT, CONVERT_TO_MARKDOWN, CONVERT_TO_DOCX, EXPORT_DOCX, CONVERT_TO_SDOC } = TextTranslation;
const permission = dirent.permission;
const { isCustomPermission, customPermission } = Utils.getUserPermission(permission);

Expand Down Expand Up @@ -613,6 +613,8 @@ export const Utils = {

if (dirent.name.endsWith('.sdoc')) {
list.push(CONVERT_TO_MARKDOWN);
list.push(CONVERT_TO_DOCX);
list.push(EXPORT_DOCX);
}
}

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ openpyxl==3.0.*
Markdown==3.4.*
bleach==5.0.*
python-ldap==3.4.*
python-docx==1.1.*
51 changes: 38 additions & 13 deletions seahub/api2/endpoints/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from seahub.seadoc.models import SeadocHistoryName, SeadocDraft, SeadocCommentReply
from seahub.base.models import FileComment
from seahub.settings import MAX_UPLOAD_FILE_NAME_LEN, OFFICE_TEMPLATE_ROOT
from seahub.api2.endpoints.utils import convert_file
from seahub.api2.endpoints.utils import convert_file, sdoc_convert_to_docx
from seahub.seadoc.utils import get_seadoc_file_uuid

from seahub.drafts.models import Draft
Expand Down Expand Up @@ -572,13 +572,16 @@ def post(self, request, repo_id, format=None):

if extension == '.md':
src_type = 'markdown'
filename = filename[:-2] + 'sdoc'
new_filename = filename[:-2] + 'sdoc'
elif extension == '.sdoc':
src_type = 'sdoc'
filename = filename[:-4] + 'md'
if dst_type == 'markdown':
new_filename = filename[:-4] + 'md'
if dst_type == 'docx':
new_filename = filename[:-4] + 'docx'

new_file_name = check_filename_or_rename(repo_id, parent_dir, filename)
new_file_path = posixpath.join(parent_dir, new_file_name)
new_filename = check_filename_or_rename(repo_id, parent_dir, new_filename)
new_file_path = posixpath.join(parent_dir, new_filename)

download_token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username)

Expand All @@ -587,15 +590,37 @@ def post(self, request, repo_id, format=None):
use_onetime=True)
doc_uuid = get_seadoc_file_uuid(repo, path)

try:
resp = convert_file(path, username, doc_uuid, download_token, upload_token, src_type, dst_type)
if dst_type != 'docx':
try:
resp = convert_file(path, username, doc_uuid,
download_token, upload_token,
src_type, dst_type)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

if resp.status_code == 500:
logger.error('convert file error status: %s body: %s', resp.status_code, resp.text)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
logger.error('convert file error status: %s body: %s',
resp.status_code, resp.text)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR,
'Internal Server Error')
else:

try:
resp = sdoc_convert_to_docx(path, username, doc_uuid,
download_token, upload_token,
src_type, dst_type)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

if resp.status_code != 200:
logger.error('convert file error status: %s body: %s',
resp.status_code, resp.text)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR,
'Internal Server Error')

file_info = self.get_file_info(username, repo_id, new_file_path)
return Response(file_info)
Expand Down
Loading

0 comments on commit 4ac0d7b

Please sign in to comment.