-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (54 loc) · 2.03 KB
/
Copy pathserver.py
File metadata and controls
67 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, send_file, jsonify
from src.converter.converter import MarkdownToPptConverter
import os
import tempfile
from werkzeug.utils import secure_filename
app = Flask(__name__)
# Configure temp directory for file operations
TEMP_DIR = tempfile.gettempdir()
@app.route('/api/healthcheck')
def healthcheck():
return jsonify({"status": "healthy"})
@app.route('/convert', methods=['POST', 'OPTIONS'])
def convert():
if request.method == 'OPTIONS':
# Handle CORS preflight request
response = jsonify({'status': 'ok'})
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
response.headers.add('Access-Control-Allow-Methods', 'POST')
return response
try:
# Get markdown content from request
data = request.get_json()
markdown_content = data.get('markdown')
if not markdown_content:
return jsonify({'error': 'No markdown content provided'}), 400
# Create temporary file paths
temp_dir = tempfile.mkdtemp()
output_file = os.path.join(temp_dir, 'presentation.pptx')
# Convert markdown to PPT
converter = MarkdownToPptConverter(markdown_content, output_file, mode=0)
converter.convert()
# Send the file
response = send_file(
output_file,
mimetype='application/vnd.openxmlformats-officedocument.presentationml.presentation',
as_attachment=True,
download_name='presentation.pptx'
)
# Add CORS headers
response.headers.add('Access-Control-Allow-Origin', '*')
return response
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
# Clean up temporary files
if os.path.exists(output_file):
try:
os.remove(output_file)
os.rmdir(temp_dir)
except:
pass
if __name__ == '__main__':
app.run(debug=True)