fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
+20
-6
@@ -51,13 +51,13 @@ public class ChunkedUploadService {
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
private static final String META_NAME = "meta.json";
|
||||
|
||||
/** 单个分片上限:比前端 16MB 分片留足余量,防一次请求把临时盘打满。 */
|
||||
/** 单个分片上限:两端与客户端实际片大小均为 8MB(各自独立构建根,无共享常量),这里留 8 倍余量。 */
|
||||
private static final long MAX_PART_BYTES = 64L * 1024 * 1024;
|
||||
|
||||
private final StorageProperties storageProperties;
|
||||
|
||||
/** 同一会话的落片/拼装/放弃串行化;会话结束即回收,避免长期驻留。 */
|
||||
private final ConcurrentHashMap<String, Object> sessionLocks = new ConcurrentHashMap<>();
|
||||
/** 同一会话的落片/拼装/放弃串行化;static 让统一清理(LocalTempCleanupService)拿到同一把锁。 */
|
||||
private static final ConcurrentHashMap<String, Object> SESSION_LOCKS = new ConcurrentHashMap<>();
|
||||
|
||||
@Value("${aiimage.file.chunk-upload.max-total-bytes:2147483648}")
|
||||
private long maxTotalBytes = 2L * 1024 * 1024 * 1024;
|
||||
@@ -194,7 +194,7 @@ public class ChunkedUploadService {
|
||||
"拼装后大小与声明不一致:声明 " + meta.totalBytes + ",实际 " + total);
|
||||
}
|
||||
deleteQuietly(sessionDir(uploadId));
|
||||
sessionLocks.remove(uploadId);
|
||||
SESSION_LOCKS.remove(uploadId);
|
||||
String sha256 = HexFormat.of().formatHex(digest.digest());
|
||||
log.info("[chunk-upload] 拼装完成 uploadId={} fileName={} bytes={} sha256={}",
|
||||
uploadId, meta.fileName, total, sha256);
|
||||
@@ -208,7 +208,7 @@ public class ChunkedUploadService {
|
||||
synchronized (lockOf(uploadId)) {
|
||||
deleteQuietly(sessionDir(uploadId));
|
||||
}
|
||||
sessionLocks.remove(uploadId);
|
||||
SESSION_LOCKS.remove(uploadId);
|
||||
log.info("[chunk-upload] 已放弃分片会话 uploadId={} fileName={}", uploadId, meta.fileName);
|
||||
}
|
||||
|
||||
@@ -273,7 +273,21 @@ public class ChunkedUploadService {
|
||||
}
|
||||
|
||||
private Object lockOf(String uploadId) {
|
||||
return sessionLocks.computeIfAbsent(uploadId, key -> new Object());
|
||||
return sessionLockOf(uploadId);
|
||||
}
|
||||
|
||||
/**
|
||||
* P2-3:统一清理与 savePart/complete 必须是<b>同一把</b>锁,所以锁身份收在这里而不是
|
||||
* 各自 new 一份 map(两份 map 等于没锁)。锁的是盘上目录名,进程内互斥即够——
|
||||
* 每台实例只清自己的 local-temp 目录,跨实例不存在同一块盘。
|
||||
*/
|
||||
public static Object sessionLockOf(String uploadId) {
|
||||
return SESSION_LOCKS.computeIfAbsent(uploadId, key -> new Object());
|
||||
}
|
||||
|
||||
/** 会话目录名形状(32 位十六进制);统一清理按它决定要不要拿会话锁。 */
|
||||
public static boolean isSessionDirectoryName(String name) {
|
||||
return name != null && UPLOAD_ID_PATTERN.matcher(name).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-1
@@ -88,7 +88,7 @@ public class LocalTempCleanupService {
|
||||
continue;
|
||||
}
|
||||
if (child.isDirectory() && CHUNK_UPLOAD_DIR_NAME.equals(child.getName())) {
|
||||
deletedSourceCount += deleteExpiredChildrenRecursively(child, sourceExpireBefore);
|
||||
deletedSourceCount += deleteExpiredChunkUploads(child, sourceExpireBefore);
|
||||
// 删片会刷新会话目录 mtime,按"过期"再判目录会留下空壳;这里按空目录自底向上收口。
|
||||
deleteEmptyDirectoriesDeep(child, tempDir);
|
||||
}
|
||||
@@ -126,6 +126,33 @@ public class LocalTempCleanupService {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* P2-3:分片会话的过期件回收必须与 savePart/complete 共用同一把会话锁——原来裸删,
|
||||
* 在飞上传的半截会话可能被整点回收腰斩,用户的大档从 0 重来(正是断点续传要消灭的事)。
|
||||
* 非会话名的残留(历史上漏下的散件)保持原路径。
|
||||
*/
|
||||
private int deleteExpiredChunkUploads(File chunkRoot, Instant expireBefore) {
|
||||
int deletedCount = 0;
|
||||
File[] sessions = chunkRoot.listFiles();
|
||||
if (sessions == null) {
|
||||
return 0;
|
||||
}
|
||||
for (File session : sessions) {
|
||||
if (!PathSafetyGuard.isInside(chunkRoot, session)) {
|
||||
log.warn("local temp cleanup skipped unsafe path: {}", session.getAbsolutePath());
|
||||
continue;
|
||||
}
|
||||
if (ChunkedUploadService.isSessionDirectoryName(session.getName())) {
|
||||
synchronized (ChunkedUploadService.sessionLockOf(session.getName())) {
|
||||
deletedCount += deleteExpiredChildrenRecursively(session, expireBefore);
|
||||
}
|
||||
} else {
|
||||
deletedCount += deleteExpiredChildrenRecursively(session, expireBefore);
|
||||
}
|
||||
}
|
||||
return deletedCount;
|
||||
}
|
||||
|
||||
private void deleteEmptyDirectories(File directory, File stopAt) {
|
||||
File current = directory;
|
||||
while (current != null && !current.equals(stopAt) && isDirectoryEmpty(current)) {
|
||||
|
||||
+30
@@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -541,6 +542,35 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式读对象(P3-1):证据件下载不再把整对象读进堆;超上限时中止并抛 IllegalArgumentException。
|
||||
*/
|
||||
public void readObjectToStream(String bucket, String objectKey, OutputStream out, long maxBytes) {
|
||||
String normalizedBucket = requireStorageName(bucket, "bucket");
|
||||
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
|
||||
try (var stream = buildClient().getObject(GetObjectArgs.builder()
|
||||
.bucket(normalizedBucket)
|
||||
.object(normalizedObjectKey)
|
||||
.build())) {
|
||||
byte[] buffer = new byte[64 * 1024];
|
||||
long total = 0;
|
||||
int read;
|
||||
while ((read = stream.read(buffer)) != -1) {
|
||||
total += read;
|
||||
if (total > maxBytes) {
|
||||
throw new IllegalArgumentException("对象过大,超过读取上限 " + maxBytes + " 字节: "
|
||||
+ normalizedBucket + "/" + normalizedObjectKey);
|
||||
}
|
||||
out.write(buffer, 0, read);
|
||||
}
|
||||
out.flush();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("read", normalizedBucket + "/" + normalizedObjectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a managed result object from either its object key or stored public URL.
|
||||
* This overload keeps callers from depending on the configured bucket name.
|
||||
|
||||
Reference in New Issue
Block a user