38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
|
|
"""Markdown to DOCX conversion endpoint."""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException
|
||
|
|
from fastapi.responses import Response
|
||
|
|
|
||
|
|
from app.core.dependencies import get_docx_converter
|
||
|
|
from app.schemas.convert import MarkdownToDocxRequest
|
||
|
|
from app.services.docx_converter import DocxConverter
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/docx")
|
||
|
|
async def convert_markdown_to_docx(
|
||
|
|
request: MarkdownToDocxRequest,
|
||
|
|
converter: DocxConverter = Depends(get_docx_converter),
|
||
|
|
) -> Response:
|
||
|
|
"""Convert markdown content to DOCX file.
|
||
|
|
|
||
|
|
Returns the generated DOCX file as a binary download.
|
||
|
|
"""
|
||
|
|
try:
|
||
|
|
docx_bytes = converter.convert(request.markdown)
|
||
|
|
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}"'},
|
||
|
|
)
|
||
|
|
|