import mimetypes import re import uuid from pathlib import Path import magic from fastapi import HTTPException, UploadFile from folkugat_web.config import db async def get_mimetype(upload_file: UploadFile) -> str: info = magic.detect_from_content(await upload_file.read(2048)) await upload_file.seek(0) return info.mime_type ACCEPTED_MIMETYPES = [ re.compile(r"image/.+"), re.compile(r".+/pdf"), ] def check_mimetype(mimetype: str) -> None: if not any(regex.match(mimetype) for regex in ACCEPTED_MIMETYPES): raise HTTPException(status_code=400, detail=f"Unsupported file type: {mimetype}") def get_db_file_path(filepath: Path) -> str: return f"{db.DB_FILES_URL}/{filepath.relative_to(db.DB_FILES_DIR)}" async def store_file(tema_id: int, upload_file: UploadFile) -> str: if not upload_file.size: raise HTTPException(status_code=400, detail="Couldn't find out the size of the file") if upload_file.size > db.FILE_MAX_SIZE: raise HTTPException( status_code=400, detail=f"The uploaded file is too big (max size = {db.FILE_MAX_SIZE} bytes)", ) mimetype = await get_mimetype(upload_file) check_mimetype(mimetype) extension = mimetypes.guess_extension(mimetype) or "" filename = str(uuid.uuid4().hex) + extension filedir = db.DB_FILES_DIR / str(tema_id) filedir.mkdir(exist_ok=True) filepath = filedir / filename with open(filepath, "wb") as f: f.write(await upload_file.read()) return get_db_file_path(filepath) def list_files(tema_id: str) -> list[str]: filedir = db.DB_FILES_DIR / str(tema_id) return [get_db_file_path(f) for f in filedir.iterdir()]