feat: 脚本下载/编辑接口、前端Groovy编辑器、Groovy错误行号提取
This commit is contained in:
@@ -229,6 +229,52 @@ public class AdminController {
|
||||
return ApiResponse.success(configService.toDTO(config));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看指定配置的 Groovy 脚本
|
||||
*/
|
||||
@GetMapping("/configs/{configId}/script")
|
||||
public ApiResponse<Map<String, Object>> getConfigScript(
|
||||
@PathVariable Long configId, HttpServletRequest request) {
|
||||
checkAdmin(request);
|
||||
SiteConfig config = siteConfigMapper.selectById(configId);
|
||||
if (config == null) return ApiResponse.error(404, "Config not found");
|
||||
String script = null;
|
||||
if (config.getScriptStoragePath() != null) {
|
||||
try { script = storageService.read(config.getScriptStoragePath()); } catch (Exception ignored) {}
|
||||
}
|
||||
return ApiResponse.success(Map.of("id", config.getId(), "script", script != null ? script : ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑待审核配置的 Groovy 脚本
|
||||
*/
|
||||
@PutMapping("/configs/{configId}/script")
|
||||
public ApiResponse<SiteConfigDTO> updateConfigScript(
|
||||
@PathVariable Long configId,
|
||||
@RequestBody Map<String, String> payload,
|
||||
HttpServletRequest request) {
|
||||
checkAdmin(request);
|
||||
SiteConfig config = siteConfigMapper.selectById(configId);
|
||||
if (config == null) return ApiResponse.error(404, "Config not found");
|
||||
if (config.getStatus() != ConfigStatus.PENDING) {
|
||||
return ApiResponse.error(400, "只能编辑待审核的配置");
|
||||
}
|
||||
|
||||
String script = payload.get("script");
|
||||
if (script == null) return ApiResponse.error(400, "script is required");
|
||||
|
||||
String scriptPath = config.getScriptStoragePath();
|
||||
if (scriptPath == null || scriptPath.isBlank()) {
|
||||
scriptPath = String.format("configs/%s/pending/%s.groovy", config.getSiteId(), config.getConfigHash().substring(0, 8));
|
||||
config.setScriptStoragePath(scriptPath);
|
||||
}
|
||||
storageService.store(scriptPath, script);
|
||||
siteConfigMapper.updateById(config);
|
||||
|
||||
log.info("Config script updated: id={}, siteId={}", configId, config.getSiteId());
|
||||
return ApiResponse.success(configService.toDTO(config));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户信任等级(禁止修改管理员、禁止提升为管理员)
|
||||
*/
|
||||
|
||||
@@ -117,6 +117,29 @@ public class ConfigController {
|
||||
.body(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载指定版本 Groovy 脚本(供 mediabot 使用)
|
||||
*/
|
||||
@GetMapping("/{siteId}/{version}/script")
|
||||
public ResponseEntity<String> downloadScript(
|
||||
@PathVariable("siteId") String siteId,
|
||||
@PathVariable("version") String version) {
|
||||
|
||||
SiteConfig config = configService.getConfig(siteId, version);
|
||||
if (config == null || config.getScriptStoragePath() == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
String script;
|
||||
try { script = storageService.read(config.getScriptStoragePath()); }
|
||||
catch (Exception e) { return ResponseEntity.status(500).body("Failed to read script"); }
|
||||
if (script == null) return ResponseEntity.notFound().build();
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_TYPE, "text/plain;charset=UTF-8")
|
||||
.body(script);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交新配置(需 HMAC 鉴权)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user