-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.py
More file actions
42 lines (35 loc) · 1.48 KB
/
router.py
File metadata and controls
42 lines (35 loc) · 1.48 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
from aiohttp import web
import os
import folder_paths
TEMP_DIR = folder_paths.get_temp_directory()
BRIDGE_DIR = os.path.join(TEMP_DIR, "tensor_bridge")
os.makedirs(BRIDGE_DIR, exist_ok=True)
async def upload_tensor_handler(request):
try:
reader = await request.multipart()
channel_name = "default"
file_written = False
bytes_written = 0
field = await reader.next()
while field:
if field.name == 'channel':
value = await field.read(decode=True)
channel_name = value.decode("utf-8")
elif field.name == 'file':
filename = f"{channel_name}.pt"
filepath = os.path.join(BRIDGE_DIR, filename)
# Write stream to disk
with open(filepath, "wb") as f:
while True:
chunk = await field.read_chunk()
if not chunk: break
f.write(chunk)
bytes_written += len(chunk)
file_written = True
field = await reader.next()
if file_written:
return web.json_response({"status": "success", "channel": channel_name, "size": bytes_written})
return web.json_response({"status": "error", "message": "No file field found"}, status=400)
except Exception as e:
print(f"[TensorBridge] Upload Error: {e}")
return web.json_response({"status": "error", "message": str(e)}, status=500)