feat(phase-3,4): 证据件服务端落点与两前端大档分片接线
- 后端新增 evidence 模块(V182 证据表/独立桶/超管读取面/内部登记)与 application.yml evidence 块;管理员四端点逐个 requireSuperAdmin。 - 分片通道补 @Scheduled 回收与 LocalTempCleanupService 的 chunk-uploads 覆盖; 版本/教程 confirm 接受 file_key,把拼装件搬进 client 桶后落库。 - 用户端 9MB 视频改走 uploadTempFileToJava 分片;后管版本/教程 >8MB 走分片, 小文件仍走原 presign 直传。 - 激活 Phase 3/4 后端与两前端预置契约。
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 任务异常证据件配置(v1.2 Phase 3/AC-13)。
|
||||
*
|
||||
* <p>证据件落在**独立桶**:默认桶 nanri-ai-images 带 7 天整桶 ILM,
|
||||
* 与"证据单独保留、单独回收"的口径互相绑死;桶名必须显式写在 application.yml,
|
||||
* 不许只在代码里留默认值(部署漏配就变成隐形行为)。</p>
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "aiimage.evidence")
|
||||
public class EvidenceProperties {
|
||||
|
||||
/** 证据桶;默认 task-evidence,独立于业务桶。 */
|
||||
private String bucket = "task-evidence";
|
||||
|
||||
/** 证据保留天数(本地与云端同口径,按天回收)。 */
|
||||
private Integer retentionDays = 7;
|
||||
|
||||
/** 对象 key 前缀;实际 key = 前缀 + taskId + "/" + UUID + 扩展名。 */
|
||||
private String keyPrefix = "task-evidence/";
|
||||
|
||||
/** 单件证据上限(字节),超过直接拒绝,避免截图/大 JSON 打满桶。 */
|
||||
private Long maxFileBytes = 64L * 1024 * 1024;
|
||||
|
||||
/** 保留期回收 cron;默认每天 03:30。 */
|
||||
private String retentionCron = "0 30 3 * * *";
|
||||
|
||||
public String bucketOrDefault() {
|
||||
return bucket == null || bucket.isBlank() ? "task-evidence" : bucket.trim();
|
||||
}
|
||||
|
||||
public int retentionDaysOrDefault() {
|
||||
return retentionDays == null || retentionDays < 1 ? 7 : retentionDays;
|
||||
}
|
||||
|
||||
public String keyPrefixOrDefault() {
|
||||
String prefix = keyPrefix == null ? "" : keyPrefix.trim();
|
||||
if (prefix.isEmpty()) {
|
||||
return "task-evidence/";
|
||||
}
|
||||
return prefix.endsWith("/") ? prefix : prefix + "/";
|
||||
}
|
||||
|
||||
public long maxFileBytesOrDefault() {
|
||||
return maxFileBytes == null || maxFileBytes < 1 ? 64L * 1024 * 1024 : maxFileBytes;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, KeywordShareSyncProperties.class, CollectRoutingProperties.class})
|
||||
@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, KeywordShareSyncProperties.class, CollectRoutingProperties.class, EvidenceProperties.class})
|
||||
public class PropertiesConfig {
|
||||
}
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
package com.nanri.aiimage.modules.evidence.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity;
|
||||
import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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 java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 后台「任务证据」读取面(AC-13):列表 / 内容 / 下载 / 删除,仅超级管理员。
|
||||
*
|
||||
* <p>证据件含账号可见的现场信息与窗口截图,访问面比常规后台更严;这里每个方法都走
|
||||
* 私有 {@link #requireSuperAdmin(HttpServletRequest)},与 AdminDeviceLogController 同口径。
|
||||
* 对象直链与预签名 URL 不入日志、不回前端——下载统一走服务端流。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/task-evidence")
|
||||
@Tag(name = "任务证据(后台)", description = "任务异常现场证据件的列表、内容、下载与删除(仅超管)。")
|
||||
public class AdminTaskEvidenceController {
|
||||
|
||||
private final TaskEvidenceService taskEvidenceService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@GetMapping("/files")
|
||||
@Operation(summary = "证据件分页列表", description = "按 taskId 关联查询;uid 只作为精确过滤条件。")
|
||||
public ApiResponse<Map<String, Object>> files(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "业务任务号") @RequestParam(required = false) String taskId,
|
||||
@Parameter(description = "归属用户 uid") @RequestParam(required = false) Long uid,
|
||||
@RequestParam(defaultValue = PageDefaults.FIRST_PAGE) Long page,
|
||||
@RequestParam(defaultValue = PageDefaults.ADMIN_LIST_PAGE_SIZE) Long pageSize) {
|
||||
AdminUserEntity operator = requireSuperAdmin(request);
|
||||
if (uid != null && !uid.equals(operator.getId())
|
||||
&& !DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator))) {
|
||||
throw new BusinessException(403, "无权读取其他用户的证据件");
|
||||
}
|
||||
return ApiResponse.success(taskEvidenceService.page(taskId, uid, page, pageSize));
|
||||
}
|
||||
|
||||
@GetMapping("/content")
|
||||
@Operation(summary = "查看证据件内容", description = "文本证据按 UTF-8 返回;图片证据返回 base64。")
|
||||
public ApiResponse<Map<String, Object>> content(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "证据主键") @RequestParam Long fileId) {
|
||||
requireSuperAdmin(request);
|
||||
return ApiResponse.success(taskEvidenceService.content(fileId));
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
@Operation(summary = "下载证据件", description = "服务端从独立证据桶流式读取,不返回直链。")
|
||||
public void download(HttpServletRequest request, HttpServletResponse response,
|
||||
@Parameter(description = "证据主键") @RequestParam Long fileId) throws IOException {
|
||||
requireSuperAdmin(request);
|
||||
TaskEvidenceEntity entity = taskEvidenceService.requireEvidence(fileId);
|
||||
byte[] bytes = taskEvidenceService.download(fileId);
|
||||
String name = "evidence-" + entity.getId()
|
||||
+ (entity.getObjectKey() != null && entity.getObjectKey().contains(".")
|
||||
? entity.getObjectKey().substring(entity.getObjectKey().lastIndexOf('.')) : ".bin");
|
||||
response.setContentType(entity.getContentType() == null
|
||||
? "application/octet-stream" : entity.getContentType());
|
||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"
|
||||
+ URLEncoder.encode(name, StandardCharsets.UTF_8).replace("+", "%20"));
|
||||
response.getOutputStream().write(bytes);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
@Operation(summary = "删除证据件", description = "先删对象再删登记行;不可恢复。")
|
||||
public ApiResponse<Map<String, Object>> delete(
|
||||
HttpServletRequest request,
|
||||
@RequestBody(required = false) List<Long> ids,
|
||||
@RequestParam(required = false) Long fileId) {
|
||||
requireSuperAdmin(request);
|
||||
List<Long> targets = new ArrayList<>();
|
||||
if (ids != null) {
|
||||
targets.addAll(ids);
|
||||
}
|
||||
if (fileId != null) {
|
||||
targets.add(fileId);
|
||||
}
|
||||
if (targets.isEmpty()) {
|
||||
throw new BusinessException("未指定要删除的证据");
|
||||
}
|
||||
int deleted = taskEvidenceService.delete(targets);
|
||||
return ApiResponse.success("已删除", Map.of("deleted", deleted));
|
||||
}
|
||||
|
||||
/**
|
||||
* 证据件仅超管可读:requireAdmin 不含超管判定,这里再判一层。
|
||||
* 返回操作者供列表端点做 uid 归属对撞。
|
||||
*/
|
||||
private AdminUserEntity requireSuperAdmin(HttpServletRequest request) {
|
||||
AdminUserEntity user = adminAuthSupport.requireAdmin(request);
|
||||
if (!DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(user))) {
|
||||
log.warn("[task-evidence] 非超管访问被拒 userId={} username={}",
|
||||
user.getId(), user.getUsername());
|
||||
throw new BusinessException(403, "仅超级管理员可访问任务证据");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.nanri.aiimage.modules.evidence.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.evidence.model.dto.TaskEvidenceRegisterRequest;
|
||||
import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService;
|
||||
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.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 内部接口:客户端证据件登记(AC-7)。客户端先把现场件分片上传到 Java 临时目录,
|
||||
* 上传完成后携带 X-Internal-Token 与 task_id/file_key 登记;服务端再把临时件搬进
|
||||
* 独立证据桶并落 biz_task_evidence。
|
||||
*
|
||||
* <p>/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller
|
||||
* 仍自校验令牌——防止配置漂移时匿名可达。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/internal/task-evidence")
|
||||
@Tag(name = "内部接口", description = "证据件登记(X-Internal-Token 鉴权)。")
|
||||
public class InternalTaskEvidenceController {
|
||||
|
||||
private final TaskEvidenceService taskEvidenceService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@PostMapping("/register")
|
||||
@Operation(summary = "登记任务证据件", description = "按 task_id 关联;file_key 指向已上传到临时目录的证据件。")
|
||||
public ApiResponse<Map<String, Object>> register(HttpServletRequest request,
|
||||
@RequestBody(required = false) TaskEvidenceRegisterRequest body) {
|
||||
if (!adminAuthSupport.isTrustedInternalToken(request)) {
|
||||
log.warn("[task-evidence] 拒绝未携带可信内部令牌的登记请求 remoteAddr={}",
|
||||
request.getRemoteAddr());
|
||||
throw new BusinessException(401, "未授权");
|
||||
}
|
||||
if (body == null) {
|
||||
throw new BusinessException("登记载荷为空");
|
||||
}
|
||||
Map<String, Object> result = taskEvidenceService.register(
|
||||
body.getTaskId(), body.getUid(), body.getEvidenceType(), body.getSource(), body.getFileKey());
|
||||
log.info("[task-evidence] registered id={} taskId={}", result.get("id"), body.getTaskId());
|
||||
return ApiResponse.success(result);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.nanri.aiimage.modules.evidence.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity;
|
||||
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;
|
||||
|
||||
@Mapper
|
||||
public interface TaskEvidenceMapper extends BaseMapper<TaskEvidenceEntity> {
|
||||
|
||||
/** 回收批次的 key 清单;先删对象再删行,避免对象清不掉而登记行先消失。 */
|
||||
@Select("SELECT * FROM biz_task_evidence WHERE created_at < #{expireBefore} ORDER BY id LIMIT #{limit}")
|
||||
List<TaskEvidenceEntity> selectExpired(@Param("expireBefore") LocalDateTime expireBefore,
|
||||
@Param("limit") int limit);
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.nanri.aiimage.modules.evidence.model.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 客户端证据登记载荷(snake_case 与客户端现场件字段同口径)。
|
||||
*/
|
||||
@Data
|
||||
public class TaskEvidenceRegisterRequest {
|
||||
|
||||
@JsonProperty("task_id")
|
||||
private String taskId;
|
||||
|
||||
@JsonProperty("file_key")
|
||||
private String fileKey;
|
||||
|
||||
@JsonProperty("evidence_type")
|
||||
private String evidenceType;
|
||||
|
||||
private String source;
|
||||
|
||||
private Long uid;
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.nanri.aiimage.modules.evidence.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 任务异常现场证据件登记(表 biz_task_evidence,V182)。
|
||||
*
|
||||
* <p>只登记归属与对象元数据;对象本体在独立证据桶,直链/预签名 URL 不入库、不入日志。</p>
|
||||
*/
|
||||
@Data
|
||||
@TableName("biz_task_evidence")
|
||||
public class TaskEvidenceEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 业务任务号(客户端字符串口径,允许非数字)。 */
|
||||
private String taskId;
|
||||
|
||||
/** 任务归属用户;未知时为空。 */
|
||||
private Long uid;
|
||||
|
||||
/** 证据类型:client/crash/brand 等。 */
|
||||
private String evidenceType;
|
||||
|
||||
/** 来源入口(崩溃记录器/商品采集等)。 */
|
||||
private String source;
|
||||
|
||||
/** 证据桶内对象 key(含 UUID)。 */
|
||||
private String objectKey;
|
||||
|
||||
private Long byteSize;
|
||||
|
||||
private String contentType;
|
||||
|
||||
/** 对象 sha256(防篡改/去重)。 */
|
||||
private String sha256;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package com.nanri.aiimage.modules.evidence.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.config.EvidenceProperties;
|
||||
import com.nanri.aiimage.modules.evidence.mapper.TaskEvidenceMapper;
|
||||
import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity;
|
||||
import com.nanri.aiimage.modules.evidence.storage.TaskEvidenceStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 证据登记与读取(AC-7/AC-13)。
|
||||
*
|
||||
* <p>登记走「客户端先分片上云到 Java 临时目录,再由服务端搬进独立证据桶」:客户端不持有
|
||||
* 任何对象存储凭据,object_key/sha256/size 全部由服务端生成,登记接口不接受直链。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskEvidenceService {
|
||||
|
||||
private static final int MAX_TASK_ID_LENGTH = 64;
|
||||
private static final int MAX_CONTENT_BYTES = 8 * 1024 * 1024;
|
||||
private static final int RETENTION_BATCH = 200;
|
||||
|
||||
private final TaskEvidenceMapper mapper;
|
||||
private final TaskEvidenceStorageService storage;
|
||||
private final LocalFileStorageService localFileStorageService;
|
||||
private final EvidenceProperties properties;
|
||||
|
||||
/** 客户端上传完成后登记:fileKey 指向 Java 临时目录里的分片拼装件。 */
|
||||
public Map<String, Object> register(String taskId, Long uid, String evidenceType,
|
||||
String source, String fileKey) {
|
||||
String normalizedTaskId = normalizeTaskId(taskId);
|
||||
if (fileKey == null || fileKey.isBlank()) {
|
||||
throw new BusinessException("证据文件标识为空");
|
||||
}
|
||||
File file = localFileStorageService.findLocalSourceFile(fileKey.trim());
|
||||
if (file == null || !file.isFile()) {
|
||||
throw new BusinessException("证据文件不存在或已过期");
|
||||
}
|
||||
TaskEvidenceStorageService.StoredEvidence stored;
|
||||
try {
|
||||
stored = storage.store(file, normalizedTaskId, file.getName());
|
||||
} catch (IOException | RuntimeException ex) {
|
||||
log.warn("[task-evidence] register failed taskId={} reason={}",
|
||||
normalizedTaskId, ex.getMessage());
|
||||
throw new BusinessException("证据件入库失败,请稍后重试");
|
||||
}
|
||||
TaskEvidenceEntity entity = new TaskEvidenceEntity();
|
||||
entity.setTaskId(normalizedTaskId);
|
||||
entity.setUid(uid);
|
||||
entity.setEvidenceType(normalizeLabel(evidenceType, "client"));
|
||||
entity.setSource(normalizeLabel(source, "client"));
|
||||
entity.setObjectKey(stored.objectKey());
|
||||
entity.setByteSize(stored.byteSize());
|
||||
entity.setContentType(stored.contentType());
|
||||
entity.setSha256(stored.sha256());
|
||||
entity.setCreatedAt(LocalDateTime.now());
|
||||
mapper.insert(entity);
|
||||
deleteTempQuietly(file);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("id", entity.getId());
|
||||
result.put("objectKey", entity.getObjectKey());
|
||||
result.put("byteSize", entity.getByteSize());
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> page(String taskId, Long uid, long page, long pageSize) {
|
||||
long safePage = Math.max(1, page);
|
||||
long safeSize = Math.min(Math.max(1, pageSize), 200);
|
||||
LambdaQueryWrapper<TaskEvidenceEntity> query = new LambdaQueryWrapper<>();
|
||||
if (taskId != null && !taskId.isBlank()) {
|
||||
query.eq(TaskEvidenceEntity::getTaskId, taskId.trim());
|
||||
}
|
||||
if (uid != null) {
|
||||
query.eq(TaskEvidenceEntity::getUid, uid);
|
||||
}
|
||||
query.orderByDesc(TaskEvidenceEntity::getId);
|
||||
Page<TaskEvidenceEntity> result = mapper.selectPage(new Page<>(safePage, safeSize), query);
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("items", result.getRecords());
|
||||
data.put("total", result.getTotal());
|
||||
data.put("page", safePage);
|
||||
data.put("pageSize", safeSize);
|
||||
return data;
|
||||
}
|
||||
|
||||
public TaskEvidenceEntity requireEvidence(Long fileId) {
|
||||
if (fileId == null) {
|
||||
throw new BusinessException("证据主键为空");
|
||||
}
|
||||
TaskEvidenceEntity entity = mapper.selectById(fileId);
|
||||
if (entity == null) {
|
||||
throw new BusinessException("证据不存在或已过期");
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Map<String, Object> content(Long fileId) {
|
||||
TaskEvidenceEntity entity = requireEvidence(fileId);
|
||||
byte[] bytes = storage.read(entity.getObjectKey(), MAX_CONTENT_BYTES);
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("id", entity.getId());
|
||||
data.put("taskId", entity.getTaskId());
|
||||
data.put("contentType", entity.getContentType());
|
||||
boolean binary = entity.getContentType() != null && entity.getContentType().startsWith("image/");
|
||||
data.put("binary", binary);
|
||||
data.put("content", binary
|
||||
? java.util.Base64.getEncoder().encodeToString(bytes)
|
||||
: new String(bytes, java.nio.charset.StandardCharsets.UTF_8));
|
||||
return data;
|
||||
}
|
||||
|
||||
public byte[] download(Long fileId) {
|
||||
TaskEvidenceEntity entity = requireEvidence(fileId);
|
||||
return storage.read(entity.getObjectKey(), properties.maxFileBytesOrDefault());
|
||||
}
|
||||
|
||||
/** 删除证据:先删对象再删登记行;对象删除失败不吞(留在库里下一轮可重试)。 */
|
||||
public int delete(List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
int deleted = 0;
|
||||
for (Long id : ids) {
|
||||
TaskEvidenceEntity entity = id == null ? null : mapper.selectById(id);
|
||||
if (entity == null) {
|
||||
continue;
|
||||
}
|
||||
storage.delete(entity.getObjectKey());
|
||||
deleted += mapper.deleteById(id);
|
||||
}
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/** 保留期回收:按天清扫到期行与对象(AC-13 的保留期必须有执行者,不能只写在配置里)。 */
|
||||
@Scheduled(cron = "${aiimage.evidence.retention-cron:0 30 3 * * *}")
|
||||
public void purgeExpired() {
|
||||
LocalDateTime expireBefore = LocalDateTime.now().minusDays(properties.retentionDaysOrDefault());
|
||||
while (true) {
|
||||
List<TaskEvidenceEntity> batch = mapper.selectExpired(expireBefore, RETENTION_BATCH);
|
||||
if (batch.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int removed = 0;
|
||||
for (TaskEvidenceEntity entity : batch) {
|
||||
try {
|
||||
storage.delete(entity.getObjectKey());
|
||||
removed += mapper.deleteById(entity.getId());
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[task-evidence] retention delete failed id={} reason={}",
|
||||
entity.getId(), ex.getMessage());
|
||||
}
|
||||
}
|
||||
if (removed == 0) {
|
||||
return;
|
||||
}
|
||||
log.info("[task-evidence] retention purge removed={} before={}", removed, expireBefore);
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeTaskId(String taskId) {
|
||||
String value = taskId == null ? "" : taskId.trim();
|
||||
if (value.isEmpty()) {
|
||||
throw new BusinessException("任务号为空");
|
||||
}
|
||||
if (value.length() > MAX_TASK_ID_LENGTH) {
|
||||
value = value.substring(0, MAX_TASK_ID_LENGTH);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String normalizeLabel(String value, String fallback) {
|
||||
String text = value == null ? "" : value.trim();
|
||||
if (text.isEmpty()) {
|
||||
return fallback;
|
||||
}
|
||||
return text.length() > 64 ? text.substring(0, 64) : text;
|
||||
}
|
||||
|
||||
private void deleteTempQuietly(File file) {
|
||||
try {
|
||||
Files.deleteIfExists(file.toPath());
|
||||
} catch (IOException ex) {
|
||||
log.warn("[task-evidence] temp evidence cleanup failed name={}", file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package com.nanri.aiimage.modules.evidence.storage;
|
||||
|
||||
import com.nanri.aiimage.config.EvidenceProperties;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 证据对象的独立落点(AC-13):桶与 key 前缀来自 {@link EvidenceProperties},
|
||||
* 对象本体不经过业务桶,也不把直链/预签名 URL 写进任何日志或数据库。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskEvidenceStorageService {
|
||||
|
||||
private static final Pattern TASK_ID_SAFE = Pattern.compile("[^0-9A-Za-z_.-]+");
|
||||
private static final Pattern EXT_SAFE = Pattern.compile("\\.[0-9A-Za-z]{1,10}");
|
||||
|
||||
private final EvidenceProperties properties;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
private volatile boolean bucketReady;
|
||||
|
||||
/** 落一份证据对象,返回对象 key 与对象元数据。 */
|
||||
public StoredEvidence store(File source, String taskId, String originalFilename) throws IOException {
|
||||
if (source == null || !source.isFile()) {
|
||||
throw new IllegalArgumentException("evidence file must exist");
|
||||
}
|
||||
long size = source.length();
|
||||
if (size > properties.maxFileBytesOrDefault()) {
|
||||
throw new IllegalArgumentException("evidence file too large");
|
||||
}
|
||||
String bucket = properties.bucketOrDefault();
|
||||
ensureBucket(bucket);
|
||||
String key = objectKey(taskId, originalFilename);
|
||||
ossStorageService.uploadFileToBucket(source, bucket, key);
|
||||
return new StoredEvidence(key, size, sha256(source), contentType(originalFilename));
|
||||
}
|
||||
|
||||
public byte[] read(String objectKey, long maxBytes) {
|
||||
return ossStorageService.readObjectBytes(
|
||||
properties.bucketOrDefault(), objectKey, maxBytes);
|
||||
}
|
||||
|
||||
public void delete(String objectKey) {
|
||||
if (objectKey == null || objectKey.isBlank()) {
|
||||
return;
|
||||
}
|
||||
ossStorageService.removeObject(properties.bucketOrDefault(), objectKey);
|
||||
}
|
||||
|
||||
public String bucket() {
|
||||
return properties.bucketOrDefault();
|
||||
}
|
||||
|
||||
private void ensureBucket(String bucket) {
|
||||
if (bucketReady) {
|
||||
return;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (bucketReady) {
|
||||
return;
|
||||
}
|
||||
ossStorageService.ensureBucketExists(bucket);
|
||||
bucketReady = true;
|
||||
log.info("[task-evidence] evidence bucket ready bucket={}", bucket);
|
||||
}
|
||||
}
|
||||
|
||||
private String objectKey(String taskId, String originalFilename) {
|
||||
String safeTask = TASK_ID_SAFE.matcher(taskId == null ? "" : taskId.trim())
|
||||
.replaceAll("_");
|
||||
if (safeTask.isEmpty()) {
|
||||
safeTask = "unknown";
|
||||
}
|
||||
return properties.keyPrefixOrDefault() + safeTask + "/" + UUID.randomUUID() + extension(originalFilename);
|
||||
}
|
||||
|
||||
private String extension(String originalFilename) {
|
||||
String name = originalFilename == null ? "" : originalFilename.trim();
|
||||
int slash = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\'));
|
||||
if (slash >= 0) {
|
||||
name = name.substring(slash + 1);
|
||||
}
|
||||
int dot = name.lastIndexOf('.');
|
||||
String ext = dot < 0 ? "" : name.substring(dot).toLowerCase(Locale.ROOT);
|
||||
return EXT_SAFE.matcher(ext).matches() ? ext : ".bin";
|
||||
}
|
||||
|
||||
private String contentType(String originalFilename) {
|
||||
String ext = extension(originalFilename);
|
||||
return switch (ext) {
|
||||
case ".json" -> "application/json";
|
||||
case ".png" -> "image/png";
|
||||
case ".jpg", ".jpeg" -> "image/jpeg";
|
||||
case ".webp" -> "image/webp";
|
||||
case ".txt", ".log" -> "text/plain; charset=utf-8";
|
||||
default -> "application/octet-stream";
|
||||
};
|
||||
}
|
||||
|
||||
private String sha256(File source) throws IOException {
|
||||
try (InputStream in = Files.newInputStream(source.toPath())) {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] buffer = new byte[1 << 16];
|
||||
int read;
|
||||
while ((read = in.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new IllegalStateException("SHA-256 not available", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public record StoredEvidence(String objectKey, long byteSize, String sha256, String contentType) {
|
||||
}
|
||||
}
|
||||
+15
@@ -9,6 +9,7 @@ import com.nanri.aiimage.config.StorageProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -275,6 +276,20 @@ public class ChunkedUploadService {
|
||||
return sessionLocks.computeIfAbsent(uploadId, key -> new Object());
|
||||
}
|
||||
|
||||
/**
|
||||
* AC-10(v1.2 Phase 4):stale 分片会话的周期回收。
|
||||
*
|
||||
* <p>此前唯一调用点在 {@link #init},断掉的上传不 init 就永久占盘;清扫间隔与
|
||||
* {@code stale-hours} 同一口径,清理阈值仍由 {@code stale-hours} 决定。</p>
|
||||
*/
|
||||
@Scheduled(cron = "${aiimage.file.chunk-upload.cleanup-cron:0 15 * * * *}")
|
||||
public void scheduledCleanupStaleSessions() {
|
||||
if (!storageProperties.isCleanupEnabled()) {
|
||||
return;
|
||||
}
|
||||
cleanupStaleSessions();
|
||||
}
|
||||
|
||||
private void cleanupStaleSessions() {
|
||||
Path root = chunkRoot();
|
||||
if (!Files.isDirectory(root)) {
|
||||
|
||||
+26
@@ -22,6 +22,8 @@ public class LocalTempCleanupService {
|
||||
|
||||
private static final Pattern ROOT_TEMP_FILE_PATTERN = Pattern.compile("^[a-fA-F0-9]{32}(\\.[^.]+)?$");
|
||||
private static final String TRANSIENT_PAYLOAD_DIR_NAME = "transient-payload";
|
||||
/** 分片上传会话目录(AC-10):断掉的上传没有业务结果目录条目,统一清理必须覆盖它。 */
|
||||
private static final String CHUNK_UPLOAD_DIR_NAME = "chunk-uploads";
|
||||
private static final String CLEANUP_FAILED_METRIC = "aiimage.temp-cleanup.failed";
|
||||
private static final List<String> RESULT_DIR_NAMES = List.of(
|
||||
"dedupe-result",
|
||||
@@ -83,6 +85,12 @@ public class LocalTempCleanupService {
|
||||
if (child.isDirectory() && TRANSIENT_PAYLOAD_DIR_NAME.equals(child.getName())) {
|
||||
deletedTransientPayloadCount += deleteExpiredChildrenRecursively(child, transientPayloadExpireBefore);
|
||||
deleteEmptyDirectories(child, tempDir);
|
||||
continue;
|
||||
}
|
||||
if (child.isDirectory() && CHUNK_UPLOAD_DIR_NAME.equals(child.getName())) {
|
||||
deletedSourceCount += deleteExpiredChildrenRecursively(child, sourceExpireBefore);
|
||||
// 删片会刷新会话目录 mtime,按"过期"再判目录会留下空壳;这里按空目录自底向上收口。
|
||||
deleteEmptyDirectoriesDeep(child, tempDir);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
handleCleanupFailure(child, ex);
|
||||
@@ -128,6 +136,24 @@ public class LocalTempCleanupService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 自底向上删空目录(不看 mtime):分片会话目录删片后会刷新父目录时间,按龄判断会留空壳。 */
|
||||
private void deleteEmptyDirectoriesDeep(File directory, File stopAt) {
|
||||
if (directory == null || directory.equals(stopAt) || !directory.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
File[] children = directory.listFiles();
|
||||
if (children != null) {
|
||||
for (File child : children) {
|
||||
if (child.isDirectory() && PathSafetyGuard.isInside(directory, child)) {
|
||||
deleteEmptyDirectoriesDeep(child, stopAt);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isDirectoryEmpty(directory)) {
|
||||
FileUtil.del(directory);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isManagedRootTempFile(File file) {
|
||||
return ROOT_TEMP_FILE_PATTERN.matcher(file.getName()).matches();
|
||||
}
|
||||
|
||||
+31
@@ -286,6 +286,13 @@ public class OssStorageService {
|
||||
removePublicClientObject(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
}
|
||||
|
||||
/** 服务端把教程包搬进公开 client 桶(分片通道确认用;不经浏览器直传)。 */
|
||||
public String uploadTutorialPackage(File file, String objectKey) {
|
||||
requireTutorialKey(objectKey);
|
||||
uploadFile(file, publicClientBucket(), objectKey);
|
||||
return getTutorialDownloadUrl(objectKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为教程安装包签发浏览器直传用的预签名 PUT URL(公开 client 桶 tutorial/ 前缀)。
|
||||
* 签名 host 同 {@link #presignSoftwareVersionUpload}:必须是公网入口,不能用内网 client。
|
||||
@@ -478,6 +485,30 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把本地文件流式上传到指定桶/对象键(证据件等独立桶使用)。
|
||||
* 与 {@link #uploadBytes} 同一归属:桶名与 key 由服务端生成,不接受用户文件名拼路径。
|
||||
*/
|
||||
public void uploadFileToBucket(File file, String bucket, String objectKey) {
|
||||
String normalizedBucket = requireStorageName(bucket, "bucket");
|
||||
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
|
||||
uploadFile(file, normalizedBucket, normalizedObjectKey);
|
||||
}
|
||||
|
||||
/** 删除指定桶内的对象(证据件回收用);对象不存在不抛。 */
|
||||
public void removeObject(String bucket, String objectKey) {
|
||||
String normalizedBucket = requireStorageName(bucket, "bucket");
|
||||
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
|
||||
try {
|
||||
buildClient().removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(normalizedBucket)
|
||||
.object(normalizedObjectKey)
|
||||
.build());
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("delete", normalizedBucket + "/" + normalizedObjectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] readObjectBytes(String bucket, String objectKey) {
|
||||
// 防御性上限:防止被误用到大对象时一次性读入内存导致 OOM(当前仅模板下载场景,模板为 xlsx KB 级)
|
||||
return readObjectBytes(bucket, objectKey, DEFAULT_READ_MAX_BYTES);
|
||||
|
||||
+7
-3
@@ -70,10 +70,14 @@ public class SoftwareVersionAdminController {
|
||||
@Operation(summary = "确认软件版本直传完成", description = "服务端核对 client 桶对象存在与大小并写 web_config,返回新版本行")
|
||||
public ApiResponse<Map<String, Object>> confirmUploadVersion(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "版本号,如 3.0.67") @RequestParam(value = "version", required = false) String version) {
|
||||
@Parameter(description = "版本号,如 3.0.67") @RequestParam(value = "version", required = false) String version,
|
||||
@Parameter(description = "分片通道拼装件 fileKey(>8MB 大档用;直传路径不传)")
|
||||
@RequestParam(value = "file_key", required = false) String fileKey) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[software-version] 管理端确认版本直传完成 operator={} version={}", operator.getUsername(), version);
|
||||
return ApiResponse.success("上传成功", Map.of("item", softwareVersionService.confirmSoftwareVersion(version)));
|
||||
log.info("[software-version] 管理端确认版本上传完成 operator={} version={} chunked={}",
|
||||
operator.getUsername(), version, fileKey != null && !fileKey.isBlank());
|
||||
return ApiResponse.success("上传成功",
|
||||
Map.of("item", softwareVersionService.confirmSoftwareVersion(version, fileKey)));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/version/delete")
|
||||
|
||||
+57
@@ -3,12 +3,14 @@ package com.nanri.aiimage.modules.softwareversion.service;
|
||||
import com.nanri.aiimage.common.text.UserMessages;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.softwareversion.mapper.SoftwareVersionMapper;
|
||||
import com.nanri.aiimage.modules.softwareversion.model.entity.SoftwareVersionEntity;
|
||||
import com.nanri.aiimage.modules.softwareversion.service.support.PublicVersionLookup;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -57,6 +59,10 @@ public class SoftwareVersionService {
|
||||
private final SoftwareVersionMapper softwareVersionMapper;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
/** 分片通道确认用;纯单元测试可缺省(只走预签名直传分支时不需要它)。 */
|
||||
@Autowired(required = false)
|
||||
private LocalFileStorageService localFileStorageService;
|
||||
|
||||
/**
|
||||
* 版本列表:web_config 全量按创建时间倒序,与 Flask 返回结构一致
|
||||
* (items 内为 id / version / file_url / created_at)。
|
||||
@@ -239,6 +245,57 @@ public class SoftwareVersionService {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端搬进 client 桶后落库。
|
||||
* 与预签名直传共用同一个对象 key 规则,客户端不接触任何对象存储凭据。
|
||||
*/
|
||||
public Map<String, Object> confirmSoftwareVersion(String version, String fileKey) {
|
||||
if (fileKey == null || fileKey.isBlank()) {
|
||||
return confirmSoftwareVersion(version);
|
||||
}
|
||||
if (localFileStorageService == null) {
|
||||
throw new BusinessException("分片上传通道未就绪,请稍后重试");
|
||||
}
|
||||
String normalizedVersion = requireVersion(version);
|
||||
File source = localFileStorageService.findLocalSourceFile(fileKey.trim());
|
||||
if (source == null || !source.isFile()) {
|
||||
throw new BusinessException("上传的版本包不存在或已过期,请重新上传");
|
||||
}
|
||||
if (!source.getName().toLowerCase(Locale.ROOT).endsWith(".zip")) {
|
||||
throw new BusinessException("仅支持 .zip 格式");
|
||||
}
|
||||
long size = source.length();
|
||||
if (size <= 0) {
|
||||
throw new BusinessException("文件为空");
|
||||
}
|
||||
if (size > MAX_UPLOAD_BYTES) {
|
||||
throw new BusinessException("文件超过允许的大小限制");
|
||||
}
|
||||
String objectKey = softwareVersionObjectKey(normalizedVersion);
|
||||
String fileUrl = ossStorageService.uploadSoftwareVersionPackage(source, objectKey);
|
||||
SoftwareVersionEntity entity = new SoftwareVersionEntity();
|
||||
entity.setVersion(normalizedVersion);
|
||||
entity.setFileUrl(fileUrl);
|
||||
try {
|
||||
entity.setSha256(sha256Hex(source));
|
||||
} catch (IOException ex) {
|
||||
throw new BusinessException("版本包校验失败,请重新上传");
|
||||
}
|
||||
softwareVersionMapper.insert(entity);
|
||||
softwareVersionMapper.markLatestById(entity.getId());
|
||||
log.info("[software-version] 分片拼装件登记成功 id={} version={} bytes={}",
|
||||
entity.getId(), normalizedVersion, size);
|
||||
deleteTempQuietly(source);
|
||||
SoftwareVersionEntity saved = softwareVersionMapper.selectById(entity.getId());
|
||||
return toItemMap(saved == null ? entity : saved);
|
||||
}
|
||||
|
||||
private void deleteTempQuietly(File file) {
|
||||
if (file != null && file.exists() && !file.delete()) {
|
||||
log.warn("[software-version] 清理分片临时件失败 path={}", file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
/** 直传完成后确认落库:服务端重算 key、校验对象存在且不超限,写 web_config 并返回新行。 */
|
||||
public Map<String, Object> confirmSoftwareVersion(String version) {
|
||||
String normalizedVersion = requireVersion(version);
|
||||
|
||||
+7
-4
@@ -60,11 +60,14 @@ public class TutorialAdminController {
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "预签名返回的对象 key") @RequestParam(value = "object_key", required = false) String objectKey,
|
||||
@Parameter(description = "上传的 zip 文件名") @RequestParam(value = "file_name", required = false) String fileName,
|
||||
@Parameter(description = "教程包版本号(展示与排序用,可空)") @RequestParam(value = "version", required = false) String version) {
|
||||
@Parameter(description = "教程包版本号(展示与排序用,可空)") @RequestParam(value = "version", required = false) String version,
|
||||
@Parameter(description = "分片通道拼装件 fileKey(>8MB 大档用;直传路径不传)")
|
||||
@RequestParam(value = "file_key", required = false) String fileKey) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[tutorial] 管理端确认教程包直传完成 operator={} objectKey={} fileName={} version={}",
|
||||
operator.getUsername(), objectKey, fileName, version);
|
||||
return ApiResponse.success("上传成功", Map.of("item", tutorialPackageService.confirmTutorialPackage(objectKey, fileName, version)));
|
||||
log.info("[tutorial] 管理端确认教程包上传完成 operator={} fileName={} version={} chunked={}",
|
||||
operator.getUsername(), fileName, version, fileKey != null && !fileKey.isBlank());
|
||||
return ApiResponse.success("上传成功",
|
||||
Map.of("item", tutorialPackageService.confirmTutorialPackage(objectKey, fileName, version, fileKey)));
|
||||
}
|
||||
|
||||
@PostMapping("/tutorial/delete")
|
||||
|
||||
+49
@@ -2,11 +2,13 @@ package com.nanri.aiimage.modules.tutorial.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.tutorial.mapper.TutorialPackageMapper;
|
||||
import com.nanri.aiimage.modules.tutorial.model.entity.TutorialPackageEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -54,6 +56,10 @@ public class TutorialPackageService {
|
||||
private final TutorialPackageMapper tutorialPackageMapper;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
/** 分片通道确认用;纯单元测试可缺省(只走预签名直传分支时不需要它)。 */
|
||||
@Autowired(required = false)
|
||||
private LocalFileStorageService localFileStorageService;
|
||||
|
||||
/** 教程包列表:按上传时间倒序(第一条即工具台下载的"最新")。 */
|
||||
public List<Map<String, Object>> listTutorialPackages() {
|
||||
List<TutorialPackageEntity> entities = tutorialPackageMapper.selectList(
|
||||
@@ -117,6 +123,49 @@ public class TutorialPackageService {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端生成对象 key、
|
||||
* 搬进 client 桶 tutorial/ 前缀后落库。客户端不接触任何对象存储凭据。
|
||||
*/
|
||||
public Map<String, Object> confirmTutorialPackage(String objectKey, String fileName, String version,
|
||||
String fileKey) {
|
||||
if (fileKey == null || fileKey.isBlank()) {
|
||||
return confirmTutorialPackage(objectKey, fileName, version);
|
||||
}
|
||||
if (localFileStorageService == null) {
|
||||
throw new BusinessException("分片上传通道未就绪,请稍后重试");
|
||||
}
|
||||
var source = localFileStorageService.findLocalSourceFile(fileKey.trim());
|
||||
if (source == null || !source.isFile()) {
|
||||
throw new BusinessException("上传的教程包不存在或已过期,请重新上传");
|
||||
}
|
||||
String normalizedName = requireZipFileName(fileName);
|
||||
String normalizedVersion = normalizeVersion(version);
|
||||
long size = source.length();
|
||||
if (size <= 0) {
|
||||
throw new BusinessException("文件为空");
|
||||
}
|
||||
if (size > MAX_UPLOAD_BYTES) {
|
||||
throw new BusinessException("文件超过允许的大小限制");
|
||||
}
|
||||
String generatedKey = OBJECT_KEY_PREFIX + OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
|
||||
+ "-" + safeFileName(normalizedName);
|
||||
ossStorageService.uploadTutorialPackage(source, generatedKey);
|
||||
TutorialPackageEntity entity = new TutorialPackageEntity();
|
||||
entity.setFileName(normalizedName);
|
||||
entity.setVersion(normalizedVersion);
|
||||
entity.setObjectKey(generatedKey);
|
||||
entity.setFileSize(size);
|
||||
tutorialPackageMapper.insert(entity);
|
||||
log.info("[tutorial] 分片拼装件登记成功 id={} fileName={} version={} bytes={}",
|
||||
entity.getId(), normalizedName, normalizedVersion, size);
|
||||
if (source.exists() && !source.delete()) {
|
||||
log.warn("[tutorial] 清理分片临时件失败 path={}", source.getAbsolutePath());
|
||||
}
|
||||
TutorialPackageEntity saved = tutorialPackageMapper.selectById(entity.getId());
|
||||
return toItemMap(saved == null ? entity : saved);
|
||||
}
|
||||
|
||||
/** 直传完成后确认落库:校验对象存在且不超限,新 key 不与其它记录冲突,写入记录并返回新行。 */
|
||||
public Map<String, Object> confirmTutorialPackage(String objectKey, String fileName, String version) {
|
||||
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
|
||||
|
||||
@@ -36,6 +36,11 @@ AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET=nanri-ai-digital-human
|
||||
AIIMAGE_OSS_ACCESS_KEY_ID=change-me
|
||||
AIIMAGE_OSS_ACCESS_KEY_SECRET=change-me
|
||||
|
||||
# 任务异常现场证据件(v1.2 Phase 3):独立桶与保留期必须显式配置;桶不得复用 nanri-ai-images。
|
||||
AIIMAGE_EVIDENCE_OSS_BUCKET=task-evidence
|
||||
AIIMAGE_EVIDENCE_RETENTION_DAYS=7
|
||||
AIIMAGE_EVIDENCE_KEY_PREFIX=task-evidence/
|
||||
|
||||
# 工作台客户端包直传预签名有效期(秒);90MB 级包在慢公网入口需要更长窗口。
|
||||
AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS=3600
|
||||
|
||||
|
||||
@@ -153,8 +153,9 @@ aiimage:
|
||||
digital-human-bucket: ${AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET:nanri-ai-digital-human}
|
||||
template-bucket: ${AIIMAGE_TEMPLATE_OSS_BUCKET:aiimage-templates}
|
||||
software-version-bucket: ${AIIMAGE_OSS_SOFTWARE_VERSION_BUCKET:client}
|
||||
# 店铺数据采集累计档独立桶:默认桶 nanri-ai-images 带 30 天全桶过期规则,
|
||||
# 而店铺数据记录约定「无更新则一直保留、只准人工删」,故单独成桶且不设过期规则。
|
||||
# 店铺数据采集累计档独立桶:默认桶 nanri-ai-images 带 7 天整桶过期规则(铁律 17,
|
||||
# 2026-10-26 产品确认),而店铺数据记录约定「无更新则一直保留、只准人工删」,
|
||||
# 故单独成桶且不设过期规则。
|
||||
shop-data-bucket: ${AIIMAGE_OSS_SHOP_DATA_BUCKET:shufu-shop-data}
|
||||
access-key-id: ${AIIMAGE_OSS_ACCESS_KEY_ID:}
|
||||
access-key-secret: ${AIIMAGE_OSS_ACCESS_KEY_SECRET:}
|
||||
@@ -167,6 +168,15 @@ aiimage:
|
||||
access-key-id: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_ID:}
|
||||
access-key-secret: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_SECRET:}
|
||||
retention-days: ${AIIMAGE_DEVICE_LOG_RETENTION_DAYS:7}
|
||||
# 任务异常现场证据件(v1.2 Phase 3/AC-7/AC-13):独立桶 + 独立保留期。
|
||||
# 桶必须与业务桶 nanri-ai-images 隔离(那边是 7 天整桶 ILM,证据保留策略不得被它绑死);
|
||||
# key-prefix 之后实际 key = <prefix><taskId>/<UUID><扩展名>(铁律 10:key 必带 UUID)。
|
||||
evidence:
|
||||
bucket: ${AIIMAGE_EVIDENCE_OSS_BUCKET:task-evidence}
|
||||
retention-days: ${AIIMAGE_EVIDENCE_RETENTION_DAYS:7}
|
||||
key-prefix: ${AIIMAGE_EVIDENCE_KEY_PREFIX:task-evidence/}
|
||||
max-file-bytes: ${AIIMAGE_EVIDENCE_MAX_FILE_BYTES:67108864}
|
||||
retention-cron: ${AIIMAGE_EVIDENCE_RETENTION_CRON:0 30 3 * * *}
|
||||
transient-storage:
|
||||
enabled: ${AIIMAGE_TRANSIENT_STORAGE_ENABLED:true}
|
||||
endpoint: ${AIIMAGE_TRANSIENT_STORAGE_ENDPOINT:http://121.196.149.225:9000}
|
||||
@@ -224,6 +234,8 @@ aiimage:
|
||||
max-parts: ${AIIMAGE_FILE_CHUNK_UPLOAD_MAX_PARTS:4096}
|
||||
# 未完成会话与残留片的驻留小时数:超时由清理动作回收,避免断掉的上传永久占盘
|
||||
stale-hours: ${AIIMAGE_FILE_CHUNK_UPLOAD_STALE_HOURS:24}
|
||||
# 周期回收 cron(AC-10):断掉的上传不会再 init,不能只靠 init 时顺手清
|
||||
cleanup-cron: ${AIIMAGE_FILE_CHUNK_UPLOAD_CLEANUP_CRON:0 15 * * * *}
|
||||
temp-dir:
|
||||
retention-hours: ${AIIMAGE_TEMP_DIR_RETENTION_HOURS:24}
|
||||
capacity-plan:
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
-- V182: 任务异常现场证据件(AC-7/AC-13,v1.2 Phase 3)
|
||||
--
|
||||
-- 背景:客户端在任务异常时会把现场件(JSON 快照+窗口/页面截图)上传到独立证据桶;
|
||||
-- 本表只登记「哪份证据属于哪个任务」,对象本体留在对象存储,按 retention-days 单独回收。
|
||||
--
|
||||
-- 设计口径(蓝图 §3.3):
|
||||
-- * 按 task_id 关联但**不建外键**(全库唯一 FK 是 V80 的 columns.parent_id);
|
||||
-- * 与 device_log_file(无 task_id/content_type)和 biz_file_result(业务交付物语义)分表,
|
||||
-- 避免把文本日志通道或业务交付物的保留策略绑死;
|
||||
-- * object_key 带 UUID(铁律 10),库内只存 key,不存任何直链/预签名 URL(AC-13)。
|
||||
--
|
||||
-- 回滚(手工):
|
||||
-- DROP TABLE `biz_task_evidence`;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `biz_task_evidence` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`task_id` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '业务任务号(客户端字符串口径)',
|
||||
`uid` BIGINT NULL COMMENT '任务归属用户(未知时为空)',
|
||||
`evidence_type` VARCHAR(32) NOT NULL DEFAULT 'client' COMMENT '证据类型:client/crash/brand 等',
|
||||
`source` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '来源入口(崩溃记录器/商品采集等)',
|
||||
`object_key` VARCHAR(512) NOT NULL COMMENT '证据桶内对象 key(含 UUID)',
|
||||
`byte_size` BIGINT NOT NULL DEFAULT 0 COMMENT '对象字节数',
|
||||
`content_type` VARCHAR(128) NOT NULL DEFAULT 'application/octet-stream' COMMENT '对象媒体类型',
|
||||
`sha256` CHAR(64) NOT NULL DEFAULT '' COMMENT '对象 sha256(防篡改/去重)',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '登记时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY idx_task_evidence_task_id (task_id, created_at),
|
||||
KEY idx_task_evidence_uid (uid)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='任务异常现场证据件登记';
|
||||
Reference in New Issue
Block a user