File size: 1,360 Bytes
040da4c
adb221d
 
040da4c
 
adb221d
 
040da4c
 
 
 
 
 
 
 
adb221d
 
 
 
 
 
 
 
 
040da4c
 
 
7694e9d
 
 
1f321e3
040da4c
 
 
 
 
7694e9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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))