🔴 P0 | KnowledgeItem 确保 sourceType 返回文件类型 #61

Closed
opened 2026-05-30 16:25:30 +08:00 by wangdl · 2 comments
Owner

背景

iOS 知识点列表需要展示文件类型标签(PDF / MD / HTML / 图片 / 文件),当前大部分知识点的 sourceType 返回空值,前端不得不通过标题后缀推断。

需求

  • 后端在导入/创建知识点时,根据源文件类型正确写入 sourceType
  • 值示例:pdf / markdown / html / txt / image

影响页面

  • 知识点列表 - 类型:PDF 标签
## 背景 iOS 知识点列表需要展示文件类型标签(PDF / MD / HTML / 图片 / 文件),当前大部分知识点的 sourceType 返回空值,前端不得不通过标题后缀推断。 ## 需求 - 后端在导入/创建知识点时,根据源文件类型正确写入 sourceType - 值示例:pdf / markdown / html / txt / image ## 影响页面 - 知识点列表 - 类型:PDF 标签
wangdl added this to the H0:iOS 对接阻断修复(P0) milestone 2026-05-30 16:25:30 +08:00
Author
Owner

修复汇报 (2026-06-05)

实现方案

KnowledgeItemsRepository.create() 中新增 detectSourceType() 自动检测逻辑:

  1. COS URL → 从 pathname 提取扩展名映射(.pdf→pdf, .md→markdown, .txt→text, .html→html, .png/.jpg→image 等)
  2. HTML 内容 → 内容以 < 开头 → html
  3. Markdown 内容 → 匹配 #/-/[link]() 模式 → markdown
  4. 标题推断 → 根据 title 扩展名兜底
  5. 默认text

修改文件

  • src/modules/knowledge-items/knowledge-items.repository.ts — 新增 detectSourceType() 函数,create() 自动填充 sourceType

兼容性

  • DTO 新增可选的 sourceType 字段,手动传入优先级高于自动检测
  • 所有创建路径(POST /knowledge-items、import-candidate accept、worker)均自动生效

状态

代码已完成,文件已传至服务器,需重启 NestJS 生效。

## 修复汇报 (2026-06-05) ### 实现方案 在 `KnowledgeItemsRepository.create()` 中新增 `detectSourceType()` 自动检测逻辑: 1. **COS URL** → 从 pathname 提取扩展名映射(.pdf→pdf, .md→markdown, .txt→text, .html→html, .png/.jpg→image 等) 2. **HTML 内容** → 内容以 `<` 开头 → `html` 3. **Markdown 内容** → 匹配 `#`/`-`/`[link]()` 模式 → `markdown` 4. **标题推断** → 根据 title 扩展名兜底 5. **默认** → `text` ### 修改文件 - `src/modules/knowledge-items/knowledge-items.repository.ts` — 新增 `detectSourceType()` 函数,`create()` 自动填充 sourceType ### 兼容性 - DTO 新增可选的 `sourceType` 字段,手动传入优先级高于自动检测 - 所有创建路径(POST /knowledge-items、import-candidate accept、worker)均自动生效 ### 状态 ✅ 代码已完成,文件已传至服务器,需重启 NestJS 生效。
Author
Owner

优化 (2026-06-06)

问题

detectSourceType() 中 markdown 检测正则 /\[.+\]\(.+\)/ 使用贪婪匹配 .+,超长文本可能触发 ReDoS 回溯爆炸。

修复

改为懒惰匹配 /\[.+?\]\(.+?\)/,显著减少回溯。

状态

已修复。

## 优化 (2026-06-06) ### 问题 `detectSourceType()` 中 markdown 检测正则 `/\[.+\]\(.+\)/` 使用贪婪匹配 `.+`,超长文本可能触发 ReDoS 回溯爆炸。 ### 修复 改为懒惰匹配 `/\[.+?\]\(.+?\)/`,显著减少回溯。 ### 状态 ✅ 已修复。
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: wangdl/api-server#61
No description provided.