소스 검색

fix(storage): 解析完成后清理 OSS 下载中转缓存(.jdcloud-cache 用后即删)

OSS 为持久存储,/data/cfc-uploads 仅为解析中转。collect() finally 块统一清理:
- 仅删除 .jdcloud-cache 缓存文件(getFilePath 下载产物),本地磁盘直存原文件不动
- 解析成功/失败/异常所有路径均清理;重试时自动重新下载
Xiaogang Liao 1 주 전
부모
커밋
90fd3694c8
1개의 변경된 파일과 24개의 추가작업 그리고 1개의 파일을 삭제
  1. 24 1
      cfc-backend/src/main/java/com/etotem/cfc/service/ReportCollectService.java

+ 24 - 1
cfc-backend/src/main/java/com/etotem/cfc/service/ReportCollectService.java

@@ -25,6 +25,7 @@ import java.io.FileInputStream;
 import java.io.InputStreamReader;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.Executor;
@@ -152,9 +153,10 @@ public class ReportCollectService {
         if (STATUS_COMPLETED.equals(draft.getParseStatus())) {
             return;
         }
+        String pdfPath = null;
         try {
             healthReportDraftService.markParseStarted(draftId);
-            String pdfPath = resolvePdfPath(draft);
+            pdfPath = resolvePdfPath(draft);
             if (pdfPath == null) {
                 healthReportDraftService.markCollectFailed(draftId, "无法获取报告文件路径");
                 return;
@@ -275,6 +277,8 @@ public class ReportCollectService {
         } catch (Exception e) {
             log.error("异步采集失败 draftId={}", draftId, e);
             healthReportDraftService.markCollectFailed(draftId, e.getMessage());
+        } finally {
+            cleanupPdfCache(pdfPath);
         }
     }
 
@@ -433,6 +437,25 @@ public class ReportCollectService {
         }
     }
 
+    /**
+     * 解析完成后清理 OSS 下载中转缓存。OSS 为持久存储,本地缓存仅为解析中转,用后即删。
+     * 仅清理 .jdcloud-cache 缓存文件,本地磁盘存储(uploadBaseDir 直存)的原文件不动。
+     */
+    private void cleanupPdfCache(String pdfPath) {
+        if (pdfPath == null) {
+            return;
+        }
+        File f = new File(pdfPath);
+        if (f.exists() && f.getAbsolutePath().contains(".jdcloud-cache")) {
+            try {
+                Files.deleteIfExists(f.toPath());
+                log.info("已清理OSS下载中转缓存 {}", pdfPath);
+            } catch (Exception e) {
+                log.warn("清理OSS下载中转缓存失败 {}", pdfPath, e);
+            }
+        }
+    }
+
     /**
      * 检查文件是否为 PDF(通过魔数 %PDF- 判断)。
      * 指纹引擎、LangGraph 解析、opencode 均要求 PDF 输入。