From 935f3d71e8df25698f1dfb1d708f999237f269a2 Mon Sep 17 00:00:00 2001 From: shanshuirenjia <978987373@qq.com> Date: Tue, 18 Jan 2022 21:29:45 +0800 Subject: [PATCH] optimize code --- frontend/src/markdown-editor.js | 4 ++-- frontend/src/shared-file-view-markdown.js | 1 + seahub/views/file.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/src/markdown-editor.js b/frontend/src/markdown-editor.js index 153e855a35..2a9c0bbf0b 100644 --- a/frontend/src/markdown-editor.js +++ b/frontend/src/markdown-editor.js @@ -26,7 +26,7 @@ const { siteRoot, serviceUrl, seafileCollabServer } = window.app.config; const userInfo = window.app.userInfo; const userName = userInfo.username; let dirPath = Utils.getDirName(filePath); -const IMAGE_SUFFIXES = ['png', 'PNG', 'jpg', 'jpeg', 'JPG', 'gif', 'GIF']; +const IMAGE_SUFFIXES = ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF']; function getImageFileNameWithTimestamp() { var d = Date.now(); @@ -734,7 +734,7 @@ class MarkdownEditor extends React.Component { const fileName = Utils.getFileName(filePath); const suffix = fileName.slice(fileName.indexOf('.') + 1); if (IMAGE_SUFFIXES.includes(suffix)) { - let innerURL = serviceUrl + '/lib/' + repoID + '/file' + filePath + '?raw=1'; + let innerURL = serviceUrl + '/lib/' + repoID + '/file' + Utils.encodePath(filePath) + '?raw=1'; window.richMarkdownEditor.addLink(fileName, innerURL, true); return; } diff --git a/frontend/src/shared-file-view-markdown.js b/frontend/src/shared-file-view-markdown.js index 2f5f55a7e4..1d8d259fd6 100644 --- a/frontend/src/shared-file-view-markdown.js +++ b/frontend/src/shared-file-view-markdown.js @@ -55,6 +55,7 @@ class FileContent extends React.Component { let index2 = imagePath.indexOf('?'); imagePath = imagePath.substring(index + 5, index2); // change image url + // the image path has been encoded when inserting the image innerNode.data.src = serviceURL + '/view-image-via-share-link/?token=' + sharedToken + '&path=' + imagePath; } return innerNode; diff --git a/seahub/views/file.py b/seahub/views/file.py index 0190820af4..e76ea8de03 100644 --- a/seahub/views/file.py +++ b/seahub/views/file.py @@ -5,6 +5,7 @@ File related views, including view_file, view_history_file, view_trash_file, view_snapshot_file, view_shared_file, etc. """ +from asyncio.proactor_events import constants import os import json import time @@ -1972,8 +1973,9 @@ def view_media_file_via_share_link(request): # Translation ‘(’ ')' image_file_name = image_file_name.replace('(', '\(') image_file_name = image_file_name.replace(')', '\)') + encoded_image_file_name = urllib.parse.quote(image_file_name) - p = re.compile('(%s)/lib/(%s)/file(.*?)%s\?raw=1' % (serviceURL, repo_id, image_file_name)) + p = re.compile('(%s)/lib/(%s)/file(.*?)%s\?raw=1' % (serviceURL, repo_id, encoded_image_file_name)) result = re.search(p, file_content) if not result: return render_error(request, 'Image does not exist')