import os import tempfile from fastapi import UploadFile async def save_upload_to_temp(file: UploadFile) -> str: """Salva o upload em um arquivo temporario e devolve o caminho.""" suffix = os.path.splitext(file.filename or "")[1] or ".bin" fd, path = tempfile.mkstemp(suffix=suffix) with os.fdopen(fd, "wb") as out: out.write(await file.read()) return path def cleanup(*paths: str | None) -> None: """Remove arquivos temporarios, ignorando o que nao existir.""" for p in paths: if p and os.path.exists(p): try: os.remove(p) except OSError: pass