fix: 脚本用自己内容的hash生成路径,避免与config文件冲突导致file exists
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user