jetli 2024-10-23 04:09:19 +00:00
commit 05cc8f9d60
7 changed files with 123 additions and 49 deletions

View File

@ -4,22 +4,24 @@ 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 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:
"""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
)

View File

@ -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,21 @@ 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 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:
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

View File

@ -1,9 +1,11 @@
from fastapi import APIRouter
from .get_document_by_id import router as doc_router
from .retrieve_document_info import router as ri_router
from .upload_document import router as ud_router
from .read_document_as_http_media import router as rd_router
api_router = APIRouter()
api_router.include_router(doc_router, tags=["attachment"])
api_router.include_router(ud_router, tags=["attachment"])
api_router.include_router(ri_router, tags=["document"])
api_router.include_router(ud_router, tags=["document"])
api_router.include_router(rd_router, tags=["document"])
websocket_router = APIRouter()

View File

@ -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}))

View File

@ -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:

View File

@ -5,44 +5,37 @@ 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()
token_manager = TokenManager()
class Item(BaseModel):
associated_with: str
name: str
blob: bytes
@router.post(
"/upload-document",
summary="upload a document for a given object.",
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 attach_document_for_request(
object_id: str = Form(...),
file: UploadFile = File(None),
current_user: dict = Depends(token_manager.get_current_user),
async def upload_document(
item:Item
):
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(
item.associated_with, item.name, item.blob
)
if document_id:
result = {"document_id": str(document_id), "file_name": file.filename}
result = {"document_id": str(document_id)}
return JSONResponse(content=jsonable_encoder(result))
else:
return JSONResponse(
status_code=500, content={"error": "File upload failed"}
status_code=500, content={"error": "Document upload failed"}
)
except Exception as e:

View File

@ -0,0 +1,42 @@
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
router = APIRouter()
token_manager = TokenManager()
@router.post(
"/upload-file",
summary="upload a document with a given associated_with id.",
description="upload a document. If success, returning the document id and file name",
)
async def upload_file(
associated_with: str = Form(...),
file: UploadFile = File(None)
):
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(
associated_with, file.filename, file_data
)
if document_id:
result = {"document_id": str(document_id), "file_name": file.filename}
return JSONResponse(content=jsonable_encoder(result))
else:
return JSONResponse(
status_code=500, content={"error": "File upload failed"}
)
except Exception as e:
print("this is exception", e)
return JSONResponse(status_code=500, content={"error": "Internal server error"})