Update routes/api.py
Browse files- routes/api.py +49 -0
routes/api.py
CHANGED
@@ -18,6 +18,55 @@ model_provider = ModelProvider()
|
|
18 |
doc_generator = DocumentGenerator(Config.UPLOAD_FOLDER)
|
19 |
_generation_tasks = {}
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def sse_stream_required(f):
|
22 |
"""Decorator to ensure SSE stream has request context"""
|
23 |
@wraps(f)
|
|
|
18 |
doc_generator = DocumentGenerator(Config.UPLOAD_FOLDER)
|
19 |
_generation_tasks = {}
|
20 |
|
21 |
+
@api_bp.route('/convert', methods=['POST'])
|
22 |
+
def convert_markdown():
|
23 |
+
try:
|
24 |
+
data = request.get_json()
|
25 |
+
markdown_content = data.get('markdown')
|
26 |
+
|
27 |
+
if not markdown_content:
|
28 |
+
return jsonify({
|
29 |
+
'success': False,
|
30 |
+
'message': 'No markdown content provided'
|
31 |
+
}), 400
|
32 |
+
|
33 |
+
# Generate unique filenames
|
34 |
+
file_id = str(uuid.uuid4())
|
35 |
+
md_filename = f"{file_id}.md"
|
36 |
+
docx_filename = f"{file_id}.docx"
|
37 |
+
|
38 |
+
# Save markdown to temporary file
|
39 |
+
md_path = os.path.join(Config.UPLOAD_FOLDER, md_filename)
|
40 |
+
with open(md_path, 'w', encoding='utf-8') as f:
|
41 |
+
f.write(markdown_content)
|
42 |
+
|
43 |
+
# Convert to DOCX
|
44 |
+
success = doc_generator.convert_to_word(md_filename, docx_filename)
|
45 |
+
|
46 |
+
# Clean up the markdown file
|
47 |
+
try:
|
48 |
+
os.remove(md_path)
|
49 |
+
except OSError:
|
50 |
+
pass
|
51 |
+
|
52 |
+
if not success:
|
53 |
+
return jsonify({
|
54 |
+
'success': False,
|
55 |
+
'message': 'Conversion failed'
|
56 |
+
}), 500
|
57 |
+
|
58 |
+
return jsonify({
|
59 |
+
'success': True,
|
60 |
+
'filename': docx_filename,
|
61 |
+
'message': 'Conversion successful'
|
62 |
+
})
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
return jsonify({
|
66 |
+
'success': False,
|
67 |
+
'message': str(e)
|
68 |
+
}), 500
|
69 |
+
|
70 |
def sse_stream_required(f):
|
71 |
"""Decorator to ensure SSE stream has request context"""
|
72 |
@wraps(f)
|