add upload document api
This commit is contained in:
parent
a604597209
commit
154c0a4d36
@ -5,36 +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 with a given associated_with id.",
|
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(
|
||||||
associated_with: str = Form(...),
|
item:Item
|
||||||
file: UploadFile = File(None)
|
|
||||||
):
|
):
|
||||||
|
|
||||||
document_hub = DocumentHub()
|
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(
|
||||||
associated_with, file.filename, file_data
|
item.associated_with, item.name, item.blob
|
||||||
)
|
)
|
||||||
|
|
||||||
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