fix: refact logic

This commit is contained in:
2025-12-31 17:38:32 +08:00
parent 6ac50f7d2f
commit 35928c2484
17 changed files with 678 additions and 738 deletions

View File

@@ -3,34 +3,28 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import Response
from app.core.dependencies import get_docx_converter
from app.core.dependencies import get_converter
from app.schemas.convert import MarkdownToDocxRequest
from app.services.docx_converter import DocxConverter
from app.services.converter import Converter
router = APIRouter()
@router.post("/docx")
@router.post("/file")
async def convert_markdown_to_docx(
request: MarkdownToDocxRequest,
converter: DocxConverter = Depends(get_docx_converter),
converter: Converter = Depends(get_converter),
) -> Response:
"""Convert markdown content to DOCX file.
Returns the generated DOCX file as a binary download.
Returns the generated DOCX file as a binary response.
"""
try:
docx_bytes = converter.convert(request.markdown)
docx_bytes = converter.export_to_file(request.markdown, export_type="docx")
return Response(
content=docx_bytes,
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
headers={"Content-Disposition": f'attachment; filename="{request.filename}.docx"'},
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Conversion failed: {e}")
# Determine filename
filename = request.filename or "output"
if not filename.endswith(".docx"):
filename = f"{filename}.docx"
return Response(
content=docx_bytes,
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)