diff --git a/app/central_storage/backend/application/document_hub.py b/app/central_storage/backend/application/document_hub.py index 68a4ce7..caa04e4 100644 --- a/app/central_storage/backend/application/document_hub.py +++ b/app/central_storage/backend/application/document_hub.py @@ -4,22 +4,21 @@ from app.central_storage.backend.business.document_manager import ( class DocumentHub: - def __init__(self, user_id: str): - self.user_id = user_id + def __init__(self, ): self.document_manager = DocumentManager(self.user_id) return - async def get_document_by_id(self, document_id: str): - return await self.document_manager.get_document_details_by_id(document_id) + async def retrieve_document_info(self, document_id: str): + return await self.document_manager.retrieve_document_info(document_id) - async def upload_document_for_object( - self, object_id: str, file_name: str, file_data: bytes + async def upload_document( + self, associated_with: str, file_name: str, file_data: bytes ) -> bool: """Upload a file Args: file_name: the name of the file file (bytes): the file to be uploaded """ - return await self.document_manager.upload_file_for_object( - object_id, file_name, file_data + return await self.document_manager.upload_file( + associated_with, file_name, file_data ) diff --git a/app/central_storage/backend/business/document_manager.py b/app/central_storage/backend/business/document_manager.py index 2787945..41c0e50 100644 --- a/app/central_storage/backend/business/document_manager.py +++ b/app/central_storage/backend/business/document_manager.py @@ -3,11 +3,10 @@ from app.central_storage.backend.models.models import MediaType, DataFormat class DocumentManager: - def __init__(self, user_id) -> None: - self.user_id = user_id + def __init__(self) -> None: self.document_service = DocumentService() - async def get_document_details_by_id(self, document_id: str): + async def retrieve_document_info(self, document_id: str): await self.document_service.load_document(document_id=document_id) download_link = ( @@ -19,15 +18,15 @@ class DocumentManager: "file_download_url": download_link, } - async def upload_file_for_object( - self, object_id: str, file_name: str, file_data: bytes + async def upload_file( + self, associated_with: str, file_name: str, file_data: bytes ) -> bool: await self.document_service.new_document( file_name=file_name, # This 'UNKNOWN' will make the document manager decide the media type from file name media_type=MediaType.UNKNOWN, data_format=DataFormat.RAW, - created_by=object_id, + created_by=associated_with, ) return await self.document_service.save_document_file(file_data) or None # TODO: This should go to Freeleaps App diff --git a/app/central_storage/webapi/routes/__init__.py b/app/central_storage/webapi/routes/__init__.py index 4c8c6d5..84c0b9d 100644 --- a/app/central_storage/webapi/routes/__init__.py +++ b/app/central_storage/webapi/routes/__init__.py @@ -1,5 +1,5 @@ from fastapi import APIRouter -from .get_document_by_id import router as doc_router +from .retrieve_document_info import router as doc_router from .upload_document import router as ud_router api_router = APIRouter() diff --git a/app/central_storage/webapi/routes/get_document_by_id.py b/app/central_storage/webapi/routes/retrieve_document_info.py similarity index 71% rename from app/central_storage/webapi/routes/get_document_by_id.py rename to app/central_storage/webapi/routes/retrieve_document_info.py index a1dc665..bcf8941 100644 --- a/app/central_storage/webapi/routes/get_document_by_id.py +++ b/app/central_storage/webapi/routes/retrieve_document_info.py @@ -17,25 +17,18 @@ token_manager = TokenManager() # Web API # Fetch document by ID @router.get( - "/get-document-by-id/{document_id}", - operation_id="get-document-by-id", + "/retrieve_document_info/{document_id}", + operation_id="retrieve_document_info", summary="Fetch a document by its ID", description="Retrieve a specific document by its document ID and return file name and download URL", response_description="The document details including file name and download URL", ) -async def get_document_by_id( - document_id: str, - current_user: dict = Depends(token_manager.get_current_user), +async def retrieve_document_info( + document_id: str ): - user_id = current_user.get("id") - - if not user_id: - raise HTTPException( - status_code=HTTP_401_UNAUTHORIZED, detail="Could not validate credentials" - ) # Fetch the document using DocumentHub - document = await DocumentHub(user_id).get_document_by_id(document_id) + document = await DocumentHub().retrieve_document_info(document_id) # If document is not found, raise 404 error if not document: diff --git a/app/central_storage/webapi/routes/upload_document.py b/app/central_storage/webapi/routes/upload_document.py index d625a48..fda28d0 100644 --- a/app/central_storage/webapi/routes/upload_document.py +++ b/app/central_storage/webapi/routes/upload_document.py @@ -13,28 +13,20 @@ token_manager = TokenManager() @router.post( "/upload-document", - summary="upload a document for a given object.", + summary="upload a document with a given associated_with id.", description="upload a document. If success, returning the document id", ) async def attach_document_for_request( - object_id: str = Form(...), - file: UploadFile = File(None), - current_user: dict = Depends(token_manager.get_current_user), + associated_with: str = Form(...), + file: UploadFile = File(None) ): - print("current user", current_user) - user_id = current_user.get("id") - print("current user id", user_id) - if not user_id: - raise HTTPException( - status_code=HTTP_401_UNAUTHORIZED, detail="Could not validate credentials" - ) - document_hub = DocumentHub(user_id) + document_hub = DocumentHub() # File processing try: file_data = await file.read() # You can use async chunking for larger files - document_id = await document_hub.upload_document_for_object( - object_id, file.filename, file_data + document_id = await document_hub.upload_document( + associated_with, file.filename, file_data ) if document_id: