feat(admin): Java 补齐后台缺失能力(软件版本/图生视频管理/店铺记录列表)与菜单排序
将 Flask 后台仅本地实现、Java 缺失的模块落为原生端点(替代 15124 本地面,
向"单一 Java 后台、可退役 15124"收敛):
- softwareversion: GET /api/admin/versions、POST /api/admin/version(web_config +
公开桶 nanri-image/versions/ 直链,桌面端"检测更新"同表兼容)
- imagevideo 管理端: GET /api/admin/image-video-tasks、/{taskId}、
POST /download-zip(近3天 IMAGE_VIDEO_WORKFLOW,语义对齐 Flask)
- shopdatacrawl 管理端: GET /api/admin/shop-data-crawl-tasks(按店分组取最新结果,
窗口函数)、POST /download-zip(流式 + X-Archive-*-Count)
- 权限: 菜单同级拖拽排序 POST /column/reorder(事务内按序重排兄弟 sort_order);
PermissionMenuCreate/UpdateRequest 补 snake_case 别名兼容旧控制台 body
以上端点均 requireAdminOrInternal 鉴权;含 PermissionMenuService 早前已改动的
hasAnyAdminMenu 等菜单权限逻辑(本迁移一环)。
This commit is contained in:
+189
@@ -0,0 +1,189 @@
|
||||
package com.nanri.aiimage.modules.imagevideo.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.util.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.modules.admin.support.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoAdminTaskService;
|
||||
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoAdminTaskService.VideoSelection;
|
||||
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoAdminTaskService.ZipBundle;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 图生视频任务管理端点(替代 Flask admin_api.py 的 /image-video-tasks* 本地实现)。
|
||||
*
|
||||
* <p>路径与入参为管理后台前端契约,字段名保持 snake_case。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/image-video-tasks")
|
||||
@Tag(name = "图生视频任务管理", description = "图生视频/视频复刻工作流任务的查询与批量打包下载")
|
||||
public class AdminImageVideoTaskController {
|
||||
|
||||
private static final DateTimeFormatter[] DATETIME_PATTERNS = {
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
|
||||
};
|
||||
private static final DateTimeFormatter ZIP_FILENAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
|
||||
private static final int MAX_ZIP_SELECTIONS = 100;
|
||||
private static final String X_ARCHIVE_FILE_COUNT = "X-Archive-File-Count";
|
||||
private static final String X_ARCHIVE_ERROR_COUNT = "X-Archive-Error-Count";
|
||||
|
||||
private final ImageVideoAdminTaskService adminTaskService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分页查询图生视频任务", description = "管理端按用户/任务状态/提交时间等条件分页查询近 3 天的工作流任务")
|
||||
public ApiResponse<Map<String, Object>> page(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "page_size", defaultValue = "20") Integer pageSize,
|
||||
@RequestParam(name = "user_id", required = false) Long userId,
|
||||
@RequestParam(required = false) String username,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(name = "coze_execute_id", required = false) String executeId,
|
||||
@RequestParam(name = "submitted_from", required = false) String submittedFrom,
|
||||
@RequestParam(name = "submitted_to", required = false) String submittedTo,
|
||||
HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
int safePage = Math.max(1, page == null ? 1 : page);
|
||||
int safePageSize = Math.min(100, Math.max(10, pageSize == null ? 20 : pageSize));
|
||||
String cleanUsername = username == null ? null : username.trim();
|
||||
String cleanStatus = status == null ? null : status.trim().toUpperCase();
|
||||
String cleanExecuteId = executeId == null ? null : executeId.trim();
|
||||
LocalDateTime from = parseAdminDatetimeParam("submitted_from", submittedFrom);
|
||||
LocalDateTime to = parseAdminDatetimeParam("submitted_to", submittedTo);
|
||||
log.info("[image-video] 管理端查询图生视频任务 page={} pageSize={} userId={} username={} status={}",
|
||||
safePage, safePageSize, userId, cleanUsername, cleanStatus);
|
||||
return ApiResponse.success(adminTaskService.page(userId, cleanUsername, cleanStatus,
|
||||
cleanExecuteId, from, to, safePage, safePageSize));
|
||||
}
|
||||
|
||||
@GetMapping("/{taskId}")
|
||||
@Operation(summary = "查询图生视频任务详情", description = "返回单个任务详情(含脱敏后的请求参数与结果 JSON)")
|
||||
public ApiResponse<Map<String, Object>> detail(@PathVariable Long taskId, HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
return ApiResponse.success(adminTaskService.detail(taskId));
|
||||
}
|
||||
|
||||
@PostMapping("/download-zip")
|
||||
@Operation(summary = "批量打包下载视频", description = "按任务+视频下标批量打包 zip;部分失败通过响应头 X-Archive-Error-Count 提示")
|
||||
public ResponseEntity<StreamingResponseBody> downloadZip(@RequestBody(required = false) Map<String, Object> body,
|
||||
HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
List<VideoSelection> selections = parseSelections(body);
|
||||
ZipBundle bundle = adminTaskService.buildZip(selections);
|
||||
String filename = "video-tasks-" + LocalDateTime.now().format(ZIP_FILENAME_FORMATTER) + ".zip";
|
||||
StreamingResponseBody stream = outputStream -> writeZipFile(bundle.file(), outputStream);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType("application/zip"))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, DownloadHeaderUtil.contentDisposition(filename))
|
||||
.header(X_ARCHIVE_FILE_COUNT, String.valueOf(bundle.fileCount()))
|
||||
.header(X_ARCHIVE_ERROR_COUNT, String.valueOf(bundle.errorCount()))
|
||||
.body(stream);
|
||||
}
|
||||
|
||||
private void writeZipFile(Path file, OutputStream output) throws IOException {
|
||||
try (InputStream input = Files.newInputStream(file)) {
|
||||
input.transferTo(output);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[image-video] 管理端 zip 回传中断 path={} error={}", file, ex.getMessage());
|
||||
throw ex;
|
||||
} finally {
|
||||
try {
|
||||
Files.deleteIfExists(file);
|
||||
} catch (IOException ex) {
|
||||
log.warn("[image-video] 管理端 zip 临时文件清理失败 path={} error={}", file, ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 解析下载选择参数,校验失败抛业务异常(对齐 Flask 文案)。 */
|
||||
private List<VideoSelection> parseSelections(Map<String, Object> body) {
|
||||
Object rawItems = body == null ? null : body.get("items");
|
||||
if (!(rawItems instanceof List<?> list) || list.isEmpty()) {
|
||||
throw new BusinessException(400, "请至少选择一个视频");
|
||||
}
|
||||
if (list.size() > MAX_ZIP_SELECTIONS) {
|
||||
throw new BusinessException(400, "单次最多打包 100 个视频");
|
||||
}
|
||||
Set<String> seen = new LinkedHashSet<>();
|
||||
List<VideoSelection> selections = new ArrayList<>(list.size());
|
||||
for (Object rawItem : list) {
|
||||
try {
|
||||
if (!(rawItem instanceof Map<?, ?> item)) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
long taskId = toLong(item.get("task_id"));
|
||||
int videoIndex = toLong(item.get("video_index")).intValue();
|
||||
if (taskId <= 0 || videoIndex < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
String key = taskId + ":" + videoIndex;
|
||||
if (seen.add(key)) {
|
||||
selections.add(new VideoSelection(taskId, videoIndex));
|
||||
}
|
||||
} catch (NumberFormatException | NullPointerException ex) {
|
||||
throw new BusinessException(400, "视频选择参数无效");
|
||||
}
|
||||
}
|
||||
return selections;
|
||||
}
|
||||
|
||||
private Long toLong(Object value) {
|
||||
if (value instanceof Number number) {
|
||||
return number.longValue();
|
||||
}
|
||||
return Long.valueOf(String.valueOf(value).trim());
|
||||
}
|
||||
|
||||
/** 解析管理页时间筛选参数(datetime-local 或 ISO),解析失败抛 400 中文提示。 */
|
||||
private LocalDateTime parseAdminDatetimeParam(String name, String rawValue) {
|
||||
if (rawValue == null || rawValue.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String text = rawValue.trim();
|
||||
if (text.endsWith("Z") || text.endsWith("z")) {
|
||||
text = text.substring(0, text.length() - 1);
|
||||
}
|
||||
for (DateTimeFormatter formatter : DATETIME_PATTERNS) {
|
||||
try {
|
||||
return LocalDateTime.parse(text, formatter);
|
||||
} catch (DateTimeParseException ignored) {
|
||||
// 尝试下一种格式
|
||||
}
|
||||
}
|
||||
throw new BusinessException(400, name + " 时间格式无效");
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package com.nanri.aiimage.modules.imagevideo.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 图生视频任务管理端查询(替代 Flask admin_api.py 中 image-video-tasks 本地 SQL)。
|
||||
*/
|
||||
@Mapper
|
||||
public interface ImageVideoAdminTaskMapper {
|
||||
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT t.id, t.user_id, t.status, t.request_json, t.submit_response_json, t.result_json,
|
||||
t.video_urls_json, t.debug_url, t.archived_videos_json, t.archive_status,
|
||||
t.archive_error, t.archive_attempt_count,
|
||||
DATE_FORMAT(t.archived_at, '%Y-%m-%d %H:%i:%s') AS archived_at,
|
||||
t.error_message, t.coze_execute_id, t.coze_status,
|
||||
DATE_FORMAT(t.submitted_at, '%Y-%m-%d %H:%i:%s') AS submitted_at,
|
||||
DATE_FORMAT(t.completed_at, '%Y-%m-%d %H:%i:%s') AS completed_at,
|
||||
u.username,
|
||||
COALESCE((
|
||||
SELECT GROUP_CONCAT(DISTINCT g.group_name ORDER BY g.id SEPARATOR '、')
|
||||
FROM biz_shop_manage_group g
|
||||
LEFT JOIN biz_shop_manage_group_member gm ON gm.group_id = g.id
|
||||
WHERE g.created_by_id = t.user_id OR g.user_id = t.user_id OR gm.user_id = t.user_id
|
||||
), '') AS group_name
|
||||
FROM biz_image_video_async_task t
|
||||
LEFT JOIN users u ON u.id = t.user_id
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
<if test="userId != null"> AND t.user_id = #{userId}</if>
|
||||
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
|
||||
<if test="status != null and status != ''"> AND t.status = #{status}</if>
|
||||
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
|
||||
<if test="submittedFrom != null"> AND t.submitted_at >= #{submittedFrom}</if>
|
||||
<if test="submittedTo != null"> AND t.submitted_at <= #{submittedTo}</if>
|
||||
ORDER BY t.submitted_at DESC, t.id DESC
|
||||
LIMIT #{limit} OFFSET #{offset}
|
||||
</script>
|
||||
""")
|
||||
List<Map<String, Object>> selectAdminTaskPage(
|
||||
@Param("userId") Long userId,
|
||||
@Param("username") String username,
|
||||
@Param("status") String status,
|
||||
@Param("executeId") String executeId,
|
||||
@Param("submittedFrom") LocalDateTime submittedFrom,
|
||||
@Param("submittedTo") LocalDateTime submittedTo,
|
||||
@Param("limit") int limit,
|
||||
@Param("offset") int offset);
|
||||
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT COUNT(*) AS total
|
||||
FROM biz_image_video_async_task t
|
||||
LEFT JOIN users u ON u.id = t.user_id
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
<if test="userId != null"> AND t.user_id = #{userId}</if>
|
||||
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
|
||||
<if test="status != null and status != ''"> AND t.status = #{status}</if>
|
||||
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
|
||||
<if test="submittedFrom != null"> AND t.submitted_at >= #{submittedFrom}</if>
|
||||
<if test="submittedTo != null"> AND t.submitted_at <= #{submittedTo}</if>
|
||||
</script>
|
||||
""")
|
||||
Long countAdminTaskPage(
|
||||
@Param("userId") Long userId,
|
||||
@Param("username") String username,
|
||||
@Param("status") String status,
|
||||
@Param("executeId") String executeId,
|
||||
@Param("submittedFrom") LocalDateTime submittedFrom,
|
||||
@Param("submittedTo") LocalDateTime submittedTo);
|
||||
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT t.id, t.video_urls_json, t.archived_videos_json
|
||||
FROM biz_image_video_async_task t
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
AND t.id IN
|
||||
<foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
|
||||
#{taskId}
|
||||
</foreach>
|
||||
</script>
|
||||
""")
|
||||
List<Map<String, Object>> selectAdminZipRows(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
@Select("""
|
||||
SELECT t.id, t.user_id, t.status, t.request_json, t.submit_response_json, t.result_json,
|
||||
t.video_urls_json, t.debug_url, t.archived_videos_json, t.archive_status,
|
||||
t.archive_error, t.archive_attempt_count,
|
||||
DATE_FORMAT(t.archived_at, '%Y-%m-%d %H:%i:%s') AS archived_at,
|
||||
t.error_message, t.coze_execute_id, t.coze_status,
|
||||
DATE_FORMAT(t.submitted_at, '%Y-%m-%d %H:%i:%s') AS submitted_at,
|
||||
DATE_FORMAT(t.completed_at, '%Y-%m-%d %H:%i:%s') AS completed_at,
|
||||
u.username,
|
||||
COALESCE((
|
||||
SELECT GROUP_CONCAT(DISTINCT g.group_name ORDER BY g.id SEPARATOR '、')
|
||||
FROM biz_shop_manage_group g
|
||||
LEFT JOIN biz_shop_manage_group_member gm ON gm.group_id = g.id
|
||||
WHERE g.created_by_id = t.user_id OR g.user_id = t.user_id OR gm.user_id = t.user_id
|
||||
), '') AS group_name
|
||||
FROM biz_image_video_async_task t
|
||||
LEFT JOIN users u ON u.id = t.user_id
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
AND t.id = #{taskId}
|
||||
LIMIT 1
|
||||
""")
|
||||
Map<String, Object> selectAdminTaskDetail(@Param("taskId") Long taskId);
|
||||
}
|
||||
+453
@@ -0,0 +1,453 @@
|
||||
package com.nanri.aiimage.modules.imagevideo.service;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoAdminTaskMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 图生视频任务管理端组装/打包逻辑(替代 Flask admin_api.py 的 image-video-tasks 实现)。
|
||||
*
|
||||
* <p>对外 JSON 一律输出 snake_case 字段名,与 Flask 管理页契约一致。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ImageVideoAdminTaskService {
|
||||
|
||||
private static final Pattern VIDEO_EXTENSION_PATTERN =
|
||||
Pattern.compile("\\.([a-z0-9]{2,5})(?:[?#]|$)", Pattern.CASE_INSENSITIVE);
|
||||
private static final String FILE_NAME_PREFIX = "video-tasks-";
|
||||
private static final DateTimeFormatter ZIP_FILENAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
|
||||
private static final Set<String> SECRET_KEYS = Set.of(
|
||||
"api_key", "apikey", "token", "coze_token", "cozetoken", "t8_key", "t8key",
|
||||
"t8_video_key", "t8videokey", "t8star_key", "t8starkey",
|
||||
"ai_conductor_key", "aiconductorkey", "copy_api_key", "copyapikey",
|
||||
"voice_api_key", "voiceapikey"
|
||||
);
|
||||
private static final Set<String> LOCAL_HOSTNAMES = Set.of(
|
||||
"localhost", "localhost.localdomain", "metadata.google.internal",
|
||||
"metadata.azure.internal", "169.254.169.254"
|
||||
);
|
||||
|
||||
private static final int HTTP_CONNECT_TIMEOUT_MILLIS = 10_000;
|
||||
private static final int HTTP_READ_TIMEOUT_MILLIS = 120_000;
|
||||
private static final int STREAM_CHUNK_BYTES = 1024 * 1024;
|
||||
private static final String DOWNLOAD_ERRORS_ENTRY = "download-errors.txt";
|
||||
|
||||
private final ImageVideoAdminTaskMapper adminTaskMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public ImageVideoAdminTaskService(ImageVideoAdminTaskMapper adminTaskMapper, ObjectMapper objectMapper) {
|
||||
this.adminTaskMapper = adminTaskMapper;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询图生视频工作流任务,返回 {items, total, page, page_size}。
|
||||
*/
|
||||
public Map<String, Object> page(Long userId, String username, String status, String executeId,
|
||||
LocalDateTime submittedFrom, LocalDateTime submittedTo,
|
||||
int page, int pageSize) {
|
||||
int offset = (page - 1) * pageSize;
|
||||
List<Map<String, Object>> rows = adminTaskMapper.selectAdminTaskPage(
|
||||
userId, username, status, executeId, submittedFrom, submittedTo, pageSize, offset);
|
||||
Long total = adminTaskMapper.countAdminTaskPage(
|
||||
userId, username, status, executeId, submittedFrom, submittedTo);
|
||||
List<Map<String, Object>> items = new ArrayList<>(rows.size());
|
||||
for (Map<String, Object> row : rows) {
|
||||
items.add(toListItem(row, false));
|
||||
}
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("items", items);
|
||||
result.put("total", total == null ? 0L : total);
|
||||
result.put("page", (long) page);
|
||||
result.put("page_size", (long) pageSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单任务详情(含 request/submit_response/result 原始 JSON,request 脱敏)。
|
||||
*/
|
||||
public Map<String, Object> detail(Long taskId) {
|
||||
Map<String, Object> row = adminTaskMapper.selectAdminTaskDetail(taskId);
|
||||
if (row == null) {
|
||||
throw new BusinessException(404, "视频任务不存在或无权访问");
|
||||
}
|
||||
return Map.of("item", toListItem(row, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量打包下载选中的视频。逐文件远程拉取写入 zip,部分失败时记录错误清单文件。
|
||||
*/
|
||||
public ZipBundle buildZip(List<VideoSelection> selections) {
|
||||
List<Long> taskIds = selections.stream()
|
||||
.map(VideoSelection::taskId)
|
||||
.distinct()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
Map<Long, Map<String, Object>> tasks = new HashMap<>();
|
||||
for (Map<String, Object> row : adminTaskMapper.selectAdminZipRows(taskIds)) {
|
||||
if (row.get("id") instanceof Number id) {
|
||||
tasks.put(id.longValue(), row);
|
||||
}
|
||||
}
|
||||
|
||||
Path zipFile = null;
|
||||
List<String> errors = new ArrayList<>();
|
||||
int fileCount = 0;
|
||||
try {
|
||||
zipFile = Files.createTempFile("image-video-tasks-", ".zip");
|
||||
try (ZipOutputStream zip = new ZipOutputStream(
|
||||
new BufferedOutputStream(Files.newOutputStream(zipFile), STREAM_CHUNK_BYTES))) {
|
||||
for (VideoSelection selection : selections) {
|
||||
Map<String, Object> row = tasks.get(selection.taskId());
|
||||
List<Map<String, Object>> videos = row == null ? List.of() : buildVideos(row);
|
||||
String url = "";
|
||||
if (selection.videoIndex() < videos.size()) {
|
||||
Object displayUrl = videos.get(selection.videoIndex()).get("display_url");
|
||||
url = displayUrl == null ? "" : String.valueOf(displayUrl);
|
||||
}
|
||||
if (url.isBlank()) {
|
||||
errors.add(String.format("task-%d-video-%d: 视频地址不存在",
|
||||
selection.taskId(), selection.videoIndex() + 1));
|
||||
continue;
|
||||
}
|
||||
String filename = "task-" + selection.taskId() + "-video-" + (selection.videoIndex() + 1)
|
||||
+ "." + detectExtension(url);
|
||||
if (isInternalUrl(url)) {
|
||||
throw new BusinessException(400, "拒绝下载内网/本机地址的视频");
|
||||
}
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = openRemoteConnection(url);
|
||||
int status = connection.getResponseCode();
|
||||
if (status < 200 || status >= 300) {
|
||||
throw new IOException("HTTP " + status);
|
||||
}
|
||||
zip.putNextEntry(new ZipEntry(filename));
|
||||
try (InputStream input = connection.getInputStream()) {
|
||||
byte[] buffer = new byte[STREAM_CHUNK_BYTES];
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
zip.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
zip.closeEntry();
|
||||
fileCount++;
|
||||
} catch (Exception ex) {
|
||||
errors.add(filename + ": 下载失败 (" + messageOf(ex) + ")");
|
||||
log.warn("[image-video] 管理端批量打包单文件失败 filename={} error={}",
|
||||
filename, messageOf(ex));
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
zip.putNextEntry(new ZipEntry(DOWNLOAD_ERRORS_ENTRY));
|
||||
zip.write(String.join("\n", errors).getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
}
|
||||
log.info("[image-video] 管理端批量打包完成 文件数={} 失败数={} 选择数={}",
|
||||
fileCount, errors.size(), selections.size());
|
||||
return new ZipBundle(zipFile, fileCount, errors.size());
|
||||
} catch (Exception ex) {
|
||||
deleteQuietly(zipFile);
|
||||
throw ex instanceof BusinessException business
|
||||
? business
|
||||
: new BusinessException("批量打包失败: " + messageOf(ex), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpURLConnection openRemoteConnection(String url) throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
|
||||
connection.setConnectTimeout(HTTP_CONNECT_TIMEOUT_MILLIS);
|
||||
connection.setReadTimeout(HTTP_READ_TIMEOUT_MILLIS);
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
connection.setRequestProperty("User-Agent", "aiimage-image-video-admin/1.0");
|
||||
return connection;
|
||||
}
|
||||
|
||||
private void deleteQuietly(Path path) {
|
||||
if (path != null) {
|
||||
try {
|
||||
Files.deleteIfExists(path);
|
||||
} catch (IOException ex) {
|
||||
log.warn("[image-video] 管理端打包临时文件清理失败 path={} error={}", path, ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String messageOf(Exception ex) {
|
||||
String message = ex.getMessage();
|
||||
return message == null || message.isBlank() ? ex.getClass().getSimpleName() : message;
|
||||
}
|
||||
|
||||
private String detectExtension(String url) {
|
||||
Matcher matcher = VIDEO_EXTENSION_PATTERN.matcher(url);
|
||||
return matcher.find() ? matcher.group(1).toLowerCase(Locale.ROOT) : "mp4";
|
||||
}
|
||||
|
||||
private Map<String, Object> toListItem(Map<String, Object> row, boolean includeJson) {
|
||||
List<Map<String, Object>> videos = buildVideos(row);
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("task_id", row.get("id"));
|
||||
item.put("user_id", row.get("user_id"));
|
||||
item.put("username", orEmpty(row.get("username")));
|
||||
item.put("group_name", orEmpty(row.get("group_name")));
|
||||
item.put("mode", modeOf(row.get("request_json")));
|
||||
item.put("status", orEmpty(row.get("status")));
|
||||
item.put("coze_status", orEmpty(row.get("coze_status")));
|
||||
item.put("coze_execute_id", orEmpty(row.get("coze_execute_id")));
|
||||
item.put("videos", videos);
|
||||
item.put("video_url", videos.isEmpty() ? "" : videos.get(0).get("display_url"));
|
||||
item.put("debug_url", orEmpty(row.get("debug_url")));
|
||||
item.put("archive_status", orEmpty(row.get("archive_status")));
|
||||
item.put("archive_error", orEmpty(row.get("archive_error")));
|
||||
item.put("archive_attempt_count", numberOrDefault(row.get("archive_attempt_count")));
|
||||
item.put("submitted_at", orEmpty(row.get("submitted_at")));
|
||||
item.put("completed_at", orEmpty(row.get("completed_at")));
|
||||
if (includeJson) {
|
||||
item.put("error_message", orEmpty(row.get("error_message")));
|
||||
item.put("archived_at", orEmpty(row.get("archived_at")));
|
||||
Object request = parseJsonLoose(stringOf(row.get("request_json")), new LinkedHashMap<String, Object>());
|
||||
item.put("request", maskSecrets(request));
|
||||
item.put("submit_response", parseJsonLoose(stringOf(row.get("submit_response_json")),
|
||||
new LinkedHashMap<String, Object>()));
|
||||
item.put("result", parseJsonLoose(stringOf(row.get("result_json")),
|
||||
new LinkedHashMap<String, Object>()));
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼装视频数组:以 video_urls_json 顺序为基准,从 archived_videos_json 补齐归档信息。
|
||||
*/
|
||||
private List<Map<String, Object>> buildVideos(Map<String, Object> row) {
|
||||
List<String> originals = parseStringList(row.get("video_urls_json"));
|
||||
List<Map<String, Object>> archived = parseArchivedList(row.get("archived_videos_json"));
|
||||
Map<String, Map<String, Object>> archivedBySource = new LinkedHashMap<>();
|
||||
for (Map<String, Object> stored : archived) {
|
||||
archivedBySource.put(String.valueOf(stored.getOrDefault("sourceUrl", "")), stored);
|
||||
}
|
||||
List<Map<String, Object>> items = new ArrayList<>(originals.size());
|
||||
for (String sourceUrl : originals) {
|
||||
Map<String, Object> stored = archivedBySource.getOrDefault(sourceUrl, Map.of());
|
||||
Map<String, Object> video = new LinkedHashMap<>();
|
||||
video.put("source_url", sourceUrl);
|
||||
video.put("archived_url", stored.getOrDefault("url", ""));
|
||||
video.put("object_key", stored.getOrDefault("objectKey", ""));
|
||||
video.put("archive_status", stored.getOrDefault("status", ""));
|
||||
video.put("archive_error", stored.getOrDefault("error", ""));
|
||||
String displayUrl = orEmpty(stored.get("url"));
|
||||
video.put("display_url", displayUrl.isBlank() ? sourceUrl : displayUrl);
|
||||
items.add(video);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** 从 request_json 中取 parameters.video_info.mode:1 图生视频、2 视频复刻,其余未知。 */
|
||||
private String modeOf(Object requestJson) {
|
||||
Object parsed = parseJsonLoose(stringOf(requestJson), new LinkedHashMap<String, Object>());
|
||||
String mode = "";
|
||||
try {
|
||||
Object parameters = childOf(parsed, "parameters");
|
||||
Object videoInfo = childOf(parameters, "video_info");
|
||||
Object rawMode = childOf(videoInfo, "mode");
|
||||
mode = rawMode == null ? "" : String.valueOf(rawMode).trim();
|
||||
} catch (Exception ignored) {
|
||||
// 结构不完整按未知模式处理
|
||||
}
|
||||
if ("1".equals(mode)) {
|
||||
return "图生视频";
|
||||
}
|
||||
if ("2".equals(mode)) {
|
||||
return "视频复刻";
|
||||
}
|
||||
return "未知";
|
||||
}
|
||||
|
||||
private Object childOf(Object value, String name) {
|
||||
return value instanceof Map<?, ?> map ? map.get(name) : null;
|
||||
}
|
||||
|
||||
/** 递归把配置密钥类的值替换为星号(与 Flask _mask_image_video_secrets 一致)。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object maskSecrets(Object value) {
|
||||
if (value instanceof Map<?, ?> map) {
|
||||
Map<String, Object> masked = new LinkedHashMap<>();
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
String key = String.valueOf(entry.getKey());
|
||||
Object child = entry.getValue();
|
||||
String canonical = key.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]", "");
|
||||
boolean secret = SECRET_KEYS.stream().anyMatch(item -> item.toLowerCase(Locale.ROOT)
|
||||
.replaceAll("[^a-z0-9]", "").equals(canonical));
|
||||
if (secret && child != null && !String.valueOf(child).isBlank()) {
|
||||
masked.put(key, "******");
|
||||
} else {
|
||||
masked.put(key, maskSecrets(child));
|
||||
}
|
||||
}
|
||||
return masked;
|
||||
}
|
||||
if (value instanceof List<?> list) {
|
||||
List<Object> masked = new ArrayList<>(list.size());
|
||||
for (Object child : list) {
|
||||
masked.add(maskSecrets(child));
|
||||
}
|
||||
return masked;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private Object parseJsonLoose(String json, Object fallback) {
|
||||
if (json == null || json.isBlank()) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
return objectMapper.readValue(json, new TypeReference<Object>() {
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> parseStringList(Object raw) {
|
||||
String json = stringOf(raw);
|
||||
if (json == null || json.isBlank()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
try {
|
||||
List<String> parsed = objectMapper.readValue(json, new TypeReference<List<String>>() {
|
||||
});
|
||||
return parsed == null ? new ArrayList<>() : parsed;
|
||||
} catch (Exception ex) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> parseArchivedList(Object raw) {
|
||||
String json = stringOf(raw);
|
||||
if (json == null || json.isBlank()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
try {
|
||||
List<Map<String, Object>> parsed = objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
|
||||
});
|
||||
return parsed == null ? new ArrayList<>() : parsed;
|
||||
} catch (Exception ex) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 内网/本机/保留地址判断(对齐 Flask utils/ssrf.py 的 is_internal_url),解析失败按不可信处理。
|
||||
*/
|
||||
private boolean isInternalUrl(String url) {
|
||||
try {
|
||||
URI uri = URI.create(url);
|
||||
String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT);
|
||||
if (!"http".equals(scheme) && !"https".equals(scheme)) {
|
||||
return true;
|
||||
}
|
||||
String host = uri.getHost();
|
||||
if (host == null || host.isBlank()) {
|
||||
return true;
|
||||
}
|
||||
String normalized = host.toLowerCase(Locale.ROOT);
|
||||
while (normalized.endsWith(".")) {
|
||||
normalized = normalized.substring(0, normalized.length() - 1);
|
||||
}
|
||||
if (LOCAL_HOSTNAMES.contains(normalized)) {
|
||||
return true;
|
||||
}
|
||||
InetAddress[] addresses = InetAddress.getAllByName(normalized);
|
||||
if (addresses.length == 0) {
|
||||
return true;
|
||||
}
|
||||
for (InetAddress address : addresses) {
|
||||
if (isBlockedAddress(address)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Exception ex) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isBlockedAddress(InetAddress address) {
|
||||
if (address.isAnyLocalAddress() || address.isLoopbackAddress()
|
||||
|| address.isLinkLocalAddress() || address.isSiteLocalAddress()
|
||||
|| address.isMulticastAddress()) {
|
||||
return true;
|
||||
}
|
||||
byte[] raw = address.getAddress();
|
||||
if (raw == null) {
|
||||
return true;
|
||||
}
|
||||
if (raw.length != 4) {
|
||||
// IPv6 其余保留段:fc00::/7 内网唯一本地地址
|
||||
int first = raw[0] & 0xff;
|
||||
return first == 0xfc || first == 0xfd;
|
||||
}
|
||||
int first = raw[0] & 0xff;
|
||||
int second = raw[1] & 0xff;
|
||||
int third = raw[2] & 0xff;
|
||||
if (first == 100) {
|
||||
return second >= 64 && second <= 127; // 100.64.0.0/10 CGNAT
|
||||
}
|
||||
if (first == 192 && second == 0 && third == 0) {
|
||||
return true; // 192.0.0.0/24
|
||||
}
|
||||
return first == 198 && (second == 18 || second == 19); // 198.18.0.0/15
|
||||
}
|
||||
|
||||
private String orEmpty(Object value) {
|
||||
return value == null ? "" : String.valueOf(value);
|
||||
}
|
||||
|
||||
private String stringOf(Object value) {
|
||||
return value == null ? null : String.valueOf(value);
|
||||
}
|
||||
|
||||
private int numberOrDefault(Object value) {
|
||||
return value instanceof Number number ? number.intValue() : 0;
|
||||
}
|
||||
|
||||
/** 待打包的视频选择:taskId + 视频下标(0 起)。 */
|
||||
public record VideoSelection(Long taskId, int videoIndex) {
|
||||
}
|
||||
|
||||
/** 打包结果:临时 zip 文件路径、成功文件数与失败数。 */
|
||||
public record ZipBundle(Path file, int fileCount, int errorCount) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user