init repo
This commit is contained in:
37
app/api/v1/endpoints/convert.py
Normal file
37
app/api/v1/endpoints/convert.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""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}"'},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user