aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-04-21 13:48:58 -0400
committerbobzel <zzzman@gmail.com>2025-04-21 13:48:58 -0400
commit17e24e780b54f2f7015c0ca955c3aa5091bba19c (patch)
treeb13002c92d58cb52a02b46e4e1d578f1d57125f2 /src/server/ApiManagers/FireflyManager.ts
parent22a40443193320487c27ce02bd3f134d13cb7d65 (diff)
parent1f294ef4a171eec72a069a9503629eaf7975d983 (diff)
merged with master and cleaned up outpainting a bit.
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r--src/server/ApiManagers/FireflyManager.ts264
1 files changed, 127 insertions, 137 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts
index 887c4aa72..fd61f6c9e 100644
--- a/src/server/ApiManagers/FireflyManager.ts
+++ b/src/server/ApiManagers/FireflyManager.ts
@@ -71,54 +71,46 @@ export default class FireflyManager extends ApiManager {
);
uploadImageToDropbox = (fileUrl: string, user: DashUserModel | undefined, dbx = new Dropbox({ accessToken: user?.dropboxToken || '' })) =>
- new Promise<string | Error>((res, rej) =>
+ new Promise<string>((resolve, reject) => {
fs.readFile(path.join(filesDirectory, `${Directory.images}/${path.basename(fileUrl)}`), undefined, (err, contents) => {
if (err) {
- console.log('Error: ', err);
- rej();
- } else {
- dbx.filesUpload({ path: `/Apps/browndash/${path.basename(fileUrl)}`, contents })
- .then(response => {
- dbx.filesGetTemporaryLink({ path: response.result.path_display ?? '' })
- .then(link => res(link.result.link))
- .catch(e => res(new Error(e.toString())));
- })
- .catch(e => {
- if (user?.dropboxRefresh) {
- console.log('*********** try refresh dropbox for: ' + user.email + ' ***********');
- this.refreshDropboxToken(user).then(token => {
- if (!token) {
- console.log('Dropbox error: cannot refresh token');
- res(new Error(e.toString()));
- } else {
- const dbxNew = new Dropbox({ accessToken: user.dropboxToken || '' });
- dbxNew
- .filesUpload({ path: `/Apps/browndash/${path.basename(fileUrl)}`, contents })
- .then(response => {
- dbxNew
- .filesGetTemporaryLink({ path: response.result.path_display ?? '' })
- .then(link => res(link.result.link))
- .catch(linkErr => res(new Error(linkErr.toString())));
- })
- .catch(uploadErr => {
- console.log('Dropbox error:', uploadErr);
- res(new Error(uploadErr.toString()));
- });
- }
- });
- } else {
- console.log('Dropbox error:', e);
- res(new Error(e.toString()));
- }
- });
+ return reject(new Error('Error reading file:' + err.message));
}
- })
- );
+
+ const uploadToDropbox = (dropboxClient: Dropbox) =>
+ dropboxClient
+ .filesUpload({ path: `/Apps/browndash/${path.basename(fileUrl)}`, contents })
+ .then(response =>
+ dropboxClient
+ .filesGetTemporaryLink({ path: response.result.path_display ?? '' })
+ .then(link => resolve(link.result.link))
+ .catch(linkErr => reject(new Error('Failed to get temporary link: ' + linkErr.message)))
+ )
+ .catch(uploadErr => reject(new Error('Failed to upload file to Dropbox: ' + uploadErr.message)));
+
+ uploadToDropbox(dbx).catch(e => {
+ if (user?.dropboxRefresh) {
+ console.log('Attempting to refresh Dropbox token for user:', user.email);
+ this.refreshDropboxToken(user)
+ .then(token => {
+ if (!token) {
+ return reject(new Error('Failed to refresh Dropbox token.' + user.email));
+ }
+
+ const dbxNew = new Dropbox({ accessToken: token });
+ uploadToDropbox(dbxNew).catch(finalErr => reject(new Error('Failed to refresh Dropbox token:' + finalErr.message)));
+ })
+ .catch(refreshErr => reject(new Error('Failed to refresh Dropbox token: ' + refreshErr.message)));
+ } else {
+ reject(new Error('Dropbox error: ' + e.message));
+ }
+ });
+ });
+ });
generateImage = (prompt: string = 'a realistic illustration of a cat coding', width: number = 2048, height: number = 2048, seed?: number) => {
let body = `{ "prompt": "${prompt}", "size": { "width": ${width}, "height": ${height}} }`;
if (seed) {
- console.log('RECEIVED SEED', seed);
body = `{ "prompt": "${prompt}", "size": { "width": ${width}, "height": ${height}}, "seeds": [${seed}]}`;
}
const fetched = this.getBearerToken().then(response =>
@@ -291,108 +283,106 @@ export default class FireflyManager extends ApiManager {
register({
method: Method.POST,
subscription: '/queryFireflyImageFromStructure',
- 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');
- }
- this.uploadImageToDropbox(req.body.structureUrl, req.user as DashUserModel)
- .then(dropboxStructureUrl => {
- if (dropboxStructureUrl instanceof Error) {
- _invalid(res, dropboxStructureUrl.message);
- throw new Error('Error uploading images to dropbox');
- }
- return { styleUrl, structureUrl: dropboxStructureUrl };
- })
- .then(uploads =>
- this.generateImageFromStructure(req.body.prompt, req.body.width, req.body.height, uploads.structureUrl, req.body.strength, req.body.presets, uploads.styleUrl)
- .then(images => {
- Promise.all((images ?? [new Error('no images were generated')]).map(fire => (fire instanceof Error ? fire : DashUploadUtils.UploadImage(fire.url))))
- .then(dashImages => {
- if (dashImages.every(img => img instanceof Error)) _invalid(res, dashImages[0]!.message);
- else _success(res, JSON.stringify(dashImages.filter(img => !(img instanceof Error))));
- })
- .then(resolver);
- })
- .catch(e => {
- _invalid(res, e.message);
- resolver();
- })
- );
- })
- .catch(() => {
- /* do nothing */
- resolver();
- });
- }),
+ secureHandler: ({ req, res }) =>
+ new Promise<void>(resolver =>
+ (req.body.styleUrl
+ ? this.uploadImageToDropbox(req.body.styleUrl, req.user as DashUserModel)
+ : Promise.resolve(undefined)
+ )
+ .then(styleUrl =>
+ this.uploadImageToDropbox(req.body.structureUrl, req.user as DashUserModel)
+ .then(dropboxStructureUrl =>
+ ({ styleUrl, structureUrl: dropboxStructureUrl })
+ )
+
+ )
+ .then(uploads =>
+ this.generateImageFromStructure(
+ req.body.prompt, req.body.width, req.body.height, uploads.structureUrl, req.body.strength, req.body.presets, uploads.styleUrl
+ ).then(images =>
+ Promise.all((images ?? [new Error('no images were generated')]).map(fire => (fire instanceof Error ? fire : DashUploadUtils.UploadImage(fire.url))))
+ .then(dashImages =>
+ (dashImages.every(img => img instanceof Error))
+ ? _invalid(res, dashImages[0]!.message)
+ : _success(res, JSON.stringify(dashImages.filter(img => !(img instanceof Error))))
+ )
+ )
+ )
+ .catch(e => {
+ _invalid(res, e.message);
+ resolver();
+ })
+ ), // prettier-ignore
});
register({
method: Method.POST,
subscription: '/outpaintImage',
secureHandler: ({ req, res }) =>
- this.uploadImageToDropbox(req.body.imageUrl, req.user as DashUserModel).then(uploadUrl =>
- uploadUrl instanceof Error
- ? _invalid(res, uploadUrl.message)
- : this.getBearerToken()
- .then(tokenResponse => tokenResponse?.json())
- .then((tokenData: { access_token: string }) =>
- fetch('https://firefly-api.adobe.io/v3/images/expand', {
- method: 'POST',
- headers: [
- ['Content-Type', 'application/json'],
- ['Accept', 'application/json'],
- ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
- ['Authorization', `Bearer ${tokenData.access_token}`],
- ],
- body: JSON.stringify({
- image: {
- source: { url: uploadUrl },
- },
- size: {
- width: Math.round(req.body.newDimensions.width),
- height: Math.round(req.body.newDimensions.height),
- },
- prompt: req.body.prompt ?? '',
- numVariations: 1,
- placement: {
- inset: {
- left: 0,
- top: 0,
- right: Math.round(req.body.newDimensions.width - req.body.originalDimensions.width),
- bottom: Math.round(req.body.newDimensions.height - req.body.originalDimensions.height),
- },
- alignment: {
- horizontal: 'center',
- vertical: 'center',
- },
- },
- }),
- })
- .then(expandResp => expandResp?.json())
- .then(expandData => {
- if (expandData.error_code || !expandData.outputs?.[0]?.image?.url) {
- console.error('Firefly validation error:', expandData);
- _error(res, expandData.message ?? 'Failed to generate image');
- } else {
- return DashUploadUtils.UploadImage(expandData.outputs[0].image.url)
- .then((info: Upload.ImageInformation | Error) => {
- if (info instanceof Error) {
- _invalid(res, info.message);
- } else {
- _success(res, { url: info.accessPaths.agnostic.client });
- }
- })
- .catch(uploadErr => {
- console.error('DashUpload Error:', uploadErr);
- _error(res, 'Failed to upload generated image.');
- });
- }
- })
- )
+ new Promise<void>(resolver =>
+ this.uploadImageToDropbox(req.body.imageUrl, req.user as DashUserModel)
+ .then(uploadUrl =>
+ this.getBearerToken()
+ .then(tokenResponse => tokenResponse?.json())
+ .then((tokenData: { access_token: string }) =>
+ fetch('https://firefly-api.adobe.io/v3/images/expand', {
+ method: 'POST',
+ headers: [
+ ['Content-Type', 'application/json'],
+ ['Accept', 'application/json'],
+ ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
+ ['Authorization', `Bearer ${tokenData.access_token}`],
+ ],
+ body: JSON.stringify({
+ image: {
+ source: { url: uploadUrl },
+ },
+ size: {
+ width: Math.round(req.body.newDimensions.width),
+ height: Math.round(req.body.newDimensions.height),
+ },
+ prompt: req.body.prompt ?? '',
+ numVariations: 1,
+ placement: {
+ inset: {
+ left: 0,
+ top: 0,
+ right: Math.round(req.body.newDimensions.width - req.body.originalDimensions.width),
+ bottom: Math.round(req.body.newDimensions.height - req.body.originalDimensions.height),
+ },
+ alignment: {
+ horizontal: 'center',
+ vertical: 'center',
+ },
+ },
+ }),
+ })
+ .then(expandResp => expandResp?.json())
+ .then(expandData => {
+ if (expandData.error_code || !expandData.outputs?.[0]?.image?.url) {
+ console.error('Firefly validation error:', expandData);
+ _error(res, expandData.message ?? 'Failed to generate image');
+ } else {
+ return DashUploadUtils.UploadImage(expandData.outputs[0].image.url)
+ .then((info: Upload.ImageInformation | Error) => {
+ if (info instanceof Error) {
+ _invalid(res, info.message);
+ } else {
+ _success(res, { url: info.accessPaths.agnostic.client });
+ }
+ })
+ .catch(uploadErr => {
+ console.error('DashUpload Error:', uploadErr);
+ _error(res, 'Failed to upload generated image.');
+ });
+ }
+ })
+ )
+ )
+ .catch(e => {
+ _invalid(res, e.message);
+ resolver();
+ })
),
});