refactor(系统设置): 加载插件时,本地没有则从对象存储重新加载

This commit is contained in:
AgAngle 2023-10-18 11:00:29 +08:00 committed by 刘瑞斌
parent b7cc4fb45e
commit b6951bcef6
5 changed files with 47 additions and 2 deletions

View File

@ -26,6 +26,16 @@ public interface FileRepository {
*/ */
String saveFile(byte[] bytes, FileRequest request) throws Exception; String saveFile(byte[] bytes, FileRequest request) throws Exception;
/**
* 保存文件
*
* @param inputStream
* @param request
* @return
* @throws Exception
*/
String saveFile(InputStream inputStream, FileRequest request) throws Exception;
/** /**
* 删除文件 * 删除文件
* *

View File

@ -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 @Override
public void delete(FileRequest request) throws Exception { public void delete(FileRequest request) throws Exception {
String path = StringUtils.join(getFilePath(request)); String path = StringUtils.join(getFilePath(request));

View File

@ -57,6 +57,11 @@ public class MinioRepository implements FileRepository {
return request.getFileName(); return request.getFileName();
} }
@Override
public String saveFile(InputStream inputStream, FileRequest request) throws Exception {
return null;
}
@Override @Override
public void delete(FileRequest request) throws Exception { public void delete(FileRequest request) throws Exception {
String filePath = getPath(request); String filePath = getPath(request);

View File

@ -55,7 +55,14 @@ public class PluginLoadService {
* @return * @return
*/ */
public String loadPlugin(String fileName) { 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 * @throws Exception
*/ */
public void loadPluginFromRepository(String fileName) { public void loadPluginFromRepository(String fileName) {
MsFileUtils.validateFileName(fileName);
String filePath = MsFileUtils.PLUGIN_DIR + "/" + fileName; String filePath = MsFileUtils.PLUGIN_DIR + "/" + fileName;
File file = new File(filePath); File file = new File(filePath);
try { 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) { private FileRequest getFileRequest(String name) {
FileRequest request = new FileRequest(); FileRequest request = new FileRequest();
request.setProjectId(MsFileUtils.PLUGIN_DIR_NAME); request.setProjectId(MsFileUtils.PLUGIN_DIR_NAME);

View File

@ -138,7 +138,7 @@ public class PluginService {
// 通知其他节点加载插件 // 通知其他节点加载插件
notifiedPluginAdd(id, plugin.getFileName()); notifiedPluginAdd(id, plugin.getFileName());
} catch (Exception e) { } catch (Throwable e) {
// 删除插件 // 删除插件
pluginLoadService.unloadPlugin(id); pluginLoadService.unloadPlugin(id);
pluginLoadService.deletePluginFile(file.getOriginalFilename()); pluginLoadService.deletePluginFile(file.getOriginalFilename());