Merge branch 'master' of https://dev.azure.com/freeleaps/freeleaps-service-hub/_git/freeleaps-service-hub
This commit is contained in:
commit
05cc8f9d60
@ -4,22 +4,24 @@ from app.central_storage.backend.business.document_manager import (
|
|||||||
|
|
||||||
|
|
||||||
class DocumentHub:
|
class DocumentHub:
|
||||||
def __init__(self, user_id: str):
|
def __init__(self, ):
|
||||||
self.user_id = user_id
|
|
||||||
self.document_manager = DocumentManager(self.user_id)
|
self.document_manager = DocumentManager(self.user_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
async def get_document_by_id(self, document_id: str):
|
async def retrieve_document_info(self, document_id: str):
|
||||||
return await self.document_manager.get_document_details_by_id(document_id)
|
return await self.document_manager.retrieve_document_info(document_id)
|
||||||
|
|
||||||
async def upload_document_for_object(
|
async def read_document_file_as_http_media_data(self, document_id: str):
|
||||||
self, object_id: str, file_name: str, file_data: bytes
|
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:
|
) -> bool:
|
||||||
"""Upload a file
|
"""Upload a file
|
||||||
Args:
|
Args:
|
||||||
file_name: the name of the file
|
file_name: the name of the file
|
||||||
file (bytes): the file to be uploaded
|
file (bytes): the file to be uploaded
|
||||||
"""
|
"""
|
||||||
return await self.document_manager.upload_file_for_object(
|
return await self.document_manager.upload_file(
|
||||||
object_id, file_name, file_data
|
associated_with, file_name, file_data
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,11 +3,10 @@ from app.central_storage.backend.models.models import MediaType, DataFormat
|
|||||||
|
|
||||||
|
|
||||||
class DocumentManager:
|
class DocumentManager:
|
||||||
def __init__(self, user_id) -> None:
|
def __init__(self) -> None:
|
||||||
self.user_id = user_id
|
|
||||||
self.document_service = DocumentService()
|
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)
|
await self.document_service.load_document(document_id=document_id)
|
||||||
|
|
||||||
download_link = (
|
download_link = (
|
||||||
@ -19,15 +18,21 @@ class DocumentManager:
|
|||||||
"file_download_url": download_link,
|
"file_download_url": download_link,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def upload_file_for_object(
|
async def read_document_file_as_http_media_data(self, document_id: str):
|
||||||
self, object_id: str, file_name: str, file_data: bytes
|
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:
|
) -> bool:
|
||||||
await self.document_service.new_document(
|
await self.document_service.new_document(
|
||||||
file_name=file_name,
|
file_name=file_name,
|
||||||
# This 'UNKNOWN' will make the document manager decide the media type from file name
|
# This 'UNKNOWN' will make the document manager decide the media type from file name
|
||||||
media_type=MediaType.UNKNOWN,
|
media_type=MediaType.UNKNOWN,
|
||||||
data_format=DataFormat.RAW,
|
data_format=DataFormat.RAW,
|
||||||
created_by=object_id,
|
created_by=associated_with,
|
||||||
)
|
)
|
||||||
return await self.document_service.save_document_file(file_data) or None
|
return await self.document_service.save_document_file(file_data) or None
|
||||||
# TODO: This should go to Freeleaps App
|
# TODO: This should go to Freeleaps App
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
from fastapi import APIRouter
|
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 .upload_document import router as ud_router
|
||||||
|
from .read_document_as_http_media import router as rd_router
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
api_router.include_router(doc_router, tags=["attachment"])
|
api_router.include_router(ri_router, tags=["document"])
|
||||||
api_router.include_router(ud_router, tags=["attachment"])
|
api_router.include_router(ud_router, tags=["document"])
|
||||||
|
api_router.include_router(rd_router, tags=["document"])
|
||||||
|
|
||||||
websocket_router = APIRouter()
|
websocket_router = APIRouter()
|
||||||
|
|||||||
@ -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}))
|
||||||
@ -17,25 +17,18 @@ token_manager = TokenManager()
|
|||||||
# Web API
|
# Web API
|
||||||
# Fetch document by ID
|
# Fetch document by ID
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get-document-by-id/{document_id}",
|
"/retrieve_document_info/{document_id}",
|
||||||
operation_id="get-document-by-id",
|
operation_id="retrieve_document_info",
|
||||||
summary="Fetch a document by its ID",
|
summary="Fetch a document by its ID",
|
||||||
description="Retrieve a specific document by its document ID and return file name and download URL",
|
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",
|
response_description="The document details including file name and download URL",
|
||||||
)
|
)
|
||||||
async def get_document_by_id(
|
async def retrieve_document_info(
|
||||||
document_id: str,
|
document_id: str
|
||||||
current_user: dict = Depends(token_manager.get_current_user),
|
|
||||||
):
|
):
|
||||||
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
|
# 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 document is not found, raise 404 error
|
||||||
if not document:
|
if not document:
|
||||||
@ -5,44 +5,37 @@ from starlette.status import HTTP_401_UNAUTHORIZED
|
|||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from app.central_storage.backend.application.document_hub import DocumentHub
|
from app.central_storage.backend.application.document_hub import DocumentHub
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
token_manager = TokenManager()
|
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
associated_with: str
|
||||||
|
name: str
|
||||||
|
blob: bytes
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/upload-document",
|
"/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",
|
description="upload a document. If success, returning the document id",
|
||||||
)
|
)
|
||||||
async def attach_document_for_request(
|
async def upload_document(
|
||||||
object_id: str = Form(...),
|
item:Item
|
||||||
file: UploadFile = File(None),
|
|
||||||
current_user: dict = Depends(token_manager.get_current_user),
|
|
||||||
):
|
):
|
||||||
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
|
# File processing
|
||||||
try:
|
try:
|
||||||
file_data = await file.read() # You can use async chunking for larger files
|
document_id = await document_hub.upload_document(
|
||||||
document_id = await document_hub.upload_document_for_object(
|
item.associated_with, item.name, item.blob
|
||||||
object_id, file.filename, file_data
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if document_id:
|
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))
|
return JSONResponse(content=jsonable_encoder(result))
|
||||||
else:
|
else:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=500, content={"error": "File upload failed"}
|
status_code=500, content={"error": "Document upload failed"}
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
42
app/central_storage/webapi/routes/upload_file.py
Normal file
42
app/central_storage/webapi/routes/upload_file.py
Normal 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"})
|
||||||
Loading…
Reference in New Issue
Block a user