79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
import os
|
|
import json
|
|
import aiohttp
|
|
from aiohttp import web
|
|
import server
|
|
|
|
# --- Configuration ---
|
|
# Where to save received workflows on the destination machine
|
|
SAVE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../user/default/workflows"))
|
|
# If the above path doesn't work for your setup, use the output folder:
|
|
# SAVE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../output/synced_workflows"))
|
|
|
|
if not os.path.exists(SAVE_DIR):
|
|
os.makedirs(SAVE_DIR)
|
|
|
|
class NetworkSync:
|
|
def __init__(self):
|
|
pass
|
|
|
|
# 1. RECEIVE Endpoint (Runs on the Destination Machine)
|
|
@server.PromptServer.instance.routes.post("/network-sync/receive")
|
|
async def receive_workflow(request):
|
|
try:
|
|
data = await request.json()
|
|
filename = data.get("name", "synced_workflow")
|
|
workflow = data.get("workflow")
|
|
|
|
if not filename.endswith(".json"):
|
|
filename += ".json"
|
|
|
|
# Sanitize filename to prevent directory traversal
|
|
filename = os.path.basename(filename)
|
|
save_path = os.path.join(SAVE_DIR, filename)
|
|
|
|
with open(save_path, "w", encoding="utf-8") as f:
|
|
json.dump(workflow, f, indent=2)
|
|
|
|
print(f"[NetworkSync] Workflow saved to: {save_path}")
|
|
return web.json_response({"status": "success", "message": f"Saved {filename}"})
|
|
|
|
except Exception as e:
|
|
print(f"[NetworkSync] Error receiving: {e}")
|
|
return web.json_response({"status": "error", "message": str(e)}, status=500)
|
|
|
|
# 2. PUSH Endpoint (Runs on the Source Machine)
|
|
@server.PromptServer.instance.routes.post("/network-sync/push")
|
|
async def push_workflow(request):
|
|
try:
|
|
data = await request.json()
|
|
target_ip = data.get("target_ip") # e.g., "192.168.1.5:8188"
|
|
filename = data.get("name")
|
|
workflow = data.get("workflow")
|
|
|
|
if not target_ip:
|
|
return web.json_response({"status": "error", "message": "No target IP provided"}, status=400)
|
|
|
|
# Ensure protocol is present
|
|
if not target_ip.startswith("http"):
|
|
target_url = f"http://{target_ip}/network-sync/receive"
|
|
else:
|
|
target_url = f"{target_ip}/network-sync/receive"
|
|
|
|
payload = {"name": filename, "workflow": workflow}
|
|
|
|
# Send from Python (Server A) to Python (Server B)
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.post(target_url, json=payload) as resp:
|
|
if resp.status == 200:
|
|
return web.json_response({"status": "success", "message": "Synced successfully!"})
|
|
else:
|
|
text = await resp.text()
|
|
return web.json_response({"status": "error", "message": f"Remote error: {text}"}, status=resp.status)
|
|
|
|
except Exception as e:
|
|
print(f"[NetworkSync] Error pushing: {e}")
|
|
return web.json_response({"status": "error", "message": str(e)}, status=500)
|
|
|
|
NODE_CLASS_MAPPINGS = {}
|
|
WEB_DIRECTORY = "./web" |