From 8d9c5666563f5ca8c3844b293651e42d7e41425f Mon Sep 17 00:00:00 2001 From: Stefan Tobler Date: Mon, 19 Feb 2024 14:03:26 -0500 Subject: [PATCH] implementing download for bulk_download events --- invokeai/frontend/web/public/locales/en.json | 2 + .../middleware/listenerMiddleware/index.ts | 4 ++ .../socketio/socketBulkDownloadComplete.ts | 41 +++++++++++++++++++ .../socketio/socketBulkDownloadFailed.ts | 32 +++++++++++++++ .../frontend/web/src/services/events/types.ts | 8 ++-- 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadComplete.ts create mode 100644 invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadFailed.ts diff --git a/invokeai/frontend/web/public/locales/en.json b/invokeai/frontend/web/public/locales/en.json index d4f00a5970..9b3a76f043 100644 --- a/invokeai/frontend/web/public/locales/en.json +++ b/invokeai/frontend/web/public/locales/en.json @@ -426,6 +426,8 @@ "downloadSelection": "Download Selection", "preparingDownload": "Preparing Download", "preparingDownloadFailed": "Problem Preparing Download", + "bulkDownloadStarting": "Beginning Download", + "bulkDownloadFailed": "Problem Preparing Download", "problemDeletingImages": "Problem Deleting Images", "problemDeletingImagesDesc": "One or more images could not be deleted" }, diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/index.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/index.ts index 322c4eb1ec..07d9bb5df5 100644 --- a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/index.ts +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/index.ts @@ -48,6 +48,8 @@ import { addInitialImageSelectedListener } from './listeners/initialImageSelecte import { addModelSelectedListener } from './listeners/modelSelected'; import { addModelsLoadedListener } from './listeners/modelsLoaded'; import { addDynamicPromptsListener } from './listeners/promptChanged'; +import { addBulkDownloadCompleteEventListener } from './listeners/socketio/socketBulkDownloadComplete'; +import { addBulkDownloadFailedEventListener } from './listeners/socketio/socketBulkDownloadFailed'; import { addSocketConnectedEventListener as addSocketConnectedListener } from './listeners/socketio/socketConnected'; import { addSocketDisconnectedEventListener as addSocketDisconnectedListener } from './listeners/socketio/socketDisconnected'; import { addGeneratorProgressEventListener as addGeneratorProgressListener } from './listeners/socketio/socketGeneratorProgress'; @@ -137,6 +139,8 @@ addModelLoadEventListener(); addSessionRetrievalErrorEventListener(); addInvocationRetrievalErrorEventListener(); addSocketQueueItemStatusChangedEventListener(); +addBulkDownloadCompleteEventListener(); +addBulkDownloadFailedEventListener(); // ControlNet addControlNetImageProcessedListener(); diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadComplete.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadComplete.ts new file mode 100644 index 0000000000..acdb61ff25 --- /dev/null +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadComplete.ts @@ -0,0 +1,41 @@ +import { logger } from 'app/logging/logger'; +import { addToast } from 'features/system/store/systemSlice'; +import { t } from 'i18next'; +import { socketBulkDownloadCompleted } from 'services/events/actions'; + +import { startAppListening } from '../..'; + +const log = logger('socketio'); + +export const addBulkDownloadCompleteEventListener = () => { + startAppListening({ + actionCreator: socketBulkDownloadCompleted, + effect: async (action, { dispatch }) => { + log.debug(action.payload, 'Bulk download complete'); + + const bulk_download_item_name = action.payload.data.bulk_download_item_name; + + const url = `/api/v1/images/download/${bulk_download_item_name}`; + const a = document.createElement('a'); + a.style.display = 'none'; + a.href = url; + a.download = bulk_download_item_name; + document.body.appendChild(a); + a.click(); + + dispatch( + addToast({ + title: t('gallery.bulkDownloadStarting'), + status: 'success', + ...(action.payload + ? { + description: bulk_download_item_name, + duration: null, + isClosable: true, + } + : {}), + }) + ); + }, + }); +}; diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadFailed.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadFailed.ts new file mode 100644 index 0000000000..a9a45c42ae --- /dev/null +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketBulkDownloadFailed.ts @@ -0,0 +1,32 @@ +import { logger } from 'app/logging/logger'; +import { addToast } from 'features/system/store/systemSlice'; +import { t } from 'i18next'; +import { socketBulkDownloadFailed } from 'services/events/actions'; + +import { startAppListening } from '../..'; + +const log = logger('socketio'); + +export const addBulkDownloadFailedEventListener = () => { + startAppListening({ + actionCreator: socketBulkDownloadFailed, + effect: async (action, { dispatch }) => { + log.debug(action.payload, 'Bulk download error'); + + + dispatch( + addToast({ + title: t('gallery.bulkDownloadFailed'), + status: 'error', + ...(action.payload + ? { + description: action.payload.data.error, + duration: null, + isClosable: true, + } + : {}), + }) + ); + }, + }); +}; diff --git a/invokeai/frontend/web/src/services/events/types.ts b/invokeai/frontend/web/src/services/events/types.ts index 092132fea2..4e68131ee9 100644 --- a/invokeai/frontend/web/src/services/events/types.ts +++ b/invokeai/frontend/web/src/services/events/types.ts @@ -156,7 +156,7 @@ export type InvocationRetrievalErrorEvent = { * * @example socket.on('queue_item_status_changed', (data: QueueItemStatusChangedEvent) => { ... } */ -export type QueueItemStatusChangedEvent = { +export type QueueItemStatusChangedEvent = { queue_id: string; queue_item: { queue_id: string; @@ -191,7 +191,7 @@ export type QueueItemStatusChangedEvent = { failed: number; canceled: number; total: number; - }; + }; }; export type ClientEmitSubscribeQueue = { @@ -205,6 +205,7 @@ export type ClientEmitUnsubscribeQueue = { export type BulkDownloadStartedEvent = { bulk_download_id: string; bulk_download_item_id: string; + bulk_download_item_name: string; }; export type BulkDownloadCompletedEvent = { @@ -216,8 +217,9 @@ export type BulkDownloadCompletedEvent = { export type BulkDownloadFailedEvent = { bulk_download_id: string; bulk_download_item_id: string; + bulk_download_item_name: string; error: string; -} +}; export type ClientEmitSubscribeBulkDownload = { bulk_download_id: string;