Image Pipeline Playground
feat(backend/api): allow appending uploads to an existing batch
Mohammad Rajaei0df9301c717e
diff --git a/backend/app/api/routes.py b/backend/app/api/routes.pyindex 83df80d..cb0d43e 100644--- a/backend/app/api/routes.py+++ b/backend/app/api/routes.py@@ -52,13 +52,26 @@ def list_nodes() -> list[NodeMetadata]:async def upload_images(files: list[UploadFile] = File(...),as_folder: bool = False,+ append_to: str | None = None,) -> UploadResponse:if not files:raise HTTPException(status_code=400, detail="No files uploaded")UPLOAD_DIR.mkdir(parents=True, exist_ok=True)- batch_dir = UPLOAD_DIR / uuid.uuid4().hex- batch_dir.mkdir(parents=True, exist_ok=True)+ uploads_root = UPLOAD_DIR.resolve()++ if append_to:+ target_path = Path(append_to).resolve()+ if uploads_root not in target_path.parents and target_path != uploads_root:+ raise HTTPException(status_code=400, detail="Path is outside uploads directory")+ batch_dir = target_path if target_path.is_dir() else target_path.parent+ if uploads_root not in batch_dir.parents and batch_dir != uploads_root:+ raise HTTPException(status_code=400, detail="Path is outside uploads directory")+ if not batch_dir.is_dir():+ raise HTTPException(status_code=404, detail="Append destination not found")+ else:+ batch_dir = UPLOAD_DIR / uuid.uuid4().hex+ batch_dir.mkdir(parents=True, exist_ok=True)saved: list[str] = []for upload in files:@@ -66,6 +79,16 @@ async def upload_images(if not filename or not _allowed_extension(filename):continuetarget = batch_dir / filename+ if target.exists():+ stem = target.stem+ suffix = target.suffix+ index = 2+ while True:+ candidate = batch_dir / f"{stem}_{index}{suffix}"+ if not candidate.exists():+ target = candidate+ break+ index += 1content = await upload.read()if not content:continue@@ -81,7 +104,12 @@ async def upload_images(),)- if as_folder or len(saved) > 1:+ total_in_dir = sum(+ 1+ for path in batch_dir.iterdir()+ if path.is_file() and _allowed_extension(path.name)+ )+ if as_folder or append_to is not None or len(saved) > 1 or total_in_dir > 1:return UploadResponse(path=str(batch_dir.resolve()),kind="folder",diff --git a/backend/tests/test_uploads.py b/backend/tests/test_uploads.pyindex 2f94ad6..3997dbc 100644--- a/backend/tests/test_uploads.py+++ b/backend/tests/test_uploads.py@@ -2,6 +2,8 @@from __future__ import annotations+from pathlib import Path+from fastapi.testclient import TestClientfrom app.main import app@@ -35,3 +37,32 @@ def test_delete_rejects_path_outside_uploads(tmp_path, monkeypatch) -> None:client = TestClient(app)response = client.delete("/api/uploads", params={"path": str(outside.resolve())})assert response.status_code == 400+++def test_upload_append_to_existing_batch(tmp_path, monkeypatch) -> None:+ uploads = tmp_path / "uploads"+ uploads.mkdir()+ monkeypatch.setattr("app.api.routes.UPLOAD_DIR", uploads)++ client = TestClient(app)+ first = client.post(+ "/api/uploads",+ files=[("files", ("a.png", b"\x89PNG\r\n\x1a\n", "image/png"))],+ )+ assert first.status_code == 200+ first_body = first.json()+ assert first_body["count"] == 1+ batch_dir = str(Path(first_body["files"][0]).parent)++ second = client.post(+ "/api/uploads",+ params={"append_to": batch_dir},+ files=[("files", ("b.png", b"\x89PNG\r\n\x1a\n", "image/png"))],+ )+ assert second.status_code == 200+ second_body = second.json()+ assert second_body["kind"] == "folder"+ assert second_body["count"] == 1+ assert Path(second_body["files"][0]).parent == Path(batch_dir)+ assert Path(batch_dir).joinpath("a.png").is_file()+ assert Path(batch_dir).joinpath("b.png").is_file()