mirror of
https://github.com/dataease/dataease.git
synced 2026-05-21 21:15:12 +08:00
fix: 插件上传增加版本验证
This commit is contained in:
@@ -35,5 +35,7 @@ public class MyPlugin implements Serializable {
|
||||
|
||||
private String icon;
|
||||
|
||||
private String require = "1.9.0";
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -195,4 +195,16 @@ public class CodingUtil {
|
||||
}
|
||||
return shortBuffer.toString();
|
||||
}
|
||||
|
||||
public static Integer string2Integer(String str) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (StringUtils.isBlank(str)) return null;
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
if (Character.isDigit(c)) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.length() > 0 ? Integer.parseInt(sb.toString()) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
package io.dataease.service.sys;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.google.gson.Gson;
|
||||
import io.dataease.base.domain.MyPlugin;
|
||||
import io.dataease.base.mapper.MyPluginMapper;
|
||||
import io.dataease.base.mapper.ext.ExtSysPluginMapper;
|
||||
import io.dataease.base.mapper.ext.query.GridExample;
|
||||
import io.dataease.commons.constants.AuthConstants;
|
||||
import io.dataease.commons.exception.DEException;
|
||||
import io.dataease.commons.utils.CodingUtil;
|
||||
import io.dataease.commons.utils.DeFileUtils;
|
||||
import io.dataease.commons.utils.LogUtil;
|
||||
import io.dataease.commons.utils.ZipUtils;
|
||||
import io.dataease.controller.sys.base.BaseGridRequest;
|
||||
import io.dataease.listener.util.CacheUtils;
|
||||
import io.dataease.plugins.config.LoadjarUtil;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -22,8 +27,10 @@ import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Service
|
||||
@@ -43,6 +50,9 @@ public class PluginService {
|
||||
@Autowired
|
||||
private LoadjarUtil loadjarUtil;
|
||||
|
||||
@Value("${version}")
|
||||
private String version;
|
||||
|
||||
|
||||
public List<MyPlugin> query(BaseGridRequest request) {
|
||||
GridExample gridExample = request.convertExample();
|
||||
@@ -66,7 +76,9 @@ public class PluginService {
|
||||
DeFileUtils.deleteFile(pluginDir + "temp/");
|
||||
DeFileUtils.deleteFile(folder);
|
||||
// 需要删除文件
|
||||
e.printStackTrace();
|
||||
// e.printStackTrace();
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
DEException.throwException(e);
|
||||
}
|
||||
//3.解析plugin.json 失败则 直接返回错误 删除文件
|
||||
File folderFile = new File(folder);
|
||||
@@ -74,15 +86,31 @@ public class PluginService {
|
||||
if (ArrayUtils.isEmpty(jsonFiles)) {
|
||||
DeFileUtils.deleteFile(pluginDir + "temp/");
|
||||
DeFileUtils.deleteFile(folder);
|
||||
throw new RuntimeException("缺少插件描述文件");
|
||||
String msg = "缺少插件描述文件【plugin.json】";
|
||||
LogUtil.error(msg);
|
||||
DEException.throwException(new RuntimeException(msg));
|
||||
}
|
||||
MyPlugin myPlugin = formatJsonFile(jsonFiles[0]);
|
||||
|
||||
if (!versionMatch(myPlugin.getRequire())) {
|
||||
String msg = "当前插件要求系统版本最低为:" + myPlugin.getRequire();
|
||||
LogUtil.error(msg);
|
||||
DEException.throwException(new RuntimeException(msg));
|
||||
}
|
||||
//4.加载jar包 失败则 直接返回错误 删除文件
|
||||
File[] jarFiles = folderFile.listFiles(this::isPluginJar);
|
||||
if (ArrayUtils.isEmpty(jarFiles)) {
|
||||
DeFileUtils.deleteFile(pluginDir + "temp/");
|
||||
DeFileUtils.deleteFile(folder);
|
||||
throw new RuntimeException("缺少插件jar文件");
|
||||
String msg = "缺少插件jar文件";
|
||||
LogUtil.error(msg);
|
||||
DEException.throwException(new RuntimeException(msg));
|
||||
}
|
||||
|
||||
if (pluginExist(myPlugin)) {
|
||||
String msg = "插件【"+myPlugin.getName()+"】已存在,请先卸载";
|
||||
LogUtil.error(msg);
|
||||
DEException.throwException(new RuntimeException(msg));
|
||||
}
|
||||
String targetDir = null;
|
||||
try {
|
||||
@@ -100,7 +128,8 @@ public class PluginService {
|
||||
if (StringUtils.isNotEmpty(targetDir)) {
|
||||
DeFileUtils.deleteFile(targetDir);
|
||||
}
|
||||
e.printStackTrace();
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
DEException.throwException(e);
|
||||
} finally {
|
||||
DeFileUtils.deleteFile(pluginDir + "temp/");
|
||||
DeFileUtils.deleteFile(folder);
|
||||
@@ -122,6 +151,19 @@ public class PluginService {
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测插件是否已存在
|
||||
* @param myPlugin
|
||||
* @return
|
||||
*/
|
||||
public boolean pluginExist(MyPlugin myPlugin) {
|
||||
GridExample gridExample = new GridExample();
|
||||
List<MyPlugin> plugins = extSysPluginMapper.query(gridExample);
|
||||
return plugins.stream().anyMatch(plugin -> {
|
||||
return StringUtils.equals(myPlugin.getName(), plugin.getName()) || StringUtils.equals(myPlugin.getModuleName(), plugin.getModuleName());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载插件
|
||||
*
|
||||
@@ -129,6 +171,13 @@ public class PluginService {
|
||||
* @return
|
||||
*/
|
||||
public Boolean uninstall(Long pluginId) {
|
||||
MyPlugin myPlugin = myPluginMapper.selectByPrimaryKey(pluginId);
|
||||
if (ObjectUtils.isEmpty(myPlugin)) {
|
||||
String msg = "当前插件不存在";
|
||||
LogUtil.error(msg);
|
||||
DEException.throwException(new RuntimeException(msg));
|
||||
}
|
||||
deleteJarFile(myPlugin);
|
||||
CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME);
|
||||
CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME);
|
||||
CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME);
|
||||
@@ -136,6 +185,15 @@ public class PluginService {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void deleteJarFile(MyPlugin plugin) {
|
||||
String version = plugin.getVersion();
|
||||
String moduleName = plugin.getModuleName();
|
||||
String fileName = moduleName + "-" + version + ".jar";
|
||||
String path = pluginDir + plugin.getStore() + "/" + fileName;
|
||||
File jarFile = new File(path);
|
||||
FileUtil.del(jarFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 改变插件状态
|
||||
*
|
||||
@@ -195,4 +253,16 @@ public class PluginService {
|
||||
public Map<String, Object> remoteInstall(Map<String, Object> params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean versionMatch(String pluginVersion) {
|
||||
List<Integer> versionLists = Arrays.stream(version.split(".")).map(CodingUtil::string2Integer).collect(Collectors.toList());
|
||||
List<Integer> requireVersionLists = Arrays.stream(pluginVersion.split(".")).map(CodingUtil::string2Integer).collect(Collectors.toList());
|
||||
int maxSize = Math.max(versionLists.size(), requireVersionLists.size());
|
||||
for (int i = 0; i < maxSize; i++) {
|
||||
Integer currentV = versionLists.size() == i ? 0 : versionLists.get(i);
|
||||
Integer requireV = requireVersionLists.size() == i ? 0 : requireVersionLists.get(i);
|
||||
if (requireV > currentV) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user