-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_routes.py
More file actions
53 lines (40 loc) · 1.86 KB
/
api_routes.py
File metadata and controls
53 lines (40 loc) · 1.86 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
"""
API routes for ShaderNoiseKSampler extension.
Provides server-side endpoints for saving shader parameters from the frontend.
"""
from aiohttp import web
import json
import os
from .shader_params_reader import ShaderParamsReader
# Get extension directory
EXTENSION_DIR = os.path.dirname(os.path.abspath(__file__))
async def save_shader_params(request):
"""
API endpoint to save shader parameters to JSON file.
Receives JSON data from the frontend and writes it to data/shader_params.json.
This enables automatic parameter persistence without manual file downloads.
"""
try:
data = await request.json()
params_file = os.path.join(EXTENSION_DIR, "data", "shader_params.json")
# Ensure data directory exists
os.makedirs(os.path.dirname(params_file), exist_ok=True)
# Validate and sanitize input data before saving
# This prevents storing potentially malicious or malformed data
sanitized_data = ShaderParamsReader.validate_and_sanitize_params(data)
with open(params_file, 'w') as f:
json.dump(sanitized_data, f, indent=2)
print(f"[ShaderNoiseKSampler] Saved shader params to {params_file}")
return web.json_response({"status": "success"})
except Exception as e:
print(f"[ShaderNoiseKSampler] Error saving shader params: {e}")
return web.json_response({"status": "error", "message": str(e)}, status=500)
def setup_routes(server):
"""
Register API routes with ComfyUI's PromptServer.
Args:
server: The PromptServer instance from ComfyUI
"""
if hasattr(server, 'app') and hasattr(server.app, 'router'):
server.app.router.add_post("/shader_noise_ksampler/save_params", save_shader_params)
print("[ShaderNoiseKSampler] Registered API route: POST /shader_noise_ksampler/save_params")