forked from intelligencedev/PromptForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (77 loc) · 3.11 KB
/
app.py
File metadata and controls
97 lines (77 loc) · 3.11 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
from flask import Flask, send_from_directory, jsonify, request, make_response
import os
import json
app = Flask(__name__, static_folder='.')
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
# Serve images with caching completely disabled
@app.route('/images/<path:filename>')
def serve_images(filename):
file_path = os.path.join('images', filename)
if not os.path.isfile(file_path):
return jsonify({"error": "Image not found"}), 404
# Serve the file with no caching
response = make_response(send_from_directory('images', filename))
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response
@app.route('/api/pages', methods=['GET'])
def list_pages():
files = [
f for f in os.listdir('.')
if f.endswith('.json') and f not in ['prompts.json', 'package.json', 'tsconfig.json']
]
return jsonify(files)
@app.route('/api/pages/<path:filename>', methods=['GET'])
def get_page(filename):
try:
with open(filename, 'r') as f:
data = json.load(f)
return jsonify(data)
except FileNotFoundError:
return jsonify({"error": "File not found"}), 404
except json.JSONDecodeError:
return jsonify({"error": "Invalid JSON"}), 500
@app.route('/api/pages', methods=['POST'])
def save_page():
data = request.json
filename = data.get('filename')
content = data.get('content')
if not filename or content is None:
return jsonify({"error": "Missing filename or content"}), 400
if not filename.endswith('.json'):
filename += '.json'
if '..' in filename or '/' in filename or '\\' in filename:
return jsonify({"error": "Invalid filename"}), 400
try:
with open(filename, 'w') as f:
json.dump(content, f, indent=2)
return jsonify({"message": "Saved successfully", "filename": filename})
except Exception as e:
return jsonify({"error": str(e)}), 500
# ----------------------------
# Image upload with New Prompt safety
# ----------------------------
@app.route('/api/upload-image', methods=['POST'])
def upload_image():
file = request.files.get('image')
prompt_tag = request.form.get('prompt_tag') # Required: prompt card title
if not file or not prompt_tag:
return jsonify({"error": "Missing file or prompt_tag"}), 400
os.makedirs('images', exist_ok=True)
# Convert spaces in prompt_tag to underscores
safe_name = prompt_tag.replace(' ', '_') + '.png'
# Safety: prevent overwriting New_Prompt.png
if prompt_tag.strip() == "New Prompt":
return jsonify({"error": "Please edit the card name before adding an image!"}), 400
file_path = os.path.join('images', safe_name)
# Save the file
try:
file.save(file_path)
except Exception as e:
return jsonify({"error": f"Failed to save image: {str(e)}"}), 500
return jsonify({"message": "Image saved", "filename": safe_name})
if __name__ == '__main__':
app.run(debug=True, port=5000)