diff --git a/backend/routes/license_routes.py b/backend/routes/license_routes.py new file mode 100644 index 0000000..9bbcc11 --- /dev/null +++ b/backend/routes/license_routes.py @@ -0,0 +1,36 @@ +from typing import Any +from fastapi import APIRouter, Form, HTTPException, BackgroundTasks +from fastapi.responses import FileResponse +from ..services.license_service import generate_license_file + +router: APIRouter = APIRouter() + +@router.post("/generate", response_class=FileResponse) +async def generate_license( + background_tasks: BackgroundTasks, + name: str = Form(...), + version: str = Form(...) +) -> Any: + """ + Генерация лицензии Custom.mxtpro. + Параметры: + - name: str - имя пользователя + - version: str - версия формата X.Y + Возвращает: + - FileResponse: сгенерированный ZIP файл с именем Custom.mxtpro + """ + if not name.strip() or not version.strip(): + raise HTTPException(status_code=400, detail="NAME и VERSION обязательны.") + try: + # Создаём временный файл на сервере + filepath, temp_dir = generate_license_file(name, version) + except Exception as e: + raise HTTPException(status_code=500, detail=f"Ошибка генерации лицензии: {e}") + # Удаляем директорию (и файл внутри) после отправки пользователю + background_tasks.add_task(temp_dir.cleanup) + # Отправляем с фиксированным именем Custom.mxtpro + return FileResponse( + filepath, + media_type="application/octet-stream", + headers={"Content-Disposition": 'attachment; filename="Custom.mxtpro"'} + )