fix: 默认脚本保存改为CachedStorageService(七牛云+本地缓存)
This commit is contained in:
@@ -24,8 +24,17 @@ public class GroovyScriptEngine {
|
|||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
private static final GroovyClassLoader classLoader = new GroovyClassLoader(
|
private static final GroovyClassLoader classLoader = new GroovyClassLoader(
|
||||||
Thread.currentThread().getContextClassLoader());
|
Thread.currentThread().getContextClassLoader());
|
||||||
/** 类型 → 默认脚本 */
|
/** 默认脚本存储路径前缀 */
|
||||||
private static final Map<String, String> DEFAULT_SCRIPTS = new ConcurrentHashMap<>();
|
private static final String DEFAULT_SCRIPTS_PATH = "scripts/default/";
|
||||||
|
/** 类型 → 默认脚本缓存 */
|
||||||
|
private static final Map<String, String> DEFAULT_SCRIPTS_CACHE = new ConcurrentHashMap<>();
|
||||||
|
/** 存储服务(由 Spring 注入) */
|
||||||
|
private static com.par.core.storage.CachedStorageService storageService;
|
||||||
|
|
||||||
|
// Spring 注入
|
||||||
|
public GroovyScriptEngine(com.par.core.storage.CachedStorageService storageService) {
|
||||||
|
GroovyScriptEngine.storageService = storageService;
|
||||||
|
}
|
||||||
/** 脚本前缀:自动导入必要包 + PluginContext 桩 */
|
/** 脚本前缀:自动导入必要包 + PluginContext 桩 */
|
||||||
private static final String SCRIPT_PREFIX =
|
private static final String SCRIPT_PREFIX =
|
||||||
"import groovy.json.*\n" +
|
"import groovy.json.*\n" +
|
||||||
@@ -44,29 +53,42 @@ public class GroovyScriptEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存站点类型默认脚本到 classpath
|
* 保存站点类型默认脚本到七牛云+本地缓存
|
||||||
*/
|
*/
|
||||||
public static void saveDefaultScript(String siteType, String script) {
|
public static void saveDefaultScript(String siteType, String script) {
|
||||||
if (siteType == null || script == null) return;
|
if (siteType == null || script == null) return;
|
||||||
String key = siteType.toLowerCase();
|
String key = siteType.toLowerCase();
|
||||||
DEFAULT_SCRIPTS.put(key, script);
|
DEFAULT_SCRIPTS_CACHE.put(key, script);
|
||||||
try {
|
if (storageService != null) {
|
||||||
var path = java.nio.file.Paths.get(
|
storageService.store(DEFAULT_SCRIPTS_PATH + key + ".groovy", script);
|
||||||
GroovyScriptEngine.class.getClassLoader().getResource("scripts/default-" + key + ".groovy").toURI());
|
} else {
|
||||||
java.nio.file.Files.writeString(path, script, java.nio.charset.StandardCharsets.UTF_8);
|
// 回退到 classpath 写入(开发环境)
|
||||||
log.info("Default script saved: default-{}.groovy", key);
|
try {
|
||||||
} catch (Exception e) {
|
var path = java.nio.file.Paths.get(
|
||||||
log.error("Failed to save default script: {}", key, e);
|
GroovyScriptEngine.class.getClassLoader().getResource("scripts/default-" + key + ".groovy").toURI());
|
||||||
|
java.nio.file.Files.writeString(path, script, java.nio.charset.StandardCharsets.UTF_8);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Fallback save failed: {}", e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
log.info("Default script saved: default-{}.groovy", key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从 classpath 加载默认脚本文件
|
* 加载站点类型默认脚本(优先七牛云/本地缓存,回退到 classpath)
|
||||||
*/
|
*/
|
||||||
public static String loadDefaultByType(String siteType) {
|
public static String loadDefaultByType(String siteType) {
|
||||||
if (siteType == null) return null;
|
if (siteType == null) return null;
|
||||||
String key = siteType.toLowerCase();
|
String key = siteType.toLowerCase();
|
||||||
return DEFAULT_SCRIPTS.computeIfAbsent(key, k -> {
|
return DEFAULT_SCRIPTS_CACHE.computeIfAbsent(key, k -> {
|
||||||
|
// 先尝试从存储服务读取
|
||||||
|
if (storageService != null) {
|
||||||
|
try {
|
||||||
|
String script = storageService.read(DEFAULT_SCRIPTS_PATH + k + ".groovy");
|
||||||
|
if (script != null && !script.isBlank()) return script;
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
// 回退到 classpath
|
||||||
String path = "scripts/default-" + k + ".groovy";
|
String path = "scripts/default-" + k + ".groovy";
|
||||||
try (var in = GroovyScriptEngine.class.getClassLoader().getResourceAsStream(path)) {
|
try (var in = GroovyScriptEngine.class.getClassLoader().getResourceAsStream(path)) {
|
||||||
if (in == null) return null;
|
if (in == null) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user