From 9c9bf320e9dca678cb1c791e05776a4a9f225799 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 14 Jun 2022 15:21:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20Ji?= =?UTF-8?q?ra=E5=9B=BE=E7=89=87=E4=B8=8D=E8=83=BD=E5=8C=BF=E5=90=8D?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013769 --user=陈建星 【测试跟踪】github#13768,项目MS创建缺陷中含有图片-点击同步缺陷到JIRA-MS缺陷中的图片消失-JIRA中的图片还在 https://www.tapd.cn/55049933/s/1181267 --- .../controller/ResourceController.java | 4 ++-- .../metersphere/service/ResourceService.java | 14 ++++++++++++- .../track/issue/AbstractIssuePlatform.java | 20 +++++++++++++------ .../track/issue/IssuesPlatform.java | 8 ++++++++ .../metersphere/track/issue/JiraPlatform.java | 11 ++++++++++ .../issue/client/JiraAbstractClient.java | 5 +++++ 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/io/metersphere/controller/ResourceController.java b/backend/src/main/java/io/metersphere/controller/ResourceController.java index d32480b31f..748936c68e 100644 --- a/backend/src/main/java/io/metersphere/controller/ResourceController.java +++ b/backend/src/main/java/io/metersphere/controller/ResourceController.java @@ -26,8 +26,8 @@ public class ResourceController { } @GetMapping(value = "/md/get/url") - public ResponseEntity getFileByUrl(@RequestParam ("url") String url) { - return resourceService.getMdImageByUrl(url); + public ResponseEntity getFileByUrl(@RequestParam ("url") String url, @RequestParam (value = "platform", required = false) String platform) { + return resourceService.getMdImageByUrl(url, platform); } @GetMapping(value = "/ui/get") diff --git a/backend/src/main/java/io/metersphere/service/ResourceService.java b/backend/src/main/java/io/metersphere/service/ResourceService.java index 286e0842dd..a8d18452d6 100644 --- a/backend/src/main/java/io/metersphere/service/ResourceService.java +++ b/backend/src/main/java/io/metersphere/service/ResourceService.java @@ -3,8 +3,12 @@ package io.metersphere.service; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.SessionUtils; import io.metersphere.controller.request.MdUploadRequest; import io.metersphere.i18n.Translator; +import io.metersphere.track.issue.IssueFactory; +import io.metersphere.track.request.testcase.IssuesRequest; +import org.apache.commons.lang3.StringUtils; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -96,12 +100,20 @@ public class ResourceService { * 如果当前访问地址是 https,直接访问 http 的图片资源 * 由于浏览器的安全机制,http 会被转成 https * @param url + * @param platform * @return */ - public ResponseEntity getMdImageByUrl(String url) { + public ResponseEntity getMdImageByUrl(String url, String platform) { if (url.contains("md/get/url")) { MSException.throwException(Translator.get("invalid_parameter")); } + if (StringUtils.isNotBlank(platform)) { + IssuesRequest issuesRequest = new IssuesRequest(); + issuesRequest.setProjectId(SessionUtils.getCurrentProjectId()); + issuesRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId()); + return IssueFactory.createPlatform(platform, issuesRequest) + .proxyForGet(url, byte[].class); + } return restTemplate.exchange(url, HttpMethod.GET, null, byte[].class); } } diff --git a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java index d5058f0333..206e086d58 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -34,6 +34,7 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.safety.Whitelist; import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.util.CollectionUtils; import org.springframework.util.MultiValueMap; @@ -372,12 +373,14 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { while (matcher.find()) { try { String path = matcher.group(2); - if (path.contains("/resource/md/get/")) { // 兼容旧数据 - String name = path.substring(path.indexOf("/resource/md/get/") + 17); - files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + name)); - } else if (path.contains("/resource/md/get")) { // 新数据走这里 - String name = path.substring(path.indexOf("/resource/md/get") + 26); - files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + URLDecoder.decode(name, "UTF-8"))); + if (!path.contains("/resource/md/get/url")) { + if (path.contains("/resource/md/get/")) { // 兼容旧数据 + String name = path.substring(path.indexOf("/resource/md/get/") + 17); + files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + name)); + } else if (path.contains("/resource/md/get")) { // 新数据走这里 + String name = path.substring(path.indexOf("/resource/md/get") + 26); + files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + URLDecoder.decode(name, "UTF-8"))); + } } } catch (Exception e) { LogUtil.error(e.getMessage(), e); @@ -599,4 +602,9 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { return null; } + @Override + public ResponseEntity proxyForGet(String url, Class responseEntityClazz) { + return null; + } + } diff --git a/backend/src/main/java/io/metersphere/track/issue/IssuesPlatform.java b/backend/src/main/java/io/metersphere/track/issue/IssuesPlatform.java index 94bf28b5b3..57b1d84dd7 100644 --- a/backend/src/main/java/io/metersphere/track/issue/IssuesPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/IssuesPlatform.java @@ -10,6 +10,7 @@ import io.metersphere.track.issue.domain.PlatformUser; import io.metersphere.track.request.testcase.EditTestCaseRequest; import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesUpdateRequest; +import org.springframework.http.ResponseEntity; import java.util.List; @@ -105,4 +106,11 @@ public interface IssuesPlatform { * @param type add or edit */ void updateDemandHyperLink(EditTestCaseRequest request, Project project, String type); + + /** + * Get请求的代理 + * @param url + * @return + */ + ResponseEntity proxyForGet(String url, Class responseEntityClazz); } diff --git a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java index 9d0d782ab7..295c6bb944 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -28,10 +28,13 @@ import io.metersphere.track.service.IssuesService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.HttpClientErrorException; import java.io.File; import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; @@ -101,6 +104,8 @@ public class JiraPlatform extends AbstractIssuePlatform { JSONObject attachment = attachments.getJSONObject(i); String filename = attachment.getString("filename"); String content = attachment.getString("content"); + content = "/resource/md/get/url?platform=Jira&url=" + URLEncoder.encode(content, StandardCharsets.UTF_8); + if (StringUtils.contains(attachment.getString("mimeType"), "image")) { String contentUrl = "![" + filename + "](" + content + ")"; fileContentMap.put(filename, contentUrl); @@ -124,6 +129,8 @@ public class JiraPlatform extends AbstractIssuePlatform { // 解析标签内容 String name = getHyperLinkPathForImg("\\!\\[(.*?)\\]", StringEscapeUtils.unescapeJava(splitStr)); String path = getHyperLinkPathForImg("\\|(.*?)\\)", splitStr); + path = "/resource/md/get/url?platform=Jira&url=" + URLEncoder.encode(path, StandardCharsets.UTF_8); + // 解析标签内容为图片超链接格式,进行替换 description = description.replace(splitStr, "\n\n![" + name + "](" + path + ")"); } @@ -807,4 +814,8 @@ public class JiraPlatform extends AbstractIssuePlatform { } return false; } + + public ResponseEntity proxyForGet(String url, Class responseEntityClazz) { + return jiraClientV2.proxyForGet(url, responseEntityClazz); + } } diff --git a/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java index 6ee996cef7..33c9da18be 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java @@ -273,4 +273,9 @@ public abstract class JiraAbstractClient extends BaseClient { MSException.throwException(e.getMessage()); } } + + public ResponseEntity proxyForGet(String url, Class responseEntityClazz) { + LogUtil.info("jira proxyForGet: " + url); + return restTemplate.exchange(url, HttpMethod.GET, getAuthHttpEntity(), responseEntityClazz); + } }