diff options
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r-- | src/server/ApiManagers/FireflyManager.ts | 86 |
1 files changed, 46 insertions, 40 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts index 700b275d4..f49cf4132 100644 --- a/src/server/ApiManagers/FireflyManager.ts +++ b/src/server/ApiManagers/FireflyManager.ts @@ -35,8 +35,10 @@ export default class FireflyManager extends ApiManager { ['Authorization', `Bearer ${data.access_token}`], ], body: JSON.stringify({ - prompt: prompt, - size: { width: width, height: height }, + prompt, + // detailLevel: 'preview', + // modelVersion: 'image3_fast', + size: { width, height }, structure: !structureUrl ? undefined : { @@ -51,16 +53,18 @@ export default class FireflyManager extends ApiManager { imageReference : !styleUrl ? undefined : { - source: { uploadId: styleUrl }, + source: { url: styleUrl }, } } }), }) - .then(response2 => response2.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image))) - .catch(error => { - console.error('Error:', error); - return ''; - }) + .then(response2 => response2.json().then(json => + { + if (json.outputs?.length) + return JSON.stringify((json.outputs?.[0] as { image: { url: string } } ?? {})?.image); + throw new Error(JSON.stringify(json)); + }) + ) ) ); @@ -127,13 +131,7 @@ export default class FireflyManager extends ApiManager { ], body: body, }) - .then(response2 => - response2.json().then(json => { - const seed = json.outputs?.[0]?.seed; - const url = json.outputs?.[0]?.image?.url; - return { seed, url }; - }) - ) + .then(response2 => response2.json().then(json => ({ seed: json.outputs?.[0]?.seed, url: json.outputs?.[0]?.image?.url }))) .catch(error => { console.error('Error:', error); return undefined; @@ -290,33 +288,42 @@ export default class FireflyManager extends ApiManager { register({ method: Method.POST, subscription: '/queryFireflyImageFromStructure', - secureHandler: async ({ req, res }) => { - (req.body.styleRef ? this.uploadImageToDropbox(req.body.styleRef, req.user as DashUserModel) : Promise.resolve(undefined)) - .then(styleUrl => { - if (styleUrl instanceof Error) { - _invalid(res, styleUrl.message); - throw new Error('Error uploading images to dropbox'); - } - return this.uploadImageToDropbox(req.body.structure, req.user as DashUserModel).then(structureUrl => { - if (structureUrl instanceof Error) { - _invalid(res, structureUrl.message); + secureHandler: ({ req, res }) => + new Promise<void>(resolver => { + (req.body.styleUrl ? this.uploadImageToDropbox(req.body.styleUrl, req.user as DashUserModel) : Promise.resolve(undefined)) + .then(styleUrl => { + if (styleUrl instanceof Error) { + _invalid(res, styleUrl.message); throw new Error('Error uploading images to dropbox'); } - return { styleUrl, structureUrl }; - }); - }) - .then(uploads => - this.generateImageFromStructure(req.body.prompt, req.body.width, req.body.height, uploads.structureUrl, req.body.strength, req.body.presets, req.body.styleUrl).then(fire => { - DashUploadUtils.UploadImage(JSON.parse(fire ?? '').url).then(info => { - if (info instanceof Error) _invalid(res, info.message); - else _success(res, info); - }); + this.uploadImageToDropbox(req.body.structure, req.user as DashUserModel) + .then(structureUrl => { + if (structureUrl instanceof Error) { + _invalid(res, structureUrl.message); + throw new Error('Error uploading images to dropbox'); + } + return { styleUrl, structureUrl }; + }) + .then(uploads => + this.generateImageFromStructure(req.body.prompt, req.body.width, req.body.height, uploads.structureUrl, req.body.strength, req.body.presets, uploads.styleUrl) + .then(fire => + DashUploadUtils.UploadImage(JSON.parse(fire ?? '').url).then(info => { + if (info instanceof Error) _invalid(res, info.message); + else _success(res, info); + resolver(); + }) + ) + .catch(e => { + _invalid(res, e.message); + resolver(); + }) + ); }) - ) - .catch(() => { - /* do nothing */ - }); - }, + .catch(() => { + /* do nothing */ + resolver(); + }); + }), }); register({ method: Method.POST, @@ -333,7 +340,6 @@ export default class FireflyManager extends ApiManager { register({ method: Method.POST, subscription: '/queryFireflyImageText', - // eslint-disable-next-line @typescript-eslint/no-unused-vars secureHandler: ({ req, res }) => fetch(req.body.file).then(json => json.blob().then(file => |