文件上传支持插件获取模式
This commit is contained in:
@@ -2,7 +2,6 @@ package com.cool.core.file;
|
||||
|
||||
import static com.cool.core.plugin.consts.PluginConsts.uploadHook;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.cool.core.exception.CoolPreconditions;
|
||||
import com.cool.core.file.strategy.FileUploadStrategy;
|
||||
@@ -25,16 +24,11 @@ public class FileUploadStrategyFactory {
|
||||
|
||||
final private CoolPluginService coolPluginService;
|
||||
|
||||
public FileUploadStrategy getStrategy() {
|
||||
PluginInfoEntity pluginInfoEntity = coolPluginService.getPluginInfoEntityByHook(uploadHook);
|
||||
return getStrategy(pluginInfoEntity);
|
||||
}
|
||||
|
||||
private FileUploadStrategy getStrategy(PluginInfoEntity pluginInfoEntity) {
|
||||
if (ObjUtil.isEmpty(pluginInfoEntity)) {
|
||||
return applicationContext.getBean("localFileUploadStrategy", FileUploadStrategy.class);
|
||||
}
|
||||
return applicationContext.getBean("ossFileUploadStrategy", FileUploadStrategy.class);
|
||||
return applicationContext.getBean("cloudFileUploadStrategy", FileUploadStrategy.class);
|
||||
}
|
||||
|
||||
public Object upload(MultipartFile[] files, HttpServletRequest request) {
|
||||
@@ -49,9 +43,11 @@ public class FileUploadStrategyFactory {
|
||||
}
|
||||
|
||||
public Object getMode() {
|
||||
FileUploadStrategy strategy = getStrategy();
|
||||
UpLoadModeType upLoadModeType = strategy.getMode();
|
||||
return Dict.create().set("mode", upLoadModeType.getMode().value())
|
||||
.set("type", upLoadModeType.getType());
|
||||
PluginInfoEntity pluginInfoEntity = coolPluginService.getPluginInfoEntityByHook(uploadHook);
|
||||
String key = null;
|
||||
if (ObjUtil.isNotEmpty(pluginInfoEntity)) {
|
||||
key = pluginInfoEntity.getKey();
|
||||
}
|
||||
return getStrategy(pluginInfoEntity).getMode(key);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.cool.core.file.strategy;
|
||||
|
||||
import com.cool.core.config.FileModeEnum;
|
||||
import com.cool.core.file.UpLoadModeType;
|
||||
import com.cool.core.util.CoolPluginInvokers;
|
||||
import com.cool.modules.plugin.entity.PluginInfoEntity;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component("ossFileUploadStrategy")
|
||||
public class OssFileUploadStrategy implements FileUploadStrategy {
|
||||
@Component("cloudFileUploadStrategy")
|
||||
public class CloudFileUploadStrategy implements FileUploadStrategy {
|
||||
|
||||
@Override
|
||||
public Object upload(MultipartFile[] files, HttpServletRequest request, PluginInfoEntity pluginInfoEntity)
|
||||
@@ -20,7 +20,14 @@ public class OssFileUploadStrategy implements FileUploadStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpLoadModeType getMode() {
|
||||
return new UpLoadModeType(FileModeEnum.CLOUD);
|
||||
public Map<String, String> getMode(String key) {
|
||||
try{
|
||||
Object mode = CoolPluginInvokers.invoke(key, "getMode");
|
||||
if (Objects.nonNull(mode)) {
|
||||
return (Map) mode;
|
||||
}
|
||||
} catch (Exception ignore){}
|
||||
return Map.of("mode", FileModeEnum.CLOUD.value(),
|
||||
"type", FileModeEnum.CLOUD.type());
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
package com.cool.core.file.strategy;
|
||||
|
||||
import com.cool.core.file.UpLoadModeType;
|
||||
import com.cool.modules.plugin.entity.PluginInfoEntity;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface FileUploadStrategy {
|
||||
|
||||
@@ -22,7 +21,7 @@ public interface FileUploadStrategy {
|
||||
*
|
||||
* @return 上传模式
|
||||
*/
|
||||
UpLoadModeType getMode();
|
||||
Map<String, String> getMode(String key);
|
||||
|
||||
default boolean isAbsolutePath(String pathStr) {
|
||||
Path path = Paths.get(pathStr);
|
||||
|
||||
@@ -8,13 +8,13 @@ import com.cool.core.config.FileModeEnum;
|
||||
import com.cool.core.config.LocalFileProperties;
|
||||
import com.cool.core.exception.CoolException;
|
||||
import com.cool.core.exception.CoolPreconditions;
|
||||
import com.cool.core.file.UpLoadModeType;
|
||||
import com.cool.modules.plugin.entity.PluginInfoEntity;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -70,7 +70,8 @@ public class LocalFileUploadStrategy implements FileUploadStrategy {
|
||||
*
|
||||
* @return 上传模式
|
||||
*/
|
||||
public UpLoadModeType getMode() {
|
||||
return new UpLoadModeType(FileModeEnum.LOCAL);
|
||||
public Map<String, String> getMode(String key) {
|
||||
return Map.of("mode", FileModeEnum.LOCAL.value(),
|
||||
"type", FileModeEnum.LOCAL.type());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,9 +102,12 @@ public class CoolPluginService {
|
||||
// 把 ApplicationContext 对象传递打插件类中,使其在插件中也能正常使用spring bean对象
|
||||
CoolPluginInvokers.setApplicationContext(pluginJson.getKey());
|
||||
} catch (PersistenceException persistenceException) {
|
||||
if (persistenceException.getMessage().contains("Duplicate entry")) {
|
||||
// 唯一键冲突
|
||||
CoolPreconditions.returnData(
|
||||
new CoolPreconditions.ReturnData(1, "插件已存在,继续安装将覆盖"));
|
||||
}
|
||||
CoolPreconditions.alwaysThrow(persistenceException.getMessage());
|
||||
} catch (CoolException e) {
|
||||
FileUtil.del(jarFile);
|
||||
throw e;
|
||||
@@ -188,6 +191,8 @@ public class CoolPluginService {
|
||||
// 重新加载配置不更新
|
||||
pluginInfo.setConfig(one.getConfig());
|
||||
pluginInfo.getPluginJson().setConfig(one.getConfig());
|
||||
// 设置插件配置
|
||||
CoolPluginInvokers.setPluginJson(pluginInfo.getKey(), pluginInfo);
|
||||
CopyOptions options = CopyOptions.create().setIgnoreNullValue(true);
|
||||
// 忽略无变更,无需更新的字段
|
||||
ignoreNoChange(pluginInfo, one);
|
||||
|
||||
@@ -127,9 +127,9 @@ public class DynamicJarLoaderService {
|
||||
DynamicJarClassLoader dynamicJarClassLoader,
|
||||
boolean force) {
|
||||
if (!force && pluginMap.containsKey(key)) {
|
||||
dynamicJarClassLoader.unload();
|
||||
CoolPreconditions.returnData(
|
||||
new CoolPreconditions.ReturnData(1, "插件已存在,继续安装将覆盖"));
|
||||
// dynamicJarClassLoader.unload();
|
||||
// CoolPreconditions.returnData(
|
||||
// new CoolPreconditions.ReturnData(1, "插件已存在,继续安装将覆盖"));
|
||||
}
|
||||
if (ObjUtil.isNotEmpty(key)) {
|
||||
pluginMap.remove(key);
|
||||
|
||||
Reference in New Issue
Block a user