refactor: 删除旧AdapterEngine及JSON配置相关代码,全面迁移到Groovy脚本
This commit is contained in:
@@ -92,15 +92,7 @@ public class AdminController {
|
||||
}
|
||||
config.setVersion(String.valueOf(nextVersion));
|
||||
|
||||
// 文件从 pending 移到 approved 路径
|
||||
String approvedPath = config.getStoragePath().replace("/pending/", "/approved/");
|
||||
String content = storageService.read(config.getStoragePath());
|
||||
if (content != null) {
|
||||
storageService.store(approvedPath, content);
|
||||
config.setStoragePath(approvedPath);
|
||||
}
|
||||
|
||||
// Groovy 脚本也移到 approved
|
||||
// Groovy 脚本从 pending 移到 approved
|
||||
if (config.getScriptStoragePath() != null && !config.getScriptStoragePath().isBlank()) {
|
||||
String approvedScriptPath = config.getScriptStoragePath().replace("/pending/", "/approved/");
|
||||
String script = storageService.read(config.getScriptStoragePath());
|
||||
@@ -125,7 +117,6 @@ public class AdminController {
|
||||
var site = siteMapper.selectBySiteId(config.getSiteId());
|
||||
if (site != null) {
|
||||
site.setCurrentConfigId(config.getId());
|
||||
site.setSchemaVersion(config.getSchemaVersion());
|
||||
siteMapper.updateById(site);
|
||||
}
|
||||
|
||||
@@ -152,9 +143,8 @@ public class AdminController {
|
||||
Long reviewerId = (Long) request.getAttribute("accountId");
|
||||
|
||||
// 删除 pending 文件
|
||||
storageService.delete(config.getStoragePath());
|
||||
if (config.getScriptStoragePath() != null) storageService.delete(config.getScriptStoragePath());
|
||||
|
||||
config.setStoragePath(null);
|
||||
config.setStatus(ConfigStatus.REJECTED);
|
||||
config.setReviewerId(reviewerId);
|
||||
config.setReviewedAt(LocalDateTime.now());
|
||||
@@ -181,54 +171,6 @@ public class AdminController {
|
||||
return ApiResponse.success(configs.stream().map(configService::toDTO).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看指定版本配置内容
|
||||
*/
|
||||
@GetMapping("/configs/{configId}/content")
|
||||
public ApiResponse<Map<String, Object>> getConfigContent(
|
||||
@PathVariable Long configId, HttpServletRequest request) {
|
||||
checkAdmin(request);
|
||||
SiteConfig config = siteConfigMapper.selectById(configId);
|
||||
if (config == null) return ApiResponse.error(404, "Config not found");
|
||||
String content = null;
|
||||
try { content = storageService.read(config.getStoragePath()); } catch (Exception e) {
|
||||
return ApiResponse.error(500, "读取配置失败: " + e.getMessage());
|
||||
}
|
||||
return ApiResponse.success(Map.of("id", config.getId(), "content", content != null ? content : ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑待审核配置内容
|
||||
*/
|
||||
@PutMapping("/configs/{configId}/content")
|
||||
public ApiResponse<SiteConfigDTO> updateConfigContent(
|
||||
@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 content = payload.get("content");
|
||||
if (content == null || content.isBlank()) {
|
||||
return ApiResponse.error(400, "content is required");
|
||||
}
|
||||
|
||||
// 更新存储文件
|
||||
storageService.store(config.getStoragePath(), content);
|
||||
|
||||
// 更新 hash 和文件大小
|
||||
config.setConfigHash(HmacUtil.sha256(content));
|
||||
config.setFileSize((long) content.getBytes(StandardCharsets.UTF_8).length);
|
||||
siteConfigMapper.updateById(config);
|
||||
|
||||
log.info("Config content updated: id={}, siteId={}", configId, config.getSiteId());
|
||||
return ApiResponse.success(configService.toDTO(config));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看指定配置的 Groovy 脚本
|
||||
*/
|
||||
|
||||
@@ -55,72 +55,6 @@ public class ConfigController {
|
||||
return ApiResponse.success(configService.listApprovedConfigs(siteId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看配置内容(管理员用,返回 ApiResponse 包装)
|
||||
*/
|
||||
@GetMapping("/{siteId}/{version}/content")
|
||||
public ApiResponse<String> viewConfigContent(
|
||||
@PathVariable("siteId") String siteId,
|
||||
@PathVariable("version") String version) {
|
||||
|
||||
SiteConfig config = configService.getConfig(siteId, version);
|
||||
if (config == null) {
|
||||
return ApiResponse.error(404, "配置不存在: " + siteId + " v" + version);
|
||||
}
|
||||
|
||||
String content;
|
||||
try {
|
||||
content = storageService.read(config.getStoragePath());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to read config: siteId={}, version={}", siteId, version, e);
|
||||
return ApiResponse.error(500, "读取配置文件失败: " + e.getMessage());
|
||||
}
|
||||
if (content == null) {
|
||||
return ApiResponse.error(404, "配置文件不存在: " + config.getStoragePath());
|
||||
}
|
||||
|
||||
return ApiResponse.success(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载指定版本配置(支持 ETag 条件请求,供 mediabot 客户端使用)
|
||||
*/
|
||||
@GetMapping("/{siteId}/{version}")
|
||||
public ResponseEntity<String> downloadConfig(
|
||||
@PathVariable("siteId") String siteId,
|
||||
@PathVariable("version") String version,
|
||||
@RequestHeader(value = "If-None-Match", required = false) String ifNoneMatch) {
|
||||
|
||||
SiteConfig config = configService.getConfig(siteId, version);
|
||||
if (config == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
// ETag 匹配则返回 304
|
||||
if (ifNoneMatch != null && ifNoneMatch.equals(config.getConfigHash())) {
|
||||
return ResponseEntity.status(304)
|
||||
.eTag(config.getConfigHash())
|
||||
.build();
|
||||
}
|
||||
|
||||
// 读取文件内容(通过存储抽象:本地缓存或七牛云)
|
||||
String content;
|
||||
try {
|
||||
content = storageService.read(config.getStoragePath());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to read config: siteId={}, version={}", siteId, version, e);
|
||||
return ResponseEntity.status(500).body("Failed to read config");
|
||||
}
|
||||
if (content == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.eTag(config.getConfigHash())
|
||||
.header(org.springframework.http.HttpHeaders.CONTENT_TYPE, "application/json")
|
||||
.body(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载最新版本 Groovy 脚本(供 mediabot 使用)
|
||||
* 优先自定义脚本,无自定义则返回站点类型默认模板
|
||||
@@ -186,20 +120,14 @@ public class ConfigController {
|
||||
HttpServletRequest request) {
|
||||
|
||||
String version = (String) payload.get("version");
|
||||
String configJson = (String) payload.get("config");
|
||||
String schemaVersion = (String) payload.get("schemaVersion");
|
||||
String groovyScript = (String) payload.get("script");
|
||||
|
||||
if (version == null) {
|
||||
return ApiResponse.error(400, "version is required");
|
||||
}
|
||||
if ((configJson == null || configJson.isBlank()) && (groovyScript == null || groovyScript.isBlank())) {
|
||||
return ApiResponse.error(400, "config 或 script 至少需要一个");
|
||||
}
|
||||
if (version == null) return ApiResponse.error(400, "version is required");
|
||||
if (groovyScript == null || groovyScript.isBlank()) return ApiResponse.error(400, "script is required");
|
||||
|
||||
Long submitterId = (Long) request.getAttribute("accountId");
|
||||
|
||||
SiteConfig config = configService.submitConfig(siteId, version, configJson, schemaVersion, groovyScript, submitterId);
|
||||
SiteConfig config = configService.submitConfig(siteId, version, groovyScript, submitterId);
|
||||
return ApiResponse.success(configService.toDTO(config));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,13 +63,11 @@ public class ManifestController {
|
||||
SiteConfig latest = siteConfigMapper.selectLatestBySiteId(site.getSiteId());
|
||||
if (latest != null) {
|
||||
entry.put("latestVersion", latest.getVersion());
|
||||
entry.put("configHash", latest.getConfigHash());
|
||||
entry.put("hasScript", latest.getScriptStoragePath() != null && !latest.getScriptStoragePath().isBlank());
|
||||
entry.put("updatedAt", latest.getCreatedAt());
|
||||
}
|
||||
// 参与 ETag 计算
|
||||
etagBuilder.append(site.getSiteId())
|
||||
.append(latest != null ? latest.getConfigHash() : "none")
|
||||
.append(latest != null ? latest.getScriptStoragePath() : "none")
|
||||
.append(";");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user