File size: 1,212 Bytes
7a6b7b4 |
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 |
"""Module for defining the main routes of the API."""
from pathlib import Path
from fastapi import APIRouter, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from controllers.loader import load_pdf
router = APIRouter(prefix="/file", tags=["mail"])
ALLOWED_FILE_TYPES = {
"application/pdf": ".pdf",
"text/plain": ".txt",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx"
}
@router.get("")
async def get(file: UploadFile = File(...)) -> JSONResponse:
"""
Handles the chat POST request.
Args:
query (ReqData): The request data containing the query parameters.
Returns:
str: The generated response from the chat function.
"""
content = await file.read()
result = []
if file.content_type not in ALLOWED_FILE_TYPES \
or Path(file.filename).suffix.lower() != ALLOWED_FILE_TYPES.get(file.content_type):
raise HTTPException(
status_code=400,
detail="Invalid file type. Only PDF, TXT, and DOCX are allowed."
)
elif file.content_type == "application/pdf":
result = load_pdf(content, file.filename)
return JSONResponse(content=result)
|