33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
from app.config import get_settings
|
|
from app.vision.preprocess import PreparedImage
|
|
|
|
|
|
def save_upload(prepared: PreparedImage, *, user_id: int) -> str:
|
|
settings = get_settings()
|
|
user_dir = Path(settings.uploads_dir) / str(user_id)
|
|
user_dir.mkdir(parents=True, exist_ok=True)
|
|
name = f"{uuid.uuid4().hex}.jpg"
|
|
path = user_dir / name
|
|
path.write_bytes(prepared.jpeg_bytes)
|
|
return name
|
|
|
|
|
|
def upload_media_path(user_id: int, filename: str) -> str:
|
|
return f"/api/v1/media/uploads/{user_id}/{filename}"
|
|
|
|
|
|
def format_upload_images_markdown(user_id: int, filenames: list[str]) -> str:
|
|
if not filenames:
|
|
return ""
|
|
total = len(filenames)
|
|
lines: list[str] = []
|
|
for index, name in enumerate(filenames, start=1):
|
|
alt = f"скриншот {index}/{total}" if total > 1 else "скриншот"
|
|
lines.append(f"})")
|
|
return "\n".join(lines)
|