fix: 禅道提交缺陷图片无法显示

--bug=1010047 --user=陈建星 【测试跟踪】github#10174,从MS提交缺陷至禅道时,上传的图片未能在禅道上正确预览显示,请修复 https://www.tapd.cn/55049933/s/1100725
This commit is contained in:
chenjianxing 2022-01-28 18:36:37 +08:00 committed by zhangdahai112
parent 41675def73
commit c30ec9a5c4
2 changed files with 27 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.service;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.controller.request.MdUploadRequest; import io.metersphere.controller.request.MdUploadRequest;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
@ -14,6 +15,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Date; import java.util.Date;
@ -30,12 +32,7 @@ public class ResourceService {
MSException.throwException(Translator.get("invalid_parameter")); MSException.throwException(Translator.get("invalid_parameter"));
File file = new File(FileUtils.MD_IMAGE_DIR + "/" + name); File file = new File(FileUtils.MD_IMAGE_DIR + "/" + name);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
String fileName = ""; String fileName = encodeFileName(file.getName());
try {
fileName = URLEncoder.encode(file.getName(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + fileName); headers.add("Content-Disposition", "attachment; filename=" + fileName);
headers.add("Pragma", "no-cache"); headers.add("Pragma", "no-cache");
@ -49,6 +46,24 @@ public class ResourceService {
.body(new FileSystemResource(file)); .body(new FileSystemResource(file));
} }
public String encodeFileName(String fileName) {
try {
return URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
LogUtil.error(e);
return fileName;
}
}
public String decodeFileName(String fileName) {
try {
return URLDecoder.decode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
LogUtil.error(e);
return fileName;
}
}
public void mdDelete(String fileName) { public void mdDelete(String fileName) {
FileUtils.deleteFile(FileUtils.MD_IMAGE_DIR + "/" + fileName); FileUtils.deleteFile(FileUtils.MD_IMAGE_DIR + "/" + fileName);
} }

View File

@ -375,12 +375,14 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
} }
private String ms2ZentaoDescription(String msDescription) { private String ms2ZentaoDescription(String msDescription) {
String imgUrlRegex = "!\\[.*?]\\(/resource/md/get/(.*?\\..*?)\\)"; String imgUrlRegex = "!\\[.*?]\\(/resource/md/get(.*?\\..*?)\\)";
String zentaoSteps = msDescription.replaceAll(imgUrlRegex, zentaoClient.requestUrl.getReplaceImgUrl()); String zentaoSteps = msDescription.replaceAll(imgUrlRegex, zentaoClient.requestUrl.getReplaceImgUrl());
Matcher matcher = zentaoClient.requestUrl.getImgPattern().matcher(zentaoSteps); Matcher matcher = zentaoClient.requestUrl.getImgPattern().matcher(zentaoSteps);
while (matcher.find()) { while (matcher.find()) {
// get file name // get file name
String fileName = matcher.group(1); String originSubUrl = matcher.group(1);
String fileName = originSubUrl.substring(10);
fileName = resourceService.decodeFileName(fileName);
// get file // get file
ResponseEntity<FileSystemResource> mdImage = resourceService.getMdImage(fileName); ResponseEntity<FileSystemResource> mdImage = resourceService.getMdImage(fileName);
// upload zentao // upload zentao
@ -388,10 +390,9 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
// todo delete local file // todo delete local file
int index = fileName.lastIndexOf("."); int index = fileName.lastIndexOf(".");
if (index != -1) { if (index != -1) {
fileName = fileName.substring(0, index); // replace id
zentaoSteps = zentaoSteps.replaceAll(Pattern.quote(originSubUrl), id);
} }
// replace id
zentaoSteps = zentaoSteps.replaceAll(Pattern.quote(fileName), id);
} }
// image link // image link
String netImgRegex = "!\\[(.*?)]\\((http.*?)\\)"; String netImgRegex = "!\\[(.*?)]\\((http.*?)\\)";