Files
TheGrapefruitsDuo/server/app/controllers/base_controller.py
2024-05-05 13:32:31 -07:00

66 lines
2.4 KiB
Python

import logging
import traceback
from datetime import datetime
from pathlib import Path
from fastapi import HTTPException, UploadFile, status
from icecream import ic
from app.constants import ALLOWED_FILES_TYPES, ONE_MB
from app.db.base_queries import BaseQueries
class BaseController:
"""
A generic controller class which includes logging, image verification, and other common methods.
Model-specific controllers should inherit from this class and this class should not be instantiated directly.
"""
def __init__(self) -> None:
"""
Initializes the BaseController with a BaseQueries object.
"""
self.db: BaseQueries = None # type: ignore
self.ALL_FILES = ALLOWED_FILES_TYPES
self.MAX_FILE_SIZE = ONE_MB
def verify_image(self, file: UploadFile) -> bytes:
"""
Verifies that the file is an image and is within the maximum file size.
:param UploadFile file: The file to be verified
:raises HTTPException: If the file type is not allowed (status code 400)
:raises HTTPException: If the file size exceeds the maximum (status code 400)
:return bytes: The image file as bytes
"""
if file.content_type not in self.ALL_FILES:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"File type {file.content_type} not allowed. Allowed file types are {self.ALL_FILES}",
)
with file.file as f:
image_file = f.read()
if len(image_file) > self.MAX_FILE_SIZE:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"File size {len(image_file)} bytes exceeds maximum of {self.MAX_FILE_SIZE} bytes",
)
return image_file
def log_error(self, e: Exception) -> None:
"""
Logs an error to a timestamped text file in the logs directory.
:param Exception e: The exception to be logged
"""
curr_dir = Path(__file__).parent
log_dir = curr_dir / "logs"
log_dir.mkdir(exist_ok=True)
log_file = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log"
with open(log_dir / log_file, "w") as f:
f.write(f"{type(e)}")
f.write("\n\n")
f.write(str(e))
f.write("\n\n")
f.write(traceback.format_exc())