diff --git a/app/central_storage/backend/application/document_hub.py b/app/central_storage/backend/application/document_hub.py index caa04e4..2b59585 100644 --- a/app/central_storage/backend/application/document_hub.py +++ b/app/central_storage/backend/application/document_hub.py @@ -11,6 +11,9 @@ class DocumentHub: async def retrieve_document_info(self, document_id: str): return await self.document_manager.retrieve_document_info(document_id) + async def read_document_file_as_http_media_data(self, document_id: str): + return await self.document_manager.read_document_file_as_http_media_data(document_id) + async def upload_document( self, associated_with: str, file_name: str, file_data: bytes ) -> bool: diff --git a/app/central_storage/backend/business/document_manager.py b/app/central_storage/backend/business/document_manager.py index 41c0e50..6ed4201 100644 --- a/app/central_storage/backend/business/document_manager.py +++ b/app/central_storage/backend/business/document_manager.py @@ -18,6 +18,12 @@ class DocumentManager: "file_download_url": download_link, } + async def read_document_file_as_http_media_data(self, document_id: str): + await self.document_service.load_document(document_id=document_id) + + return await self.document_service.read_document_file_as_http_media_data() + + async def upload_file( self, associated_with: str, file_name: str, file_data: bytes ) -> bool: diff --git a/app/central_storage/webapi/routes/read_document_as_http_media.py b/app/central_storage/webapi/routes/read_document_as_http_media.py new file mode 100644 index 0000000..76b141c --- /dev/null +++ b/app/central_storage/webapi/routes/read_document_as_http_media.py @@ -0,0 +1,37 @@ +from fastapi import APIRouter +from infra.token.token_manager import TokenManager +from fastapi import APIRouter, Depends +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse +from fastapi import Depends, HTTPException +from starlette.status import HTTP_401_UNAUTHORIZED +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse +from infra.token.token_manager import TokenManager +from app.central_storage.backend.application.document_hub import DocumentHub + +router = APIRouter() + + +# Web API +# read document as http media +@router.get( + "/read-document-as-http-media/{document_id}", + operation_id="read-document-as-http-media", + summary="Read document as http media", + description="Read document as http media which can be used by html directly", + response_description="The http media which can be used by html directly", +) +async def read_document_as_http_media( + document_id: str +): + + # Fetch the document using DocumentHub + media = await DocumentHub().read_document_file_as_http_media_data(document_id) + + # If document is not found, raise 404 error + if not media: + raise HTTPException(status_code=404, detail="Document not found") + + # Return the document details + return JSONResponse(content=jsonable_encoder({"media": media}))