sifars-chatbot-demo / src /controllers /_file_controller.py
Aryan Jain
add tool calls and implemented knowledge base
e1fb2f2
import os
from fastapi import APIRouter, HTTPException, UploadFile
from fastapi.responses import JSONResponse
from src.utils import logger
from src.services import FileService
import aiofiles
class FileController:
def __init__(self):
self.file_service = FileService
self.router = APIRouter(prefix="/upload_file", tags=["file_upload"])
self.router.add_api_route("/", self.upload_file, methods=["POST"])
async def upload_file(self, file: UploadFile):
try:
async with aiofiles.tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = os.path.join(temp_dir, file.filename)
async with aiofiles.open(temp_file_path, "wb") as f:
await f.write(file.file.read())
async with self.file_service() as file_service:
response = await file_service.upload_file(temp_file_path)
return JSONResponse(
{
"status": "success",
"data": {"web_url": response},
}
)
except Exception as e:
logger.error(e)
raise HTTPException(status_code=500, detail=str(e))