From 8172ff47344defd22abb7218c31fcd9c635c2984 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 5 Jan 2023 13:31:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AD=98=E5=85=A5MinIO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/service/PluginService.java | 28 +++++++++++++++++-- .../io/metersphere/service/PluginService.java | 24 +++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/PluginService.java b/api-test/backend/src/main/java/io/metersphere/service/PluginService.java index 15cdad6b47..cc05a4f12f 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/PluginService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/PluginService.java @@ -7,10 +7,14 @@ import io.metersphere.base.domain.PluginExample; import io.metersphere.base.domain.PluginWithBLOBs; import io.metersphere.base.mapper.PluginMapper; import io.metersphere.commons.constants.PluginScenario; +import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.metadata.service.FileManagerService; +import io.metersphere.metadata.vo.FileRequest; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,6 +31,8 @@ import java.util.stream.Collectors; public class PluginService { @Resource private PluginMapper pluginMapper; + @Resource + private FileManagerService fileManagerService; private boolean isXpack(Class aClass, Object instance) { try { @@ -37,11 +43,17 @@ public class PluginService { } } - private boolean loadJar(String jarPath) { + private boolean loadJar(String jarPath, String pluginId) { try { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try { File file = new File(jarPath); + if (!file.exists()) { + // 从MinIO下载 + if (!this.downPluginJar(jarPath, pluginId, jarPath)) { + return false; + } + } if (!file.exists()) { return false; } @@ -61,6 +73,18 @@ public class PluginService { return false; } + private boolean downPluginJar(String path, String pluginId, String jarPath) { + FileRequest request = new FileRequest(); + request.setProjectId(StringUtils.join(FileUtils.BODY_FILE_DIR, "/plugin", pluginId)); + request.setFileName(pluginId); + request.setStorage(StorageConstants.MINIO.name()); + byte[] bytes = fileManagerService.downloadFile(request); + if (ArrayUtils.isNotEmpty(bytes)) { + FileUtils.createFile(path, bytes); + } + return new File(jarPath).exists(); + } + public void loadPlugins() { try { PluginExample example = new PluginExample(); @@ -71,7 +95,7 @@ public class PluginService { -> new TreeSet<>(Comparator.comparing(Plugin::getPluginId))), ArrayList::new)); if (CollectionUtils.isNotEmpty(plugins)) { plugins.forEach(item -> { - boolean isLoad = this.loadJar(item.getSourcePath()); + boolean isLoad = this.loadJar(item.getSourcePath(), item.getPluginId()); if (!isLoad) { PluginExample pluginExample = new PluginExample(); pluginExample.createCriteria().andPluginIdEqualTo(item.getPluginId()); diff --git a/system-setting/backend/src/main/java/io/metersphere/service/PluginService.java b/system-setting/backend/src/main/java/io/metersphere/service/PluginService.java index d537cd234a..94619b768b 100644 --- a/system-setting/backend/src/main/java/io/metersphere/service/PluginService.java +++ b/system-setting/backend/src/main/java/io/metersphere/service/PluginService.java @@ -5,14 +5,18 @@ import io.metersphere.base.domain.PluginExample; import io.metersphere.base.domain.PluginWithBLOBs; import io.metersphere.base.mapper.PluginMapper; import io.metersphere.commons.constants.PluginScenario; +import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; +import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.LogUtil; import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.system.SystemReference; +import io.metersphere.metadata.service.FileManagerService; +import io.metersphere.metadata.vo.FileRequest; import io.metersphere.request.PluginDTO; import io.metersphere.request.PluginRequest; import org.apache.commons.collections4.CollectionUtils; @@ -33,6 +37,8 @@ public class PluginService { private PlatformPluginService platformPluginService; @Resource private ApiPluginService apiPluginService; + @Resource + private FileManagerService fileManagerService; public void addPlugin(PluginWithBLOBs plugin) { if (StringUtils.isBlank(plugin.getId())) { @@ -91,6 +97,8 @@ public class PluginService { platformPluginService.delete(id); } else { // 接口传的是 pluginId + FileRequest request = getRequest(id); + fileManagerService.delete(request); apiPluginService.delete(id); } } @@ -119,9 +127,23 @@ public class PluginService { } else { List plugins = apiPluginService.addApiPlugin(file); plugins.forEach(this::addPlugin); + // 存入MinIO + if (CollectionUtils.isNotEmpty(plugins)) { + String pluginId = plugins.get(0).getPluginId(); + FileRequest request = getRequest(pluginId); + fileManagerService.upload(file, request); + } } } + private FileRequest getRequest(String pluginId) { + FileRequest request = new FileRequest(); + request.setProjectId(StringUtils.join(FileUtils.BODY_FILE_DIR, "/plugin", pluginId)); + request.setFileName(pluginId); + request.setStorage(StorageConstants.MINIO.name()); + return request; + } + public void checkPluginExist(MultipartFile file) { String filename = file.getOriginalFilename(); PluginExample example = new PluginExample(); @@ -137,7 +159,7 @@ public class PluginService { example.createCriteria().andPluginIdEqualTo(id); List plugins = pluginMapper.selectByExampleWithBLOBs(example); if (CollectionUtils.isNotEmpty(plugins)) { - Plugin plugin = plugins.get(0); + Plugin plugin = plugins.get(0); List columns = ReflexObjectUtil.getColumns(plugin, SystemReference.pluginColumns); OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(plugin.getId()), null, plugin.getSourceName(), plugin.getCreateUserId(), columns); return JSON.toJSONString(details);