diff --git a/backend/app/api/main.py b/backend/app/api/main.py index 875d2d40..6f29bc39 100644 --- a/backend/app/api/main.py +++ b/backend/app/api/main.py @@ -7,6 +7,7 @@ user, api_key, feedback, + document, ) from app.api.admin_routes.knowledge_base.routes import ( router as admin_knowledge_base_router, @@ -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"]) diff --git a/backend/app/api/routes/document.py b/backend/app/api/routes/document.py new file mode 100644 index 00000000..eb88871a --- /dev/null +++ b/backend/app/api/routes/document.py @@ -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") + + +