Skip to content

Commit

Permalink
feat: support download uploaded documents (#532)
Browse files Browse the repository at this point in the history
part of #466

---------

Co-authored-by: MingWei Liu <[email protected]>
  • Loading branch information
Rutheniumlmw and MingWei Liu authored Dec 30, 2024
1 parent 9b05f10 commit aaf81b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
user,
api_key,
feedback,
document,
)
from app.api.admin_routes.knowledge_base.routes import (
router as admin_knowledge_base_router,
Expand Down Expand Up @@ -52,6 +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(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
34 changes: 34 additions & 0 deletions backend/app/api/routes/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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:
yield from f
return StreamingResponse(
iterfile(),
media_type = doc.mime_type,
headers = headers
)
else:
raise HTTPException(status_code = 404, detail = "File not found")



0 comments on commit aaf81b9

Please sign in to comment.