Stable Diffusion WebUI integration. File CORP proxy.
This commit is contained in:
@@ -5,7 +5,7 @@ import signal
|
||||
import os
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Route
|
||||
from starlette.responses import Response, StreamingResponse
|
||||
from starlette.responses import Response, StreamingResponse, FileResponse
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
from mcp_logic import MCPServer
|
||||
@@ -75,10 +75,35 @@ async def messages_endpoint(request):
|
||||
await mcp_logic.send_to_session(sid, {"jsonrpc": "2.0", "id": req_id, "result": result})
|
||||
return Response(status_code=202)
|
||||
|
||||
async def file_endpoint(request):
|
||||
"""
|
||||
Proxy endpoint to serve files from disk with CORP/CORS headers to bypass browser restrictions.
|
||||
"""
|
||||
path = request.path_params.get("path")
|
||||
if not path:
|
||||
return Response("Path not provided", status_code=400)
|
||||
|
||||
# Ensure the path is absolute (SDFiles are usually in /tmp/gradio/...)
|
||||
# If the path doesn't start with /, we assume it's relative to root for simplicity in this context
|
||||
full_path = path if path.startswith("/") else f"/{path}"
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
return Response(f"File not found: {full_path}", status_code=404)
|
||||
|
||||
return FileResponse(
|
||||
full_path,
|
||||
headers={
|
||||
"Cross-Origin-Resource-Policy": "cross-origin",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET"
|
||||
}
|
||||
)
|
||||
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Route("/sse", endpoint=sse_endpoint),
|
||||
Route("/messages", endpoint=messages_endpoint, methods=["POST"]),
|
||||
Route("/file/{path:path}", endpoint=file_endpoint),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user