From 5b0b7241e68febc2d0556b6c2e8349411b5c12a0 Mon Sep 17 00:00:00 2001 From: bobzel Date: Mon, 11 Aug 2025 13:04:26 -0400 Subject: fixed image fill api to handler errors better. --- src/client/Network.ts | 6 +++++- src/client/views/nodes/imageEditor/ImageEditor.tsx | 13 +++++++++---- src/server/ApiManagers/FireflyManager.ts | 11 ++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/client/Network.ts b/src/client/Network.ts index 850ab4f91..ff3f3d1af 100644 --- a/src/client/Network.ts +++ b/src/client/Network.ts @@ -38,7 +38,11 @@ export namespace Networking { method: 'POST', body: formData, }; - return fetch('/queryFireflyImageFillWithMask', parameters); + return fetch('/queryFireflyImageFillWithMask', parameters).then(response => { + if (response.ok) return response.json() as object; + + return response.text().then(text => ({ error: '' + response.status + ':' + response.statusText + '-' + text })); + }); } /** diff --git a/src/client/views/nodes/imageEditor/ImageEditor.tsx b/src/client/views/nodes/imageEditor/ImageEditor.tsx index b56490bc3..1f38ff81a 100644 --- a/src/client/views/nodes/imageEditor/ImageEditor.tsx +++ b/src/client/views/nodes/imageEditor/ImageEditor.tsx @@ -26,6 +26,7 @@ import { CutMode, CursorData, ImageDimensions, ImageEditTool, ImageToolType, Poi import { DocumentView } from '../DocumentView'; import { SettingsManager } from '../../../util/SettingsManager'; import { Upload } from '../../../../server/SharedMediaTypes'; +import { DrawingFillHandler } from '../../smartdraw/DrawingFillHandler'; interface GenerativeFillProps { imageEditorOpen: boolean; @@ -301,12 +302,16 @@ const ImageEditor = ({ imageEditorOpen, imageEditorSource, imageRootDoc, addDoc }; if (useFirefly) { const res = await Networking.PostFormToServer(input || 'Fill in the image in the same style', imgBlob, maskBlob, img.width, img.height); - if (!res.ok) throw new Error(await res.text()); - const json = (await res.json()) as APISuccess; - imgUrls = json.urls ?? []; + + const error = ('error' in res && (res.error as string)) || ''; + if (error.includes('Dropbox') && confirm('Create image failed. Try authorizing DropBox?\r\n' + error.replace(/^[^"]*/, ''))) { + DrawingFillHandler.authorizeDropbox(); + } else { + imgUrls = (res as { urls: string[] }).urls ?? []; + } } else { const res = await ImageUtility.getEdit(imgBlob, maskBlob, input || 'Fill in the image in the same style', 2); - if (res.status == 'error') throw new Error(res.message); + if (res.status === 'error') throw new Error(res.message); const json = res as APISuccess; imgUrls = await Promise.all((json.urls ?? []).map(url => ImageUtility.convertImgToCanvasUrl(url, canvasDims.width, canvasDims.height))); } diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts index fbf7d7202..11893aa0f 100644 --- a/src/server/ApiManagers/FireflyManager.ts +++ b/src/server/ApiManagers/FireflyManager.ts @@ -360,7 +360,7 @@ export default class FireflyManager extends ApiManager { method: Method.POST, subscription: '/queryFireflyImageFillWithMask', secureHandler: ({ req, res }) => - new Promise(resolve => { + new Promise(resolve => { const user = req.user as DashUserModel; const accessToken = user?.dropboxToken || ''; const dbx = new Dropbox({ accessToken }); @@ -369,12 +369,13 @@ export default class FireflyManager extends ApiManager { if (files.source && files.mask) { Promise.all([this.uploadToDropbox(dbx, user, 'source.png', fs.readFileSync(files.source[0].filepath)), this.uploadToDropbox(dbx, user, 'mask.png', fs.readFileSync(files.mask[0].filepath))]) - .then(stuff => - stuff.some(s => s instanceof Error) ? resolve("") : this.generateImageFillWithMask(fields["prompt"]?.[0], stuff[0] as string, stuff[1] as string, 2048, 2048, 1).then(url => resolve(url![0].url)) - ).catch(() => resolve("") ); // prettier-ignore + .then(async stuff => + resolve(stuff?.find(s => s instanceof Error) ?? + (await this.generateImageFillWithMask(fields["prompt"]?.[0], stuff[0] as string, stuff[1] as string, 2048, 2048, 1).then(url => url![0].url)) + )).catch(e => resolve(e.message) ); // prettier-ignore } }); - }).then(url => (url ? _success(res, { urls: [url] }) : _invalid(res, 'Failed to fill image'))), + }).then(url => (typeof url === 'string' ? _success(res, { urls: [url] }) : _invalid(res, url?.message))), }); register({ -- cgit v1.2.3-70-g09d2