From ea54bfe739b406758934bba660393e8439c2b56b Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 26 May 2022 17:56:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A6=85=E9=81=93=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013432 --user=陈建星 [缺陷管理] github#13685通过metersphere平台创建缺陷,同步至禅道缺陷之后,meterphere点击【同步缺陷】,禅道缺陷同步失败 https://www.tapd.cn/55049933/s/1165437 --- .../controller/ResourceController.java | 5 +++++ .../metersphere/service/ResourceService.java | 20 +++++++++++++++++++ .../track/issue/ZentaoPlatform.java | 3 +++ 3 files changed, 28 insertions(+) diff --git a/backend/src/main/java/io/metersphere/controller/ResourceController.java b/backend/src/main/java/io/metersphere/controller/ResourceController.java index 102a0b17db..59fe59f6ba 100644 --- a/backend/src/main/java/io/metersphere/controller/ResourceController.java +++ b/backend/src/main/java/io/metersphere/controller/ResourceController.java @@ -25,6 +25,11 @@ public class ResourceController { return resourceService.getMdImage(fileName); } + @GetMapping(value = "/md/get/url") + public ResponseEntity getFileByUrl(@RequestParam ("url") String url) { + return resourceService.getMdImageByUrl(url); + } + @GetMapping(value = "/ui/get") public ResponseEntity getUiFile(@RequestParam ("fileName") String fileName) { return resourceService.getUiResultImage(fileName); diff --git a/backend/src/main/java/io/metersphere/service/ResourceService.java b/backend/src/main/java/io/metersphere/service/ResourceService.java index 4abb6df477..1990fd57f2 100644 --- a/backend/src/main/java/io/metersphere/service/ResourceService.java +++ b/backend/src/main/java/io/metersphere/service/ResourceService.java @@ -7,12 +7,15 @@ import io.metersphere.controller.request.MdUploadRequest; import io.metersphere.i18n.Translator; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; @@ -23,6 +26,9 @@ import java.util.Date; @Transactional(rollbackFor = Exception.class) public class ResourceService { + @Resource + private RestTemplate restTemplate; + public void mdUpload(MdUploadRequest request, MultipartFile file) { FileUtils.uploadFile(file, FileUtils.MD_IMAGE_DIR, request.getId() + "_" + request.getFileName()); } @@ -82,4 +88,18 @@ public class ResourceService { } FileUtils.deleteFile(FileUtils.MD_IMAGE_DIR + "/" + fileName); } + + /** + * http 代理 + * 如果当前访问地址是 https,直接访问 http 的图片资源 + * 由于浏览器的安全机制,http 会被转成 https + * @param url + * @return + */ + public ResponseEntity getMdImageByUrl(String url) { + if (url.contains("md/get/url")) { + MSException.throwException(Translator.get("invalid_parameter")); + } + return restTemplate.exchange(url, HttpMethod.GET, null, byte[].class); + } } diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java index 4e68a66cb6..7d6ddc6565 100644 --- a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java @@ -35,6 +35,8 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -460,6 +462,7 @@ public class ZentaoPlatform extends AbstractIssuePlatform { name = name.replaceAll("&", "&"); path = zentaoClient.getBaseUrl() + path.replaceAll("&", "&"); } + path = "/resource/md/get/url?url=" + URLEncoder.encode(path, StandardCharsets.UTF_8); // 图片与描述信息之间需换行,否则无法预览图片 result = "\n\n![" + name + "](" + path + ")"; }