diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java index d354accf7c..23d626600a 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java @@ -26,6 +26,16 @@ public interface FileRepository { */ String saveFile(byte[] bytes, FileRequest request) throws Exception; + /** + * 保存文件 + * + * @param inputStream + * @param request + * @return + * @throws Exception + */ + String saveFile(InputStream inputStream, FileRequest request) throws Exception; + /** * 删除文件 * diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/LocalFileRepository.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/LocalFileRepository.java index b221350851..c734e1a85b 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/LocalFileRepository.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/LocalFileRepository.java @@ -43,6 +43,13 @@ public class LocalFileRepository implements FileRepository { } } + @Override + public String saveFile(InputStream inputStream, FileRequest request) throws Exception { + File file = new File(getFilePath(request)); + FileUtils.copyInputStreamToFile(inputStream, file); + return file.getPath(); + } + @Override public void delete(FileRequest request) throws Exception { String path = StringUtils.join(getFilePath(request)); diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java index 98820366f5..6c2e75426a 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java @@ -57,6 +57,11 @@ public class MinioRepository implements FileRepository { return request.getFileName(); } + @Override + public String saveFile(InputStream inputStream, FileRequest request) throws Exception { + return null; + } + @Override public void delete(FileRequest request) throws Exception { String filePath = getPath(request); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java index cb57fbae29..fa78f07aab 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java @@ -55,7 +55,14 @@ public class PluginLoadService { * @return */ public String loadPlugin(String fileName) { - return msPluginManager.loadPlugin(Paths.get(MsFileUtils.PLUGIN_DIR + "/" + fileName)); + MsFileUtils.validateFileName(fileName); + String filePath = MsFileUtils.PLUGIN_DIR + "/" + fileName; + File file = new File(filePath); + if (!file.exists()) { + // 文件不存在,则从对象存储重新下载 + downloadPluginFromRepository(fileName); + } + return msPluginManager.loadPlugin(Paths.get(filePath)); } @@ -66,6 +73,7 @@ public class PluginLoadService { * @throws Exception */ public void loadPluginFromRepository(String fileName) { + MsFileUtils.validateFileName(fileName); String filePath = MsFileUtils.PLUGIN_DIR + "/" + fileName; File file = new File(filePath); try { @@ -108,6 +116,21 @@ public class PluginLoadService { } } + /** + * 从对象存储中下载插件 + * + * @param fileName + */ + public void downloadPluginFromRepository(String fileName) { + try { + InputStream inputStream = FileCenter.getDefaultRepository().getFileAsStream(getFileRequest(fileName)); + FileCenter.getRepository(StorageType.LOCAL).saveFile(inputStream, getFileRequest(fileName)); + } catch (Exception e) { + LogUtils.error(e); + throw new MSException("下载插件异常", e); + } + } + private FileRequest getFileRequest(String name) { FileRequest request = new FileRequest(); request.setProjectId(MsFileUtils.PLUGIN_DIR_NAME); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java index 27c6276750..7e2e91a9b1 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java @@ -138,7 +138,7 @@ public class PluginService { // 通知其他节点加载插件 notifiedPluginAdd(id, plugin.getFileName()); - } catch (Exception e) { + } catch (Throwable e) { // 删除插件 pluginLoadService.unloadPlugin(id); pluginLoadService.deletePluginFile(file.getOriginalFilename());