-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (40 loc) · 1.42 KB
/
app.py
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
from flask import Flask, render_template, request, send_from_directory, jsonify
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
return render_template('editor.html')
@app.route('/save', methods=['POST'])
def save():
filename = request.form['filename']
content = request.form['content']
if not filename:
return 'Filename is required', 400
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
with open(filepath, 'w') as file:
file.write(content)
return 'File saved successfully'
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route('/search')
def search():
query = request.args.get('query')
results = []
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
if query.lower() in filename.lower():
results.append(filename)
return jsonify({'results': results})
@app.route('/load', methods=['GET'])
def load():
filename = request.args.get('filename')
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if os.path.exists(filepath):
with open(filepath, 'r') as file:
content = file.read()
return jsonify({'filename': filename, 'content': content})
return 'File not found', 404
if __name__ == '__main__':
app.run(debug=True)