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:
2026-10-31 03:45:50 +08:00
parent 5c283a79fc
commit 6fe5c8c8d0
30 changed files with 1016 additions and 54 deletions
@@ -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)) {
@@ -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();
}
@@ -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);