-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (76 loc) · 3 KB
/
app.py
File metadata and controls
97 lines (76 loc) · 3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import uuid
from flask import Flask, request, jsonify, send_file
import pypandoc
from werkzeug.utils import secure_filename
app = Flask(__name__)
# Create uploads directory if it doesn't exist
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/convert', methods=['POST'])
def convert_text():
try:
data = request.get_json()
if not data or 'text' not in data or 'output_format' not in data:
return jsonify({'error': 'Missing required fields: text and output_format'}), 400
text = data['text']
output_format = data['output_format']
# Generate a unique filename
filename = f"{uuid.uuid4()}.{output_format}"
output_path = os.path.join(UPLOAD_FOLDER, filename)
extra_args = []
if output_format == 'pdf':
extra_args = ['--pdf-engine=xelatex','-V','CJKmainfont=Noto Sans CJK SC']
# Convert text using pandoc
pypandoc.convert_text(
text,
output_format,
format='markdown',
outputfile=output_path,
extra_args=extra_args
)
return jsonify({
'message': 'File converted successfully',
'filename': filename,
'download_url': f'/download/{filename}'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
try:
# Secure the filename to prevent directory traversal
safe_filename = secure_filename(filename)
file_path = os.path.join(UPLOAD_FOLDER, safe_filename)
if not os.path.exists(file_path):
return jsonify({'error': 'File not found'}), 404
return send_file(
file_path,
as_attachment=True,
download_name=safe_filename
)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/delete/<filename>', methods=['DELETE'])
def delete_file(filename):
try:
# Secure the filename to prevent directory traversal
safe_filename = secure_filename(filename)
file_path = os.path.join(UPLOAD_FOLDER, safe_filename)
# Check if file exists
if not os.path.exists(file_path):
return jsonify({'error': 'File not found'}), 404
# Check if file is within the uploads directory
if not os.path.abspath(file_path).startswith(os.path.abspath(UPLOAD_FOLDER)):
return jsonify({'error': 'Invalid file path'}), 400
# Delete the file
os.remove(file_path)
return jsonify({
'message': 'File deleted successfully',
'filename': safe_filename
})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)