mandala-for-us / src /controllers /_file_controller.py
kanha-upadhyay's picture
add documentation comments
adb221d
from typing import Dict
from fastapi import APIRouter, HTTPException, UploadFile
from src.services import FileService
from src.utils import logger
class FileController:
def __init__(self):
self.router = APIRouter(prefix="/files", tags=["FILES"])
self.router.add_api_route("/", self.parse_document, methods=["POST"])
self.file_service = FileService()
async def parse_document(self, file: UploadFile) -> Dict:
"""
Parse a document and save it to the database
Args:
file (UploadFile): The file to parse
Returns:
Dict: A dictionary with a message indicating the file was parsed
"""
logger.info(f"Parsing file: {file.filename}")
try:
async with self.file_service as service:
file_path = await service.save_uploaded_file(file)
file_name = file.filename
result = await service.parse_document(file_name, file_path)
return {"message": "file was successfully parsed"}
except ValueError as e:
logger.warning(f"Validation error: {str(e)}")
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error(f"Error parsing document: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))