aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-04-30 14:43:29 -0400
committerbobzel <zzzman@gmail.com>2025-04-30 14:43:29 -0400
commit0317a667a5d4a0f298522eeddb8afa96e7b7ecfc (patch)
tree2d66c3caa5c102a3ca78837b87e61c16fd07fc01 /src/server/ApiManagers/FireflyManager.ts
parent9ec7df6af508eccdfda3a195a69c8bb1f86c41ea (diff)
parent2296c314be710f983d595de37c9d8039d73568a6 (diff)
Merge branch 'master' into task_nodes_aarav
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r--src/server/ApiManagers/FireflyManager.ts129
1 files changed, 95 insertions, 34 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts
index d22142d7d..1b8a85a5c 100644
--- a/src/server/ApiManagers/FireflyManager.ts
+++ b/src/server/ApiManagers/FireflyManager.ts
@@ -6,6 +6,7 @@ import * as path from 'path';
import { DashUserModel } from '../authentication/DashUserModel';
import { DashUploadUtils } from '../DashUploadUtils';
import { _error, _invalid, _success, Method } from '../RouteManager';
+import { Upload } from '../SharedMediaTypes';
import { Directory, filesDirectory } from '../SocketData';
import ApiManager, { Registration } from './ApiManager';
@@ -85,25 +86,23 @@ export default class FireflyManager extends ApiManager {
.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)));
+ .catch(uploadErr => {
+ 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));
- 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: ' + uploadErr.message));
+ }
+ });
- 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));
- }
- });
+ uploadToDropbox(dbx);
});
});
@@ -313,6 +312,85 @@ export default class FireflyManager extends ApiManager {
})
), // prettier-ignore
});
+
+ register({
+ method: Method.POST,
+ subscription: '/outpaintImage',
+ secureHandler: ({ req, res }) =>
+ 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: Math.round((req.body.newDimensions.width - req.body.originalDimensions.width) / 2),
+ top: Math.round((req.body.newDimensions.height - req.body.originalDimensions.height) / 2),
+ right: Math.round((req.body.newDimensions.width - req.body.originalDimensions.width) / 2),
+ bottom: Math.round((req.body.newDimensions.height - req.body.originalDimensions.height) / 2),
+ },
+ 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();
+ })
+ ),
+ });
+
+ /* register({
+ method: Method.POST
+ subscription: '/queryFireflyOutpaint',
+ secureHandler: ({req, res}) =>
+ this.outpaintImage()
+ })*/
+
register({
method: Method.POST,
subscription: '/queryFireflyImage',
@@ -339,23 +417,6 @@ export default class FireflyManager extends ApiManager {
)
),
});
- register({
- method: Method.POST,
- subscription: '/expandImage',
- secureHandler: ({ req, res }) =>
- this.uploadImageToDropbox(req.body.file, req.user as DashUserModel)
- .then(uploadUrl =>
- this.expandImage(uploadUrl, req.body.prompt).then(text => {
- if (text.error_code) _error(res, text.message);
- else
- DashUploadUtils.UploadImage(text.outputs[0].image.url).then(info => {
- if (info instanceof Error) _invalid(res, info.message);
- else _success(res, info);
- });
- })
- )
- .catch(e => _invalid(res, e.message)),
- });
// construct this url and send user to it. It will allow them to authorize their dropbox account and will send the resulting token to our endpoint /refreshDropbox
// https://www.dropbox.com/oauth2/authorize?client_id=DROPBOX_CLIENT_ID&response_type=code&token_access_type=offline&redirect_uri=http://localhost:1050/refreshDropbox