From acd80dd2f5385e3d9adcb4d522286d3e23386c39 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 8 Feb 2023 11:30:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):?= =?UTF-8?q?=20node=E8=B0=83=E7=94=A8=E4=B8=BB=E6=9C=8D=E5=8A=A1=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=8F=92=E4=BB=B6=E6=94=B9=E5=8F=98=E4=BC=A0=E5=8F=82?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ApiJMeterFileService.java | 37 +++++++++---------- .../metersphere/commons/utils/FileUtils.java | 2 + 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java index 3e95f193fb..05844197e4 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java @@ -8,8 +8,10 @@ import io.metersphere.api.exec.api.ApiCaseSerialService; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper; import io.metersphere.base.mapper.ApiScenarioMapper; +import io.metersphere.base.mapper.PluginMapper; import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper; import io.metersphere.commons.constants.ApiRunMode; +import io.metersphere.commons.constants.PluginScenario; import io.metersphere.commons.utils.*; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.environment.service.BaseEnvGroupProjectService; @@ -45,9 +47,9 @@ public class ApiJMeterFileService { @Resource private TestPlanApiScenarioMapper testPlanApiScenarioMapper; @Resource - private PluginService pluginService; - @Resource private FileMetadataService fileMetadataService; + @Resource + private PluginMapper pluginMapper; // 接口测试 用例/接口 private static final List CASE_MODES = new ArrayList<>() {{ @@ -159,24 +161,17 @@ public class ApiJMeterFileService { return listBytesToZip(files); } - public byte[] downloadPluginJar(List jarPaths) { + public byte[] downloadPluginJar(List pluginIds) { Map files = new HashMap<>(); - // 获取JAR - Map jarFiles = new LinkedHashMap<>(); - jarPaths.forEach(item -> { - File file = new File(item); - if (file.exists() && !file.isDirectory()) { - byte[] fileByte = FileUtils.fileToByte(file); - if (ArrayUtils.isNotEmpty(fileByte)) { - jarFiles.put(file.getName(), fileByte); + if (!CollectionUtils.isNotEmpty(pluginIds)) { + // 获取JAR + Map jarFiles = this.getPlugJar(pluginIds); + if (MapUtils.isNotEmpty(jarFiles)) { + for (String k : jarFiles.keySet()) { + byte[] v = jarFiles.get(k); + files.put(k, v); } } - }); - if (MapUtils.isNotEmpty(jarFiles)) { - for (String k : jarFiles.keySet()) { - byte[] v = jarFiles.get(k); - files.put(k, v); - } } return listBytesToZip(files); } @@ -204,9 +199,11 @@ public class ApiJMeterFileService { } } - public Map getPlugJar() { + public Map getPlugJar(List pluginIds) { Map jarFiles = new LinkedHashMap<>(); - List plugins = pluginService.list(); + PluginExample example = new PluginExample(); + example.createCriteria().andPluginIdIn(pluginIds).andScenarioNotEqualTo(PluginScenario.platform.name()); + List plugins = pluginMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(plugins)) { plugins = plugins.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Plugin::getPluginId))), ArrayList::new)); @@ -303,7 +300,7 @@ public class ApiJMeterFileService { ApiFileUtil.downloadFile(bodyFile.getId(), bodyFile.getName()); file = new File(bodyFile.getName()); } - if (file != null && file.exists()) { + if (file != null && file.exists() && StringUtils.startsWith(file.getPath(), FileUtils.ROOT_DIR)) { byte[] fileByte = FileUtils.fileToByte(file); if (fileByte != null) { files.put(file.getAbsolutePath(), fileByte); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java index daa8d17cc2..7dd3140b66 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java @@ -26,6 +26,8 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileUtils { + + public static final String ROOT_DIR = "/opt/metersphere/"; public static final String BODY_FILE_DIR = "/opt/metersphere/data/body"; public static final String MD_IMAGE_DIR = "/opt/metersphere/data/image/markdown"; public static final String UI_IMAGE_DIR = "/opt/metersphere/data/image/ui/screenshots";