- **文件管理**: - 支持文件上传、解析和删除。 - 限制支持格式(txt, md, docx, pdf),最大 10MB。 - 提供前端上传区、文件列表及后端解析服务。 - **技能管理**: - 新增技能的创建、更新及删除功能。 - 支持分配自定义提示词和工具权限。 - 提供技能列表及分组工具选择交互界面。 同时调整相关 API 和前端组件,优化协作体验。
This commit is contained in:
64
backend/app/services/file_parser.py
Normal file
64
backend/app/services/file_parser.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""文件解析服务:将上传的文件转换为纯文本"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# 支持的文件扩展名
|
||||
ALLOWED_EXTENSIONS = {".txt", ".md", ".docx", ".pdf"}
|
||||
|
||||
# 最大文件大小 10MB
|
||||
MAX_FILE_SIZE = 10 * 1024 * 1024
|
||||
|
||||
|
||||
def parse_file(file_path: Path, mime_type: str) -> str:
|
||||
"""解析文件为纯文本,返回文本内容"""
|
||||
ext = file_path.suffix.lower()
|
||||
|
||||
if ext in (".txt", ".md"):
|
||||
return _parse_text(file_path)
|
||||
elif ext == ".docx":
|
||||
return _parse_docx(file_path)
|
||||
elif ext == ".pdf":
|
||||
return _parse_pdf(file_path)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件格式: {ext}")
|
||||
|
||||
|
||||
def _parse_text(file_path: Path) -> str:
|
||||
"""解析纯文本文件,自动检测编码"""
|
||||
for encoding in ("utf-8", "gbk", "gb18030", "utf-16", "latin-1"):
|
||||
try:
|
||||
return file_path.read_text(encoding=encoding)
|
||||
except (UnicodeDecodeError, UnicodeError):
|
||||
continue
|
||||
# 最后用 errors='replace' 兜底
|
||||
return file_path.read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
|
||||
def _parse_docx(file_path: Path) -> str:
|
||||
"""解析 Word 文档"""
|
||||
try:
|
||||
from docx import Document
|
||||
except ImportError:
|
||||
raise RuntimeError("需要安装 python-docx: pip install python-docx")
|
||||
|
||||
doc = Document(str(file_path))
|
||||
paragraphs = [p.text for p in doc.paragraphs if p.text.strip()]
|
||||
return "\n\n".join(paragraphs)
|
||||
|
||||
|
||||
def _parse_pdf(file_path: Path) -> str:
|
||||
"""解析 PDF 文件"""
|
||||
try:
|
||||
import fitz # pymupdf
|
||||
except ImportError:
|
||||
raise RuntimeError("需要安装 pymupdf: pip install pymupdf")
|
||||
|
||||
doc = fitz.open(str(file_path))
|
||||
pages = []
|
||||
for page in doc:
|
||||
text = page.get_text().strip()
|
||||
if text:
|
||||
pages.append(text)
|
||||
doc.close()
|
||||
return "\n\n".join(pages)
|
||||
Reference in New Issue
Block a user