refactor: 优化文件下载,移除原有逻辑
清理minio客户端导入顺序 移除已处理的TODO注释和废弃代码
This commit is contained in:
parent
2094fdf25e
commit
31a4e54764
@ -516,29 +516,10 @@ async def rechunks_documents(
|
||||
|
||||
@knowledge.get("/databases/{db_id}/documents/{doc_id}/download")
|
||||
async def download_document(db_id: str, doc_id: str, request: Request, current_user: User = Depends(get_admin_user)):
|
||||
# TODO: 可以考虑修改为minio下载,将文件相关逻辑完全迁移到minio
|
||||
"""下载原始文件"""
|
||||
logger.debug(f"Download document {doc_id} from {db_id}")
|
||||
try:
|
||||
file_info = await knowledge_base.get_file_basic_info(db_id, doc_id)
|
||||
if not file_info:
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
|
||||
file_path = file_info.get("meta", {}).get("path")
|
||||
if not file_path:
|
||||
raise HTTPException(status_code=404, detail="File path not found in metadata")
|
||||
|
||||
# 安全检查:验证文件路径
|
||||
from src.knowledge.utils.kb_utils import validate_file_path
|
||||
|
||||
try:
|
||||
normalized_path = validate_file_path(file_path, db_id)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=403, detail=str(e))
|
||||
|
||||
if not os.path.exists(normalized_path):
|
||||
raise HTTPException(status_code=404, detail=f"File not found on disk: {file_info=}")
|
||||
|
||||
# 获取文件扩展名和MIME类型,解码URL编码的文件名
|
||||
filename = file_info.get("meta", {}).get("filename", "file")
|
||||
logger.debug(f"Original filename from database: {filename}")
|
||||
@ -1075,9 +1056,7 @@ async def upload_file(
|
||||
|
||||
basename, ext = os.path.splitext(file.filename)
|
||||
# TODO:
|
||||
# 如果知识库中的文件多了,上传了内容修改过的同名文件应当把旧的文件删除掉
|
||||
# 否则会保存两份相同的文档,建议固定salt,上传逻辑是:
|
||||
# 若上传了同名文件时且hash相同则报错,不同则直接替换同名文件
|
||||
# 后续修改为遇到同名文件则在上传区域提示,是否删除旧文件,同时 filename name 也就不用添加 hash 了
|
||||
filename = f"{basename}_{hashstr(basename, 4, with_salt=True, salt='fixed_salt')}{ext}".lower()
|
||||
|
||||
file_path = os.path.join(upload_dir, filename)
|
||||
|
||||
@ -371,11 +371,6 @@ async def process_file_to_markdown(file_path: str, params: dict | None = None) -
|
||||
|
||||
return markdown_content.strip()
|
||||
|
||||
# TODO:
|
||||
# 此处修改了excel的处理逻辑,原本的excel是转为markdown后切分
|
||||
# 但是实际使用时发现,对于excel这种结构化数据,保留表头非常有必要
|
||||
# 因此改为了每10行重复保存一次表头
|
||||
# 目前前端显示有点问题,不知道为啥不能换行
|
||||
elif file_ext in [".xls", ".xlsx"]:
|
||||
# 处理 Excel 文件
|
||||
import pandas as pd
|
||||
|
||||
@ -90,7 +90,7 @@ def split_text_into_chunks(text: str, file_id: str, filename: str, params: dict
|
||||
chunks.append(
|
||||
{
|
||||
"id": f"{file_id}_chunk_{chunk_index}",
|
||||
"content": chunk_content, # .strip(),
|
||||
"content": chunk_content, # .strip(),
|
||||
"file_id": file_id,
|
||||
"filename": filename,
|
||||
"chunk_index": chunk_index,
|
||||
|
||||
@ -10,10 +10,10 @@ import uuid
|
||||
from datetime import timedelta
|
||||
from io import BytesIO
|
||||
|
||||
from minio import Minio
|
||||
from minio.error import S3Error
|
||||
from urllib3 import BaseHTTPResponse
|
||||
|
||||
from minio import Minio
|
||||
from minio.error import S3Error
|
||||
from src.utils import logger
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user