freeleaps-service-hub/app/central_storage/webapi/routes/upload_document.py

43 lines
1.3 KiB
Python

from fastapi import APIRouter, UploadFile, File, Form, HTTPException
from fastapi import APIRouter, Depends
from infra.token.token_manager import TokenManager
from starlette.status import HTTP_401_UNAUTHORIZED
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from app.central_storage.backend.application.document_hub import DocumentHub
from pydantic import BaseModel
router = APIRouter()
class Item(BaseModel):
associated_with: str
name: str
blob: bytes
@router.post(
"/upload-document",
summary="upload a document with a given associated_with id, document name and document data.",
description="upload a document. If success, returning the document id",
)
async def upload_document(item: Item):
document_hub = DocumentHub()
# File processing
try:
document_id = await document_hub.upload_document(
item.associated_with, item.name, item.blob
)
if document_id:
result = {"document_id": str(document_id)}
return JSONResponse(content=jsonable_encoder(result))
else:
return JSONResponse(
status_code=500, content={"error": "Document upload failed"}
)
except Exception as e:
return JSONResponse(status_code=500, content={"error": "Internal server error"})