| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- from fastapi import APIRouter
- from app.api.v1 import ok
- from app.core.exceptions import ExportError
- from app.schemas.export import ExportDocRequest, ExportDocResponse
- from app.services.export_service import export_doc
- from app.core.database import AsyncSessionLocal
- from app.schemas.document import UpdateDocumentRequest
- from app.services.document_service import DocumentService
- router = APIRouter(tags=["Export"])
- # ------------------------------------------------------------------ #
- # POST /export/doc — 导出 .doc,写入记录,返回永久下载链接
- # ------------------------------------------------------------------ #
- @router.post("/export/doc", summary="导出 .doc 文件")
- async def export_document(body: ExportDocRequest) -> dict:
- # 若传入 documentId,可选同步更新草稿(失败不阻塞导出)
- if body.document_id:
- try:
- async with AsyncSessionLocal() as db:
- svc = DocumentService(db)
- await svc.update_document(
- body.document_id,
- UpdateDocumentRequest(content=body.content),
- )
- except Exception:
- pass
- result = await export_doc(
- user_id=body.user_id,
- file_name=body.file_name,
- content=body.content,
- style_id=body.style_id,
- document_id=body.document_id,
- )
- return ok(ExportDocResponse(
- record_id=result["record_id"],
- download_url=result["download_url"],
- file_name=result["file_name"],
- style_id=result["style_id"],
- warning=result["warning"],
- ).model_dump(by_alias=True))
|