|
|
@@ -0,0 +1,203 @@
|
|
|
+# 后端文件存储重构:qingjian/{system,data} + 案件为核心
|
|
|
+
|
|
|
+## Context
|
|
|
+
|
|
|
+当前业务文件散在 5 个根:`QingJian/workspace/{caseId}`、`QingJian/chat-attachments/{userId}/{caseId}/{sessionId}`、`.agentscope/users/{userId}/workspaces/{caseId}/agents/{row}`、`data/files/knowledge|eds`、`QingJian/tmp|graph_his`。
|
|
|
+
|
|
|
+具体问题:
|
|
|
+- 一个案件的文件散在 4 个根;删案只删 `QingJian/workspace/{caseId}`,agent 工作区、EDS 媒体、Redis 会话状态都残留。
|
|
|
+- EDS 媒体路径 `data/files/eds/{日期}/{md5(采集编号)}` 里**没有 caseId**,无法按案清理,不同案件同采集编号会撞目录。
|
|
|
+- 绝对路径入库:`case_info.db_path`、`file_info.filePath`、`eds_media_file.media_path`、`person_basic_info.photograph`、`chat_attachment.file_path`、`graph_his.path`,换根/迁机器即断。
|
|
|
+- 死目录/死代码:`QingJian/agent/sessions`、`QingJian/agent/skills`、`bcp/`、`GraphServicecopy`、`WorkspaceScaffolder`、`App.init()`。
|
|
|
+- 研判 agent 工作区出现 `.agentscope/insight/.agentscope/...` 双层嵌套。
|
|
|
+- 命名口径五套(UUID/雪花/原名/中文/md5),清理策略不一致(artifacts 只清 `.csv`,agent 工作区无限增长)。
|
|
|
+- 工作目录跑偏会同时产生 `ai-server/QingJian` 和 `ai-server/data/files` 两套数据。
|
|
|
+
|
|
|
+已确认的决策:
|
|
|
+- **根目录统一小写**:`qingjian/{system,data}`。
|
|
|
+- **测试阶段,不做历史文件迁移**,旧目录保留不动,新代码只走新布局;用户重新建案验证。
|
|
|
+- **知识库保持用户级**(`knowledge_base` 不加 `case_id`)。
|
|
|
+- **不建人员目录**,人员文件放案件级 `attachments/`,靠 DB 关联。
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 一、目标目录树
|
|
|
+
|
|
|
+```
|
|
|
+<repo>/qingjian/
|
|
|
+├── system/ # 程序资产 / 系统状态(不随案件增长)
|
|
|
+│ ├── conf/ # c5e503ad.json、a4961dbp.json、config.yml、
|
|
|
+│ │ # eds_table_columns.tsv、基站导入模板.xls
|
|
|
+│ ├── templates/case.duckdb # 原 qingjian/db/5w8mf4kf
|
|
|
+│ ├── db/ # plat、temp
|
|
|
+│ ├── license/ # license.xlts、regInfo
|
|
|
+│ ├── rocksdb/d5e03ad1、uy76tkp8
|
|
|
+│ ├── libs/cell/ # 原 w6df89sk
|
|
|
+│ ├── libs/7zip/ # 原 7Zip
|
|
|
+│ ├── native/ # 原 data/native(OpenCV)
|
|
|
+│ ├── models/ # bge-m3、vosk-model
|
|
|
+│ ├── tools/ffmpeg/
|
|
|
+│ ├── extension/
|
|
|
+│ └── lock.lck
|
|
|
+└── data/ # 业务数据(唯一需要备份的目录)
|
|
|
+ ├── cases/
|
|
|
+ │ └── {caseId}/
|
|
|
+ │ ├── case.json # 案件快照:id/name/ownerId/createdAt/dbPath/schemaVersion
|
|
|
+ │ ├── db/
|
|
|
+ │ │ ├── case.duckdb
|
|
|
+ │ │ └── case.duckdb.wal
|
|
|
+ │ ├── imports/{batchId}/{fileId}_{安全原名}
|
|
|
+ │ ├── materials/{packageId}/
|
|
|
+ │ │ ├── source.zip
|
|
|
+ │ │ ├── extracted/ # 解压临时,导入后删
|
|
|
+ │ │ └── media/{采集编号MD5}/{uuid}.{ext}
|
|
|
+ │ ├── attachments/ # 案件级附件(人员头像等)
|
|
|
+ │ │ └── person_{姓名hash8}_{安全原名}
|
|
|
+ │ ├── knowledge/ # 预留:将来若做案件级知识库
|
|
|
+ │ ├── ai/
|
|
|
+ │ │ ├── artifacts/ # 工具 CSV、agent_file_*
|
|
|
+ │ │ ├── sessions/{sessionId}/attachments/
|
|
|
+ │ │ ├── agents/{agentRowId}/ # harness 工作区(sessions/memory/tool files)
|
|
|
+ │ │ ├── insight/ # 研判 agent 工作区(去掉双层 .agentscope)
|
|
|
+ │ │ ├── python/
|
|
|
+ │ │ │ ├── out/{execId}/
|
|
|
+ │ │ │ ├── sandbox/{execId}/ # 运行时,执行完删
|
|
|
+ │ │ │ ├── snapshot/case.duckdb
|
|
|
+ │ │ │ └── mpl/matplotlibrc
|
|
|
+ │ │ └── graph/{graphHisId}_{安全名}.json
|
|
|
+ │ └── tmp/ # 案件级临时
|
|
|
+ ├── users/{userId}/knowledge/{kbId}/
|
|
|
+ │ ├── docs/{docId}_{安全原名}.{ext}
|
|
|
+ │ └── text/{docId}.txt # Tika 全文 sidecar
|
|
|
+ ├── index/cases.tsv # caseId / name / ownerId / createdAt / dbPath
|
|
|
+ └── tmp/ # 全局临时(TTL 清理)
|
|
|
+```
|
|
|
+
|
|
|
+## 二、命名与路径规则
|
|
|
+
|
|
|
+1. **所有路径字面量统一小写 `qingjian`**:`PathConst.APP_NAME = "qingjian"`;`ChatAttachmentService`、`SystemConfManager`、`LicenseUtil`、`Z7`、`TowerUtils`、`EdsTableColumns`、`SystemService` 等处的 `"QingJian"` 全部替换;`.gitignore` 的 `/QingJian/` 改 `/qingjian/`;文档、部署脚本、注释同步。
|
|
|
+2. Windows 是大小写不敏感文件系统,改名前必须“先改成临时名、再改成小写”,否则 `mv QingJian qingjian` 不会真正改变大小写;`deploy/storage-layout-init.ps1` 里处理这一步骤。
|
|
|
+3. 唯一数据根:`storage.local.root-path`,默认 `./qingjian/data`;所有入库路径改为**相对数据根 + POSIX `/`**,由 `StorageLayout` 统一解析。
|
|
|
+4. 案件是唯一物理隔离单元:`cases/{caseId}` 下包含该案全部文件;删案 = 删目录 + PG/ES/Redis 清理。
|
|
|
+5. `system/` 与 `data/` 严格分离;业务代码不得再引用 `qingjian/workspace`、`chat-attachments`、`.agentscope`、`data/files`。
|
|
|
+6. 目录名固定英文小写;文件名 `{业务ID}_{安全原名}` 或 `{uuid}.{ext}`;所有用户输入段做 `sanitize` + `startsWith` 校验。
|
|
|
+7. 人员不建目录:头像等写 `cases/{caseId}/attachments/person_{md5(姓名)前8位}_{安全名}`,通过 `person_basic_info.photograph` 关联。
|
|
|
+8. `case.json` + `data/index/cases.tsv` 供人肉定位案件;创建/改名/删除时同步,启动时可重建。
|
|
|
+
|
|
|
+## 三、代码改造
|
|
|
+
|
|
|
+### 1. 新增存储层(`com.zsjz.ai.common.storage`)
|
|
|
+
|
|
|
+- `StorageLayout.java`(Spring Bean,唯一路径来源):
|
|
|
+ - 根与方法:`root()`、`resolve(String stored)`、`relative(Path)`、`publicUrl(String)`;
|
|
|
+ - 案件:`caseDir`、`caseDb`、`caseImportsDir`、`importBatchDir`、`materialDir`、`materialMediaDir`、`caseAttachmentsDir`、`personPhoto`、`caseTmpDir`;
|
|
|
+ - AI:`artifactsDir`、`sessionAttachmentsDir`、`agentWorkspace`、`insightWorkspace`、`pythonOutDir`、`pythonSandboxDir`、`pythonSnapshot`、`pythonMplDir`、`graphDir`;
|
|
|
+ - 用户:`userKnowledgeDir`、`knowledgeDoc`、`knowledgeText`;
|
|
|
+ - 生命周期:`initCase(caseId)`、`deleteCase(caseId)`、`safeName(original)`、`personTag(name)`。
|
|
|
+ - 构造时:相对 `root-path` 按 `PathConst.ROOT_PATH` 解析(不是 `user.dir`),修复 IDE 从 `ai-server/` 启动导致的双根问题。
|
|
|
+- `StorageBootstrap.java`:启动时创建 `data/{cases,users,tmp,index}`、`system/{conf,templates,db,license,rocksdb,libs,native,models,tools}`,打印实际根;检测到旧目录只告警不搬。
|
|
|
+- `StorageMaintenanceService.java`:定时清理 `python/sandbox/*`、全局 `tmp/*`(接手现有 24h TTL 扫描)、`artifact.retain-days > 0` 时清理全部产物(不再只清 `.csv`)。
|
|
|
+- `WorkspacePathGuard` 改为 `@Component`,注入 `StorageLayout`,校验“数据库/客户端路径必须落在 `cases/{caseId}` 内”,保留符号链接和 `toRealPath()` 二次校验。
|
|
|
+- `LocalFileStorageUtil` 保留 IO 能力(upload/download/delete/list/stat),删除业务方法 `generatePath`、`storeEdsMedia`、`getPresignedDownloadUrl`,根路径委托 `StorageLayout`。
|
|
|
+
|
|
|
+### 2. `PathConst` 与系统资产
|
|
|
+
|
|
|
+`common/constants/PathConst.java` 改为:
|
|
|
+- `APP_NAME = "qingjian"`;`ROOT_PATH` 自动探测工作目录(`user.dir` 下有 `qingjian`/`QingJian` 就用它,否则回退父目录),修复双根。
|
|
|
+- `SYSTEM_PATH = ROOT_PATH/qingjian/system`,并重定向:
|
|
|
+ `conf`、`templates/case.duckdb`、`db/plat|temp`、`license`、`rocksdb/d5e03ad1|uy76tkp8`、`libs/cell`、`libs/7zip`、`native`、`extension`、`lock.lck`。
|
|
|
+- 删除业务常量:`WORKSPACE`、`AGENT_PATH`、`SKILLS_PATH`、`GRAPH_HIS_PATH`、`TMP_PATH`、`TEMP_DB_PATH`、`EDS_PATH/EDS_*`、`ORI_QINGJIAN_PATH/ORI_UPGRADE_PATH/UPGRADE_JAR_PATH`(`App.init()` 已注释,直接删)。
|
|
|
+- `RUN_QINGJIAN_PATH` 重命名为 `APP_HOME`,避免继续带旧大小写。
|
|
|
+- `TowerUtils` 去掉硬编码 `E:\luminara\...`,改用 `CELL_TOWER_PATH`/`CELL_TOWER_PATH_SO`。
|
|
|
+- `PpOcrEngine` 原生库目录改 `system/native`;`MediaTranscriber` 的 ffmpeg/Vosk 回退改 `system/tools|models`;`EmbeddingService` 模型目录由配置指向 `system/models/bge-m3`。
|
|
|
+
|
|
|
+### 3. 业务域替换
|
|
|
+
|
|
|
+| 业务 | 旧位置 | 新位置 | 主要修改文件 |
|
|
|
+|---|---|---|---|
|
|
|
+| 案件库 | `qingjian/workspace/{caseId}/{随机名}` | `cases/{caseId}/db/case.duckdb` | `CaseInfoService`、`CaseDataSourceRegistry`、`PythonExecutor`、`SystemService` |
|
|
|
+| 清洗导入 | `workspace/{caseId}/upload/{batchId}/{雪花}_{名}` | `cases/{caseId}/imports/{batchId}/{fileId}_{名}` | `DmService` |
|
|
|
+| EDS 包/解压 | `workspace/{caseId}/eds/{pkgId}/{zip,解压目录}` | `cases/{caseId}/materials/{pkgId}/{source.zip,extracted}` | `EdsMaterialProcessService`、`EdsExtra`、`Eds` |
|
|
|
+| EDS 媒体 | `data/files/eds/{日期}/{md5(cid)}/{uuid}` | `cases/{caseId}/materials/{pkgId}/media/{md5(cid)}/{uuid}` | `LocalFileStorageUtil`、`EdsMediaUploader`、`EdsExtra` |
|
|
|
+| 对话附件 | `qingjian/chat-attachments/{uid}/{cid}/{sid}/` | `cases/{caseId}/ai/sessions/{sid}/attachments/`(draft 用 `_draft`) | `ChatAttachmentService` |
|
|
|
+| 工具产物 | `workspace/{cid}/artifacts/` | `cases/{caseId}/ai/artifacts/` | `ArtifactService`、`AgentArtifactScanner` |
|
|
|
+| Python | `workspace/{cid}/{tmp/py-*,py-out,py-snapshot,py-mpl}` | `cases/{caseId}/ai/python/{sandbox,out,snapshot,mpl}` | `PythonExecutor`、`AgentPythonFileController` |
|
|
|
+| Agent 工作区 | `.agentscope/users/{uid}/workspaces/{cid}/agents/{row}/` | `cases/{caseId}/ai/agents/{agentRowId}/` | `WorkspaceManagerFactory`(删除)、`AgentService`、`AgentArtifactScanner` |
|
|
|
+| 研判工作区 | `.agentscope/insight/.agentscope/...` | `cases/{caseId}/ai/insight/` | `InsightAgentFactory` |
|
|
|
+| 图谱历史 | `qingjian/graph_his/{随机+uid}.json` | `cases/{caseId}/ai/graph/{graphHisId}_{名}.json` | `GraphService` |
|
|
|
+| 人员头像 | `workspace/{cid}/photograph/{名}` | `cases/{caseId}/attachments/person_{hash8}_{名}` | `PersonBasicInfoService` |
|
|
|
+| 知识库 | `data/files/knowledge/{kbId}/doc/{uuid}` | `users/{userId}/knowledge/{kbId}/docs/{docId}_{名}` + `text/{docId}.txt` | `KnowledgeDocService`、`KnowledgeDocParser`、`KnowledgeDocIndexer`、`LocalFileStorageUtil` |
|
|
|
+| 全局临时 | `qingjian/tmp/` | `data/tmp/` | `DmService`、`TowerService`、`AppLoadEndEventListener` |
|
|
|
+
|
|
|
+关键实现点:
|
|
|
+- **ID 前置生成**:`FileInfo`、`ChatAttachment`、`KnowledgeDoc`、`EdsMedia` 在落盘前用 `IdUtil.getSnowflakeNextId()` 生成 ID,文件名直接带业务 ID,DB 与磁盘可互查。
|
|
|
+- **EDS**:`media_path` 存相对路径,`media_url` 由 `StorageLayout.publicUrl()` 派生;`EdsMaterialInfoService.downloadFile` 改为“相对路径 + 当前案件目录守卫”,修掉现在可传任意绝对路径的问题。
|
|
|
+- **知识库 sidecar**:不再用“同目录同名 `.txt`”,改为 `text/{docId}.txt`,解析/索引/重试统一走 DB 的 `sidecar_path`。
|
|
|
+- **前端契约**:`person_basic_info.photograph` 入库改相对路径,出参在 VO 层转 `/files/...` URL(`<img src>` 可直接用);`CaseInfo.workspacePath` 改为新案件目录绝对路径。
|
|
|
+- **会话/删案清理**:`AgentChatServiceImpl.deleteSession` 增加删除 `ai/sessions/{sid}` 与 Redis slot;`CaseInfoService.delete` 删除整个 `cases/{caseId}`,覆盖产物、附件、agent 工作区、EDS 媒体、Python 快照、图谱历史。
|
|
|
+
|
|
|
+### 4. 删除死代码/死目录
|
|
|
+
|
|
|
+- `qingjian/agent/sessions`、`qingjian/agent/skills` 的创建逻辑(`AppLoadEndEventListener`、`CaseInfoService`)。
|
|
|
+- `PathConst` 中 `WORKSPACE/AGENT_PATH/SKILLS_PATH/GRAPH_HIS_PATH/TMP_PATH/TEMP_DB_PATH/EDS_*/ORI_*`。
|
|
|
+- `bcp/`、`GraphServicecopy`、`WorkspaceScaffolder`、`App.init()`、`StateManager.caseWorkspace()`。
|
|
|
+- `LocalFileStorageUtil.getPresignedDownloadUrl`(`/file-download/**` 未实现)。
|
|
|
+- `SystemService.getFilePath` 返回裸绝对路径、`openLocalFile` 任意路径打开:加 `StorageLayout` 守卫或下线。
|
|
|
+
|
|
|
+## 四、配置 / 部署 / 文档
|
|
|
+
|
|
|
+- `application.yaml` 增加默认 `storage.local` 块;`application-dev.yaml`/`application-pro.yaml` 改:
|
|
|
+ ```yaml
|
|
|
+ storage:
|
|
|
+ local:
|
|
|
+ root-path: "${STORAGE_LOCAL_ROOT_PATH:./qingjian/data}"
|
|
|
+ base-url: "${STORAGE_LOCAL_BASE_URL:http://localhost:8980/js/a}"
|
|
|
+ ```
|
|
|
+ 删除 `eds-dir`(路径已由 `StorageLayout` 决定)。
|
|
|
+- `deploy/env.sh`:`STORAGE_LOCAL_ROOT_PATH="./qingjian/data"`;模型/ffmpeg 路径改 `qingjian/system/models|tools`。
|
|
|
+- `SaTokenWebConfig` 免登录白名单改为 `/files/cases/*/materials/*/media/**`(EDS 媒体仍免登录,范围收窄到媒体目录)。
|
|
|
+- 新增 `deploy/storage-layout-init.sh` / `.ps1`:`--dry-run`,把 `conf/db模板/license/rocksdb/w6df89sk/7Zip/extension/data/native/bge-m3/vosk-model/tools/ffmpeg` 迁入 `qingjian/system`,只建 `data/` 骨架,不碰旧案件数据;Windows 先改临时目录名再改成小写。
|
|
|
+- 新增 `deploy/storage-legacy-clean.sh`:需显式 `--yes`,删除旧 `workspace/chat-attachments/.agentscope/data/files`。
|
|
|
+- 新增 `docs/design/storage-layout.md`(本方案的目录、命名、生命周期、备份说明);更新 `QUICK_START.md` 的路径表和“工作目录”说明;`.gitignore` 改 `/qingjian/`。
|
|
|
+- 执行一次全量字面量检查:
|
|
|
+ `grep -rn "QingJian" ai-server/src/main/java ai-server/src/main/resources deploy docs QUICK_START.md AI_AGENT.md .gitignore`
|
|
|
+ 除历史说明外,路径一律改成 `qingjian`。
|
|
|
+
|
|
|
+## 五、实施顺序
|
|
|
+
|
|
|
+1. **基础设施**:`StorageLayout`、`StorageBootstrap`、`StorageMaintenanceService`、`PathConst` 系统资产与小写化、`LocalFileStorageUtil` 收敛、配置/脚本;编译通过,启动后目录骨架正确。
|
|
|
+2. **案件与数据导入**:`CaseInfoService`(新布局、`case.json`、索引、相对 `db_path`)、`DmService`、EDS 材料/媒体、`WorkspacePathGuard`、EDS 下载守卫。
|
|
|
+3. **AI 运行时**:对话附件、artifacts、agent/insight 工作区、Python、图谱、会话删除清理、Redis 清理。
|
|
|
+4. **知识库与人员**:知识文档相对路径 + `text/` sidecar、人员头像 + URL 转换。
|
|
|
+5. **清理与验证**:删死代码、更新文档、跑测试、按“验证”清单走一遍完整流程。
|
|
|
+
|
|
|
+## 六、验证
|
|
|
+
|
|
|
+1. `mvn -pl ai-server -am clean compile`、`mvn -pl ai-server test`。
|
|
|
+2. 从仓库根启动,确认生成:
|
|
|
+ - `qingjian/system/{conf,templates,db,license,rocksdb,libs,native,models}`;
|
|
|
+ - `qingjian/data/{cases,users,tmp,index}`。
|
|
|
+3. 完整冒烟:
|
|
|
+ - 建案 → `data/cases/{id}/case.json`、`db/case.duckdb`;
|
|
|
+ - 上传 CSV → `imports/{batchId}/{fileId}_x.csv`;
|
|
|
+ - 导入 EDS 包 → `materials/{pkgId}/source.zip`、`media/{cidHash}/{uuid}.ext`,图片 URL 可加载;
|
|
|
+ - 对话附件 → `ai/sessions/{sid}/attachments/`;
|
|
|
+ - 工具产物 → `ai/artifacts/`,预览/下载正常;
|
|
|
+ - Python → `ai/python/out/{execId}/`,图片 URL 正常;
|
|
|
+ - 人员头像 → `attachments/person_{hash8}_*`,前端可显示;
|
|
|
+ - 知识库上传/下载 → `users/{uid}/knowledge/{kbId}/docs|text/`;
|
|
|
+ - 图谱历史保存/读取;
|
|
|
+ - 删案 → `data/cases/{id}` 消失,PG/ES/Redis 无残留。
|
|
|
+4. 反向检查:
|
|
|
+ - `grep -rn "PathConst.WORKSPACE\|chat-attachments\|\.agentscope/users\|graph_his\|data/files" ai-server/src/main/java` 仅应命中 storage 层/注释;
|
|
|
+ - `grep -rn "\"QingJian\"\|/QingJian/" ai-server/src/main/java` 应为 0;
|
|
|
+ - 故意从 `ai-server/` 作为工作目录启动,数据仍落在仓库根 `qingjian/data`。
|
|
|
+
|
|
|
+## 七、风险与注意
|
|
|
+
|
|
|
+- **无历史迁移**:旧 `QingJian/workspace`、`chat-attachments`、`.agentscope`、`data/files` 保留但不再读取;验证后由 `storage-legacy-clean.sh` 手动清理。
|
|
|
+- **大小写改名**:Windows 上必须走“临时名 → 小写名”;Linux 上直接 `mv QingJian qingjian` 即可。Git 里同时会看到 `.gitignore` 和路径字面量的变更。
|
|
|
+- **知识库用户级**:内置库文档按上传者放 `users/{userId}/...`;如果以后要案件级知识库,再给 `knowledge_base` 加 `case_id` 并启用 `cases/{caseId}/knowledge/`。
|
|
|
+- **人员不建目录**:人员文件全部平铺在 `attachments/`,靠 `person_basic_info.photograph` 和文件名前缀 `person_{hash8}` 关联;改名/删除时按 DB 路径处理。
|
|
|
+- **EDS 媒体免登录**:白名单从 `/files/eds/**` 收窄到 `/files/cases/*/materials/*/media/**`,URL 仍是不可猜的 UUID。
|
|
|
+- **多节点部署**:若 gateway 仍路由到多台 ai-server,`qingjian/data` 必须放在共享存储上(与当前 `data/files` 的要求一致)。
|