This commit is contained in:
root
2026-05-28 15:40:02 +00:00
parent 81913f3d8d
commit cc1ea166c8
5 changed files with 75 additions and 40 deletions

View File

@@ -1,12 +1,11 @@
import asyncio
import uuid
from datetime import datetime, timezone
from pathlib import Path
from fastapi import APIRouter, HTTPException
from fastapi.responses import FileResponse
from config import FORMAT_META, STORAGE_DIR
from config import FORMAT_META
from database import (
create_guide, delete_guide, get_guide, list_guides,
create_baustein as db_create_baustein, list_bausteine, get_baustein, delete_baustein as db_delete_baustein,
@@ -17,6 +16,7 @@ from models import (
GuideCreateRequest, GuideReworkRequest, GuideResponse,
BausteinCreateRequest, BausteinResponse, SuggestionResponse,
)
from paths import final_paths
router = APIRouter(prefix="/api")
@@ -36,8 +36,6 @@ async def create(req: GuideCreateRequest):
"instructions": req.instructions.strip(),
"status": "queued",
"progress": None,
"html_path": None,
"pdf_path": None,
"created_at": now,
"updated_at": now,
}
@@ -64,12 +62,12 @@ async def download_html(guide_id: str):
guide = await get_guide(guide_id)
if guide is None:
raise HTTPException(404, "Guide nicht gefunden")
if guide["status"] != "done" or not guide["html_path"]:
if guide["status"] != "done":
raise HTTPException(404, "HTML nicht verfügbar")
path = Path(guide["html_path"])
if not path.exists():
html_path, _ = final_paths(guide["topic"], guide["format"])
if not html_path.exists():
raise HTTPException(404, "Datei nicht gefunden")
return FileResponse(path, filename=f"{guide['topic']}-{guide['format']}.html", media_type="text/html")
return FileResponse(html_path, filename=html_path.name, media_type="text/html")
@router.post("/guides/{guide_id}/rework")
@@ -96,12 +94,12 @@ async def download_pdf(guide_id: str):
guide = await get_guide(guide_id)
if guide is None:
raise HTTPException(404, "Guide nicht gefunden")
if guide["status"] != "done" or not guide["pdf_path"]:
if guide["status"] != "done":
raise HTTPException(404, "PDF nicht verfügbar")
path = Path(guide["pdf_path"])
if not path.exists():
_, pdf_path = final_paths(guide["topic"], guide["format"])
if not pdf_path.exists():
raise HTTPException(404, "Datei nicht gefunden")
return FileResponse(path, filename=f"{guide['topic']}-{guide['format']}.pdf", media_type="application/pdf")
return FileResponse(pdf_path, filename=pdf_path.name, media_type="application/pdf")
@router.delete("/guides/{guide_id}")
@@ -109,10 +107,9 @@ async def remove(guide_id: str):
guide = await get_guide(guide_id)
if guide is None:
raise HTTPException(404, "Guide nicht gefunden")
if guide["html_path"]:
Path(guide["html_path"]).unlink(missing_ok=True)
if guide["pdf_path"]:
Path(guide["pdf_path"]).unlink(missing_ok=True)
html_path, pdf_path = final_paths(guide["topic"], guide["format"])
html_path.unlink(missing_ok=True)
pdf_path.unlink(missing_ok=True)
await delete_guide(guide_id)
return {"ok": True}
@@ -165,8 +162,10 @@ async def trigger_suggestions(topic: str):
guides = await list_guides()
html_paths = []
for g in guides:
if g["topic"] == topic and g["status"] == "done" and g["html_path"]:
html_paths.append(Path(g["html_path"]))
if g["topic"] == topic and g["status"] == "done":
html_path, _ = final_paths(g["topic"], g["format"])
if html_path.exists():
html_paths.append(html_path)
asyncio.create_task(generate_suggestions(topic, html_paths))
return {"ok": True}