34 lines
939 B
Python
34 lines
939 B
Python
"""Helpers de armazenamento seguro dos arquivos enviados."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from .config import get_settings
|
|
|
|
|
|
def safe_original_name(name: str) -> str:
|
|
return os.path.basename(name or "").replace("\x00", "") or "upload"
|
|
|
|
|
|
def unique_storage_name(original: str) -> str:
|
|
upload_dir = get_settings().upload_dir
|
|
stem = re.sub(r"[^A-Za-z0-9_.-]+", "_", Path(original).stem).strip("._") or "upload"
|
|
suffix = Path(original).suffix.lower()
|
|
index = 0
|
|
while True:
|
|
candidate = f"{stem}{'-' + str(index) if index else ''}{suffix}"
|
|
if not (upload_dir / candidate).exists():
|
|
return candidate
|
|
index += 1
|
|
|
|
|
|
def money(value: object) -> str:
|
|
try:
|
|
amount = float(value or 0)
|
|
except (TypeError, ValueError):
|
|
amount = 0.0
|
|
return f"R$ {amount:,.2f}".replace(",", "X").replace(".", ",").replace("X", ".")
|