fix(知识库): 优化解决ppt中图片解析同步dify时去掉base64数据

This commit is contained in:
runny committed 2026-06-12 16:44:02 +08:00
1 parent d4f629106f
commit c1c4efe4f1
4 files changed
+59 -16

No files matched your search

@@ -6,6 +6,7 @@ from __future__ import annotations
from datetime import datetime
import hashlib
import re
from pathlib import Path
import requests
@@ -207,6 +208,13 @@ class KbDifySyncService:
)
def _strip_base64_images(text: str) -> str:
"""Remove markdown image syntax containing base64 data to avoid token bloat in Dify."""
# Matches ![alt](data:image/...)
pattern = r'!\[[^\]]*\]\(data:image\/[a-zA-Z]*;base64,[^)]*\)'
return re.sub(pattern, '', text)
def _load_sync_text(version: KbDocumentVersion) -> tuple[str, str]:
candidate_paths = [version.cleaned_file_path, version.cleaned_preview_path, version.source_file_path]
for value in candidate_paths:
@@ -217,6 +225,8 @@ def _load_sync_text(version: KbDocumentVersion) -> tuple[str, str]:
continue
text = path.read_text(encoding='utf-8', errors='ignore').strip()
if text:
# Strip base64 images before syncing
text = _strip_base64_images(text)
return text, str(path)
raise KbDifySyncError('没有可用于同步的文本结果')