diff --git a/par-api/src/main/java/com/par/api/controller/AdminController.java b/par-api/src/main/java/com/par/api/controller/AdminController.java index 0868d97..bdb2614 100644 --- a/par-api/src/main/java/com/par/api/controller/AdminController.java +++ b/par-api/src/main/java/com/par/api/controller/AdminController.java @@ -261,15 +261,14 @@ public class AdminController { if (script == null) return ApiResponse.error(400, "script is required"); String scriptPath = config.getScriptStoragePath(); - if (scriptPath == null || scriptPath.isBlank()) { - String hash = config.getConfigHash() != null ? config.getConfigHash().substring(0, 8) - : Long.toHexString(System.currentTimeMillis()); - // 始终用 pending 目录,审批通过时才移到 approved - scriptPath = String.format("configs/%s/pending/%s.groovy", config.getSiteId(), hash); - } else { - // 已有路径也强制用 pending 目录 - scriptPath = scriptPath.replace("/approved/", "/pending/"); + // 用脚本内容的 hash 生成路径,避免和旧脚本冲突 + String scriptHash = HmacUtil.sha256(script).substring(0, 8); + String newPath = String.format("configs/%s/pending/%s.groovy", config.getSiteId(), scriptHash); + // 如果旧路径存在且内容变了,删除旧文件 + if (scriptPath != null && !scriptPath.equals(newPath)) { + try { storageService.delete(scriptPath); } catch (Exception ignored) {} } + scriptPath = newPath; config.setScriptStoragePath(scriptPath); siteConfigMapper.updateById(config); try { diff --git a/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java b/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java index 1654a2c..fe75b04 100644 --- a/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java +++ b/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java @@ -83,10 +83,11 @@ public class ConfigServiceImpl implements ConfigService { } } - // 存储 Groovy 脚本 + // 存储 Groovy 脚本(用脚本自己的 hash,不和 config 共用) String scriptPath = null; if (hasScript) { - scriptPath = String.format("configs/%s/pending/%s.groovy", siteId, idPrefix); + String scriptHash = HmacUtil.sha256(groovyScript); + scriptPath = String.format("configs/%s/pending/%s.groovy", siteId, scriptHash.substring(0, 8)); storageService.store(scriptPath, groovyScript); }