export.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from fastapi import APIRouter
  2. from app.api.v1 import ok
  3. from app.core.exceptions import ExportError
  4. from app.schemas.export import ExportDocRequest, ExportDocResponse
  5. from app.services.export_service import export_doc
  6. from app.core.database import AsyncSessionLocal
  7. from app.schemas.document import UpdateDocumentRequest
  8. from app.services.document_service import DocumentService
  9. router = APIRouter(tags=["Export"])
  10. # ------------------------------------------------------------------ #
  11. # POST /export/doc — 导出 .doc,写入记录,返回永久下载链接
  12. # ------------------------------------------------------------------ #
  13. @router.post("/export/doc", summary="导出 .doc 文件")
  14. async def export_document(body: ExportDocRequest) -> dict:
  15. # 若传入 documentId,可选同步更新草稿(失败不阻塞导出)
  16. if body.document_id:
  17. try:
  18. async with AsyncSessionLocal() as db:
  19. svc = DocumentService(db)
  20. await svc.update_document(
  21. body.document_id,
  22. UpdateDocumentRequest(content=body.content),
  23. )
  24. except Exception:
  25. pass
  26. result = await export_doc(
  27. user_id=body.user_id,
  28. file_name=body.file_name,
  29. content=body.content,
  30. style_id=body.style_id,
  31. document_id=body.document_id,
  32. )
  33. return ok(ExportDocResponse(
  34. record_id=result["record_id"],
  35. download_url=result["download_url"],
  36. file_name=result["file_name"],
  37. style_id=result["style_id"],
  38. warning=result["warning"],
  39. ).model_dump(by_alias=True))