fix: 脚本导出改为ZIP(每个站点一个.groovy文件)+导入改为上传ZIP

This commit is contained in:
mediabot-pt
2026-07-02 12:57:09 +08:00
parent dc0a9a09d8
commit c2ae8ab89c

View File

@@ -405,61 +405,77 @@ public class AdminController {
// ==================== 脚本批量导入导出 ==================== // ==================== 脚本批量导入导出 ====================
/** /**
* 导出所有站点脚本 JSON:{siteId: scriptContent} * 导出所有站点脚本为 ZIP:每个站点一个 .groovy 文件
*/ */
@GetMapping("/scripts/export") @GetMapping("/scripts/export")
public ResponseEntity<byte[]> exportScripts(HttpServletRequest request) { public ResponseEntity<byte[]> exportScripts(HttpServletRequest request) {
checkAdmin(request); checkAdmin(request);
Map<String, String> all = new LinkedHashMap<>(); List<SiteConfig> configs = siteConfigMapper.selectList(
for (SiteConfig sc : siteConfigMapper.selectList(
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>() new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>()
.eq(SiteConfig::getDeleted, 0) .eq(SiteConfig::getDeleted, 0)
.eq(SiteConfig::getIsLatest, true) .eq(SiteConfig::getIsLatest, true)
.isNotNull(SiteConfig::getScriptStoragePath))) { .isNotNull(SiteConfig::getScriptStoragePath));
try { try {
String script = storageService.read(sc.getScriptStoragePath()); var baos = new java.io.ByteArrayOutputStream();
if (script != null && !script.isBlank()) all.put(sc.getSiteId(), script); try (var zos = new java.util.zip.ZipOutputStream(baos)) {
} catch (Exception ignored) {} for (SiteConfig sc : configs) {
String script = storageService.read(sc.getScriptStoragePath());
if (script == null || script.isBlank()) continue;
var entry = new java.util.zip.ZipEntry(sc.getSiteId() + ".groovy");
zos.putNextEntry(entry);
zos.write(script.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
}
}
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=scripts.zip")
.contentType(MediaType.parseMediaType("application/zip"))
.body(baos.toByteArray());
} catch (Exception e) {
log.error("导出脚本失败", e);
return ResponseEntity.status(500).build();
} }
byte[] json = new com.fasterxml.jackson.databind.ObjectMapper()
.writerWithDefaultPrettyPrinter().writeValueAsBytes(all);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=scripts-export.json")
.contentType(MediaType.APPLICATION_JSON)
.body(json);
} }
/** /**
* 导入脚本:{siteId: scriptContent},保存到本地存储 * 导入脚本 ZIP:每个 .groovy 文件名 = siteId,保存到本地
*/ */
@PostMapping("/scripts/import") @PostMapping("/scripts/import")
public ApiResponse<Map<String, Object>> importScripts( public ApiResponse<Map<String, Object>> importScripts(
@RequestBody Map<String, String> payload, @RequestParam("file") org.springframework.web.multipart.MultipartFile file,
HttpServletRequest request) { HttpServletRequest request) {
checkAdmin(request); checkAdmin(request);
int ok = 0, fail = 0; int ok = 0, fail = 0;
for (Map.Entry<String, String> e : payload.entrySet()) { try (var zis = new java.util.zip.ZipInputStream(file.getInputStream())) {
String siteId = e.getKey(); java.util.zip.ZipEntry entry;
String script = e.getValue(); while ((entry = zis.getNextEntry()) != null) {
if (script == null || script.isBlank()) continue; if (entry.isDirectory() || !entry.getName().endsWith(".groovy")) continue;
try { String siteId = entry.getName().replace(".groovy", "");
int nextVersion = 1; String script = new String(zis.readAllBytes(), StandardCharsets.UTF_8);
List<SiteConfig> approved = siteConfigMapper.selectList( if (script.isBlank()) continue;
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>() try {
.eq(SiteConfig::getSiteId, siteId) int nextVersion = 1;
.eq(SiteConfig::getDeleted, 0) List<SiteConfig> existing = siteConfigMapper.selectList(
.orderByDesc(SiteConfig::getVersion) new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>()
.last("limit 1")); .eq(SiteConfig::getSiteId, siteId)
if (!approved.isEmpty()) { .eq(SiteConfig::getDeleted, 0)
try { nextVersion = Integer.parseInt(approved.get(0).getVersion()) + 1; } .orderByDesc(SiteConfig::getVersion)
catch (NumberFormatException ex) {} .last("limit 1"));
if (!existing.isEmpty()) {
try { nextVersion = Integer.parseInt(existing.get(0).getVersion()) + 1; }
catch (NumberFormatException ignored) {}
}
configService.submitConfig(siteId, String.valueOf(nextVersion), script, null);
ok++;
} catch (Exception ex) {
log.error("导入脚本失败 siteId={}: {}", siteId, ex.getMessage());
fail++;
} }
configService.submitConfig(siteId, String.valueOf(nextVersion), script, null); zis.closeEntry();
ok++;
} catch (Exception ex) {
log.error("导入脚本失败 siteId={}: {}", siteId, ex.getMessage());
fail++;
} }
} catch (Exception e) {
log.error("解压导入ZIP失败", e);
return ApiResponse.error(500, "导入失败: " + e.getMessage());
} }
return ApiResponse.success(Map.of("ok", ok, "fail", fail)); return ApiResponse.success(Map.of("ok", ok, "fail", fail));
} }