Skip to content

Commit

Permalink
fix:The link of upload files is broken in the chat view pingcap#466
Browse files Browse the repository at this point in the history
  • Loading branch information
Rutheniumlmw committed Dec 24, 2024
1 parent 634f951 commit 621e4aa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions backend/app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
user,
api_key,
feedback,
download,
document,
)
from app.api.admin_routes.knowledge_base.routes import (
router as admin_knowledge_base_router,
Expand Down Expand Up @@ -53,7 +53,7 @@
api_router.include_router(feedback.router, tags=["chat"])
api_router.include_router(user.router, tags=["user"])
api_router.include_router(api_key.router, tags=["auth"])
api_router.include_router(download.router, tags=["download_file"])
api_router.include_router(document.router, tags=["documents"])
api_router.include_router(admin_chat_engine.router, tags=["admin/chat_engine"])
api_router.include_router(admin_document_router, tags=["admin/documents"])
api_router.include_router(admin_feedback.router, tags=["admin/feedback"])
Expand Down
35 changes: 35 additions & 0 deletions backend/app/api/routes/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from fastapi import FastAPI, HTTPException, APIRouter
from fastapi.responses import StreamingResponse
from sqlmodel import Session
from app.api.deps import SessionDep
from app.repositories import document_repo
from app.file_storage import get_file_storage

router = APIRouter()

@router.get("/documents/{doc_id}/download")
def download_file(
doc_id: int,
session: SessionDep
):
doc = document_repo.must_get(session, doc_id)

name = doc.source_uri
filestorage = get_file_storage()
if filestorage.exists(name):
file_size = filestorage.size(name)
headers = {"Content-Length": str(file_size)}
def iterfile():
with filestorage.open(name) as f:
while chunk := f.read(8192): # 每次读取 8KB
yield chunk
return StreamingResponse(
iterfile(),
media_type = doc.mime_type,
headers = headers
)
else:
raise HTTPException(status_code = 404, detail = "File not found")



28 changes: 0 additions & 28 deletions backend/app/api/routes/download.py

This file was deleted.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
depends_on:
- redis
ports:
- "8002:80"
- "8000:80"
env_file:
- .env
volumes:
Expand Down

0 comments on commit 621e4aa

Please sign in to comment.