diff options
author | bobzel <zzzman@gmail.com> | 2025-08-11 13:04:26 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-08-11 13:04:26 -0400 |
commit | 5b0b7241e68febc2d0556b6c2e8349411b5c12a0 (patch) | |
tree | e4ee96e10ad16cff290f86108d160ec93575fc03 | |
parent | b3aa4a5b0bbbb87d041b9515bddedbcbf8ae9fc5 (diff) |
fixed image fill api to handler errors better.
-rw-r--r-- | src/client/Network.ts | 6 | ||||
-rw-r--r-- | src/client/views/nodes/imageEditor/ImageEditor.tsx | 13 | ||||
-rw-r--r-- | 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<string>(resolve => { + new Promise<string | Error>(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({ |