Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(api): allow downloading the Uncategorized board (#10148)
Vladimir Babinbf36af06a4c8
diff --git a/invokeai/app/api/routers/images.py b/invokeai/app/api/routers/images.pyindex 88488bf8a6..622a364ed1 100644--- a/invokeai/app/api/routers/images.py+++ b/invokeai/app/api/routers/images.py@@ -905,7 +905,7 @@ def download_images_from_list(# Validate that the caller can read every image they are requesting.# For a board_id request, check board visibility; for explicit image names,# check each image individually.- if board_id:+ if board_id is not None and board_id != "none":_assert_board_read_access(board_id, current_user)if image_names:for name in image_names:@@ -921,6 +921,7 @@ def download_images_from_list(board_id,bulk_download_item_id,current_user.user_id,+ uncategorized_owner_id=None if current_user.is_admin else current_user.user_id,)return ImagesDownloaded(bulk_download_item_name=bulk_download_item_id + ".zip")diff --git a/invokeai/app/services/bulk_download/bulk_download_base.py b/invokeai/app/services/bulk_download/bulk_download_base.pyindex 6cd4ed0cba..3756b8aabd 100644--- a/invokeai/app/services/bulk_download/bulk_download_base.py+++ b/invokeai/app/services/bulk_download/bulk_download_base.py@@ -12,6 +12,7 @@ class BulkDownloadBase(ABC):board_id: Optional[str],bulk_download_item_id: Optional[str],user_id: str = "system",+ uncategorized_owner_id: Optional[str] = None,) -> None:"""Create a zip file containing the images specified by the given image names or board id.@@ -20,6 +21,7 @@ class BulkDownloadBase(ABC)::param board_id: The ID of the board. If provided, all images associated with the board will be included in the zip file.:param bulk_download_item_id: The bulk_download_item_id that will be used to retrieve the bulk download item when it is prepared, if none is provided a uuid will be generated.:param user_id: The ID of the user who initiated the download.+ :param uncategorized_owner_id: For board_id "none", include only this user's uncategorized images. None includes every account's (admins)."""@abstractmethoddiff --git a/invokeai/app/services/bulk_download/bulk_download_default.py b/invokeai/app/services/bulk_download/bulk_download_default.pyindex c037e9c5c1..6a6129a349 100644--- a/invokeai/app/services/bulk_download/bulk_download_default.py+++ b/invokeai/app/services/bulk_download/bulk_download_default.py@@ -34,6 +34,7 @@ class BulkDownloadService(BulkDownloadBase):board_id: Optional[str],bulk_download_item_id: Optional[str],user_id: str = "system",+ uncategorized_owner_id: Optional[str] = None,) -> None:bulk_download_id: str = DEFAULT_BULK_DOWNLOAD_IDbulk_download_item_id = bulk_download_item_id or uuid_string()@@ -48,7 +49,7 @@ class BulkDownloadService(BulkDownloadBase):image_dtos: list[ImageDTO] = []if board_id:- image_dtos = self._board_handler(board_id)+ image_dtos = self._board_handler(board_id, uncategorized_owner_id)elif image_names:image_dtos = self._image_handler(image_names)else:@@ -71,11 +72,14 @@ class BulkDownloadService(BulkDownloadBase):def _image_handler(self, image_names: list[str]) -> list[ImageDTO]:return [self._invoker.services.images.get_dto(image_name) for image_name in image_names]- def _board_handler(self, board_id: str) -> list[ImageDTO]:+ def _board_handler(self, board_id: str, uncategorized_owner_id: Optional[str]) -> list[ImageDTO]:+ # "none" spans every account's uncategorized images, so it is scoped to the owner. A real board was+ # authorized as a whole, so a shared board keeps every contributor's images.image_names = self._invoker.services.board_image_records.get_all_board_image_names_for_board(board_id,categories=None,is_intermediate=None,+ user_id=uncategorized_owner_id if board_id == "none" else None,)return self._image_handler(image_names)diff --git a/tests/app/routers/test_images.py b/tests/app/routers/test_images.pyindex fe6e72ddce..3c4548ad27 100644--- a/tests/app/routers/test_images.py+++ b/tests/app/routers/test_images.py@@ -57,6 +57,15 @@ def test_download_images_from_board_id_empty_image_name_list(assert json_response["bulk_download_item_name"] == "test.zip"+def test_download_uncategorized_board(monkeypatch: Any, mock_invoker: Invoker, client: TestClient) -> None:+ prepare_download_images_test(monkeypatch, mock_invoker)++ response = client.post("/api/v1/images/download", json={"board_id": "none"})+ json_response = response.json()+ assert response.status_code == 202+ assert json_response["bulk_download_item_name"] == "test.zip"++def prepare_download_images_test(monkeypatch: Any, mock_invoker: Invoker) -> None:mock_deps = MockApiDependencies(mock_invoker)monkeypatch.setattr("invokeai.app.api.routers.images.ApiDependencies", mock_deps)diff --git a/tests/app/routers/test_multiuser_authorization.py b/tests/app/routers/test_multiuser_authorization.pyindex ca5f847a8a..caf34009d6 100644--- a/tests/app/routers/test_multiuser_authorization.py+++ b/tests/app/routers/test_multiuser_authorization.py@@ -2643,6 +2643,99 @@ class TestBulkDownloadAccessControl:)assert r.status_code == status.HTTP_202_ACCEPTED+ @staticmethod+ def _download_zip_names(+ client: TestClient, mock_invoker: Invoker, monkeypatch: Any, tmp_path: Path, token: str, board_id: str+ ) -> set[str]:+ """Request a board download, run the queued job, fetch the ZIP as the caller and list its images."""+ import io+ import zipfile++ from fastapi import BackgroundTasks++ queued: list[tuple[Any, tuple[Any, ...], dict[str, Any]]] = []+ monkeypatch.setattr(+ BackgroundTasks, "add_task", lambda _self, fn, *args, **kwargs: queued.append((fn, args, kwargs))+ )++ def image_path(image_name: str, thumbnail: bool = False) -> str:+ path = tmp_path / image_name+ path.write_bytes(image_name.encode())+ return str(path)++ monkeypatch.setattr(mock_invoker.services.images, "get_path", image_path)++ r = client.post("/api/v1/images/download", json={"board_id": board_id}, headers=_auth(token))+ assert r.status_code == status.HTTP_202_ACCEPTED+ fn, args, kwargs = queued.pop()+ fn(*args, **kwargs)++ r = client.get(f"/api/v1/images/download/{r.json()['bulk_download_item_name']}", headers=_auth(token))+ assert r.status_code == status.HTTP_200_OK+ with zipfile.ZipFile(io.BytesIO(r.content)) as archive:+ return {Path(name).name for name in archive.namelist()}++ def test_uncategorized_download_contains_only_callers_images(+ self,+ client: TestClient,+ mock_invoker: Invoker,+ monkeypatch: Any,+ tmp_path: Path,+ user1_token: str,+ user2_token: str,+ ):+ """Downloading "none" must not bundle another account's uncategorized images."""+ user1 = mock_invoker.services.users.get_by_email("[email protected]")+ user2 = mock_invoker.services.users.get_by_email("[email protected]")+ assert user1 is not None and user2 is not None+ _save_image(mock_invoker, "user1-uncategorized-dl", user1.user_id)+ _save_image(mock_invoker, "user2-uncategorized-dl", user2.user_id)++ names = self._download_zip_names(client, mock_invoker, monkeypatch, tmp_path, user2_token, "none")++ assert names == {"user2-uncategorized-dl"}++ def test_admin_uncategorized_download_contains_every_users_images(+ self,+ client: TestClient,+ mock_invoker: Invoker,+ monkeypatch: Any,+ tmp_path: Path,+ admin_token: str,+ user1_token: str,+ user2_token: str,+ ):+ user1 = mock_invoker.services.users.get_by_email("[email protected]")+ user2 = mock_invoker.services.users.get_by_email("[email protected]")+ assert user1 is not None and user2 is not None+ _save_image(mock_invoker, "user1-uncategorized-admin-dl", user1.user_id)+ _save_image(mock_invoker, "user2-uncategorized-admin-dl", user2.user_id)++ names = self._download_zip_names(client, mock_invoker, monkeypatch, tmp_path, admin_token, "none")++ assert names == {"user1-uncategorized-admin-dl", "user2-uncategorized-admin-dl"}++ def test_shared_board_download_contains_owners_images(+ self,+ client: TestClient,+ mock_invoker: Invoker,+ monkeypatch: Any,+ tmp_path: Path,+ user1_token: str,+ user2_token: str,+ ):+ """Scoping uncategorized downloads must not filter a shared board down to the caller's own images."""+ user1 = mock_invoker.services.users.get_by_email("[email protected]")+ assert user1 is not None+ board_id = _create_board(client, user1_token, "Shared Contents DL Board")+ _share_board(client, user1_token, board_id)+ _save_image(mock_invoker, "user1-shared-board-dl", user1.user_id)+ mock_invoker.services.board_image_records.add_image_to_board(board_id, "user1-shared-board-dl")++ names = self._download_zip_names(client, mock_invoker, monkeypatch, tmp_path, user2_token, board_id)++ assert names == {"user1-shared-board-dl"}+def test_bulk_download_events_carry_user_id(self):"""BulkDownloadEventBase must carry user_id so events can be routed privately."""from invokeai.app.services.events.events_common import (