feat: GET /configs/{siteId}/script 无版本号取最新脚本+默认模板回退
This commit is contained in:
@@ -3,6 +3,8 @@ package com.par.api.controller;
|
|||||||
import com.par.core.dto.ApiResponse;
|
import com.par.core.dto.ApiResponse;
|
||||||
import com.par.core.dto.SiteConfigDTO;
|
import com.par.core.dto.SiteConfigDTO;
|
||||||
import com.par.core.entity.SiteConfig;
|
import com.par.core.entity.SiteConfig;
|
||||||
|
import com.par.core.adapter.GroovyScriptEngine;
|
||||||
|
import com.par.core.mapper.SiteMapper;
|
||||||
import com.par.core.service.ConfigService;
|
import com.par.core.service.ConfigService;
|
||||||
import com.par.core.storage.CachedStorageService;
|
import com.par.core.storage.CachedStorageService;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -25,6 +27,7 @@ public class ConfigController {
|
|||||||
|
|
||||||
private final ConfigService configService;
|
private final ConfigService configService;
|
||||||
private final CachedStorageService storageService;
|
private final CachedStorageService storageService;
|
||||||
|
private final SiteMapper siteMapper;
|
||||||
|
|
||||||
public ConfigController(ConfigService configService, CachedStorageService storageService) {
|
public ConfigController(ConfigService configService, CachedStorageService storageService) {
|
||||||
this.configService = configService;
|
this.configService = configService;
|
||||||
@@ -117,6 +120,38 @@ public class ConfigController {
|
|||||||
.body(content);
|
.body(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载最新版本 Groovy 脚本(供 mediabot 使用)
|
||||||
|
* 优先自定义脚本,无自定义则返回站点类型默认模板
|
||||||
|
*/
|
||||||
|
@GetMapping("/{siteId}/script")
|
||||||
|
public ResponseEntity<String> downloadLatestScript(@PathVariable("siteId") String siteId) {
|
||||||
|
// 1. 尝试最新自定义脚本
|
||||||
|
SiteConfig latest = configService.getLatestConfig(siteId);
|
||||||
|
if (latest != null && latest.getScriptStoragePath() != null && !latest.getScriptStoragePath().isBlank()) {
|
||||||
|
try {
|
||||||
|
String script = storageService.read(latest.getScriptStoragePath());
|
||||||
|
if (script != null && !script.isBlank()) {
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.header(HttpHeaders.CONTENT_TYPE, "text/plain;charset=UTF-8")
|
||||||
|
.body(script);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 回退到站点类型默认脚本
|
||||||
|
var site = siteMapper.selectBySiteId(siteId);
|
||||||
|
String siteType = site != null && site.getSiteType() != null ? site.getSiteType().getValue() : null;
|
||||||
|
String defaultScript = GroovyScriptEngine.loadDefaultByType(siteType);
|
||||||
|
if (defaultScript != null) {
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.header(HttpHeaders.CONTENT_TYPE, "text/plain;charset=UTF-8")
|
||||||
|
.body(defaultScript);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载指定版本 Groovy 脚本(供 mediabot 使用)
|
* 下载指定版本 Groovy 脚本(供 mediabot 使用)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user