File size: 673 Bytes
1b64148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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