Skip to content

Image Pipeline Playground

feat(backend): support desktop data and frontend dist paths

Mohammad Rajaei0c176d24dcdb
diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py
index 1d831d4..8bc926d 100644
--- a/backend/app/api/routes.py
+++ b/backend/app/api/routes.py
@@ -14,13 +14,14 @@ from app.engine.registry import registry
from app.models.graph import ExecuteRequest, Graph, NodeMetadata
from app.nodes import register_builtin_nodes
from app.nodes.common import IMAGE_EXTENSIONS
+from app.paths import cache_dir, output_dir, upload_dir
from app.services.codegen import generate_python
router = APIRouter(prefix="/api")
-CACHE_DIR = Path(__file__).resolve().parents[2] / "cache"
-UPLOAD_DIR = Path(__file__).resolve().parents[2] / "uploads"
-OUTPUT_DIR = Path(__file__).resolve().parents[2] / "outputs"
+CACHE_DIR = cache_dir()
+UPLOAD_DIR = upload_dir()
+OUTPUT_DIR = output_dir()
class CodegenBody(BaseModel):
@@ -166,13 +167,14 @@ def create_output_directory() -> OutputDirResponse:
@router.get("/downloads/{filename}")
def download_result_archive(filename: str) -> FileResponse:
"""Serve a ZIP produced by Save Image nodes during a pipeline run."""
- from app.engine.save_bundle import DOWNLOAD_DIR
+ from app.engine import save_bundle as save_bundle_module
safe_name = Path(filename).name
if safe_name != filename or not safe_name.endswith(".zip"):
raise HTTPException(status_code=400, detail="Invalid download name")
- target = (DOWNLOAD_DIR / safe_name).resolve()
- downloads_root = DOWNLOAD_DIR.resolve()
+ downloads = save_bundle_module.DOWNLOAD_DIR
+ target = (downloads / safe_name).resolve()
+ downloads_root = downloads.resolve()
if downloads_root not in target.parents and target != downloads_root:
raise HTTPException(status_code=400, detail="Path is outside downloads directory")
if not target.is_file():
diff --git a/backend/app/engine/save_bundle.py b/backend/app/engine/save_bundle.py
index 8cf6576..dfd1ca8 100644
--- a/backend/app/engine/save_bundle.py
+++ b/backend/app/engine/save_bundle.py
@@ -13,7 +13,9 @@ from pathlib import Path
import cv2
import numpy as np
-DOWNLOAD_DIR = Path(__file__).resolve().parents[2] / "outputs" / "downloads"
+from app.paths import download_dir
+
+DOWNLOAD_DIR = download_dir()
@dataclass
diff --git a/backend/app/main.py b/backend/app/main.py
index 47c2d0c..fb53249 100644
--- a/backend/app/main.py
+++ b/backend/app/main.py
@@ -20,6 +20,11 @@ app.add_middleware(
allow_origins=[
"http://localhost:5173",
"http://127.0.0.1:5173",
+ "http://localhost:8765",
+ "http://127.0.0.1:8765",
+ "http://localhost:8000",
+ "http://127.0.0.1:8000",
+ "null",
],
allow_credentials=True,
allow_methods=["*"],
diff --git a/backend/app/paths.py b/backend/app/paths.py
new file mode 100644
index 0000000..64955c7
--- /dev/null
+++ b/backend/app/paths.py
@@ -0,0 +1,41 @@
+"""Shared filesystem locations for cache, uploads, and downloads."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+
+
+def data_root() -> Path:
+ """Return writable app data root (Electron sets IMAGE_PIPES_DATA_DIR)."""
+ override = os.environ.get("IMAGE_PIPES_DATA_DIR")
+ if override:
+ root = Path(override)
+ else:
+ root = Path(__file__).resolve().parents[2]
+ root.mkdir(parents=True, exist_ok=True)
+ return root
+
+
+def cache_dir() -> Path:
+ path = data_root() / "cache"
+ path.mkdir(parents=True, exist_ok=True)
+ return path
+
+
+def upload_dir() -> Path:
+ path = data_root() / "uploads"
+ path.mkdir(parents=True, exist_ok=True)
+ return path
+
+
+def output_dir() -> Path:
+ path = data_root() / "outputs"
+ path.mkdir(parents=True, exist_ok=True)
+ return path
+
+
+def download_dir() -> Path:
+ path = output_dir() / "downloads"
+ path.mkdir(parents=True, exist_ok=True)
+ return path
diff --git a/backend/app/services/static.py b/backend/app/services/static.py
index a288a03..4d0695f 100644
--- a/backend/app/services/static.py
+++ b/backend/app/services/static.py
@@ -1,5 +1,9 @@
"""Production helpers for serving the built React app."""
+from __future__ import annotations
+
+import os
+import sys
from pathlib import Path
from fastapi import FastAPI
@@ -7,10 +11,33 @@ from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
-def mount_frontend(app: FastAPI, dist_dir: Path | None = None) -> None:
+def resolve_frontend_dist(dist_dir: Path | None = None) -> Path | None:
+ if dist_dir is not None:
+ return dist_dir if dist_dir.exists() else None
+
+ env = os.environ.get("IMAGE_PIPES_FRONTEND_DIST")
+ if env:
+ candidate = Path(env)
+ return candidate if candidate.exists() else None
+
+ if getattr(sys, "frozen", False):
+ bundle_dir = Path(getattr(sys, "_MEIPASS", Path(sys.executable).parent))
+ for candidate in (
+ bundle_dir / "frontend" / "dist",
+ Path(sys.executable).resolve().parent / "frontend" / "dist",
+ ):
+ if candidate.exists():
+ return candidate
+ return None
+
root = Path(__file__).resolve().parents[3]
- dist = dist_dir or (root / "frontend" / "dist")
- if not dist.exists():
+ candidate = root / "frontend" / "dist"
+ return candidate if candidate.exists() else None
+
+
+def mount_frontend(app: FastAPI, dist_dir: Path | None = None) -> None:
+ dist = resolve_frontend_dist(dist_dir)
+ if dist is None:
return
assets = dist / "assets"
diff --git a/backend/app/websocket/execution.py b/backend/app/websocket/execution.py
index 92d46fd..9ee3aa7 100644
--- a/backend/app/websocket/execution.py
+++ b/backend/app/websocket/execution.py
@@ -3,7 +3,6 @@
from __future__ import annotations
import asyncio
-from pathlib import Path
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from starlette.websockets import WebSocketState
@@ -11,11 +10,10 @@ from starlette.websockets import WebSocketState
from app.engine.executor import CancellationToken, DagExecutor
from app.models.graph import ExecuteRequest, ExecutionEvent, ExecutionEventType
from app.nodes import register_builtin_nodes
+from app.paths import cache_dir
router = APIRouter()
-CACHE_DIR = Path(__file__).resolve().parents[2] / "cache"
-
@router.websocket("/ws/execute")
async def execute_pipeline(websocket: WebSocket) -> None:
@@ -33,7 +31,7 @@ async def execute_pipeline(websocket: WebSocket) -> None:
loop.call_soon_threadsafe(queue.put_nowait, event)
def run_executor() -> None:
- executor = DagExecutor(CACHE_DIR)
+ executor = DagExecutor(cache_dir())
try:
executor.execute(request, on_event=on_event, cancel=cancel)
except InterruptedError: