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