fix: 脚本用自己内容的hash生成路径,避免与config文件冲突导致file exists

This commit is contained in:
mediabot-pt
2026-07-01 12:57:57 +08:00
parent 4aab1a74a5
commit 8c95af8028
2 changed files with 10 additions and 10 deletions

View File

@@ -261,15 +261,14 @@ public class AdminController {
if (script == null) return ApiResponse.error(400, "script is required"); if (script == null) return ApiResponse.error(400, "script is required");
String scriptPath = config.getScriptStoragePath(); String scriptPath = config.getScriptStoragePath();
if (scriptPath == null || scriptPath.isBlank()) { // 用脚本内容的 hash 生成路径,避免和旧脚本冲突
String hash = config.getConfigHash() != null ? config.getConfigHash().substring(0, 8) String scriptHash = HmacUtil.sha256(script).substring(0, 8);
: Long.toHexString(System.currentTimeMillis()); String newPath = String.format("configs/%s/pending/%s.groovy", config.getSiteId(), scriptHash);
// 始终用 pending 目录,审批通过时才移到 approved // 如果旧路径存在且内容变了,删除旧文件
scriptPath = String.format("configs/%s/pending/%s.groovy", config.getSiteId(), hash); if (scriptPath != null && !scriptPath.equals(newPath)) {
} else { try { storageService.delete(scriptPath); } catch (Exception ignored) {}
// 已有路径也强制用 pending 目录
scriptPath = scriptPath.replace("/approved/", "/pending/");
} }
scriptPath = newPath;
config.setScriptStoragePath(scriptPath); config.setScriptStoragePath(scriptPath);
siteConfigMapper.updateById(config); siteConfigMapper.updateById(config);
try { try {

View File

@@ -83,10 +83,11 @@ public class ConfigServiceImpl implements ConfigService {
} }
} }
// 存储 Groovy 脚本 // 存储 Groovy 脚本(用脚本自己的 hash,不和 config 共用)
String scriptPath = null; String scriptPath = null;
if (hasScript) { 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); storageService.store(scriptPath, groovyScript);
} }