aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-04-16 19:53:26 -0400
committerbobzel <zzzman@gmail.com>2025-04-16 19:53:26 -0400
commit49c571cb3f8db0732ac9b461890af2c09722c71b (patch)
treea31b354a56a98727ba8355779feb0bdd437db0dc /src/server/ApiManagers/FireflyManager.ts
parent32f6e2f9e96f16c9b1855a570a3fe4d5bf3e7d9c (diff)
fixed authorizing dropbox.
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r--src/server/ApiManagers/FireflyManager.ts164
1 files changed, 75 insertions, 89 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts
index 07428798c..d22142d7d 100644
--- a/src/server/ApiManagers/FireflyManager.ts
+++ b/src/server/ApiManagers/FireflyManager.ts
@@ -70,49 +70,42 @@ 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}} }`;
@@ -289,43 +282,36 @@ 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,
@@ -357,18 +343,18 @@ export default class FireflyManager extends ApiManager {
method: Method.POST,
subscription: '/expandImage',
secureHandler: ({ req, res }) =>
- this.uploadImageToDropbox(req.body.file, req.user as DashUserModel).then(uploadUrl =>
- uploadUrl instanceof Error
- ? _invalid(res, uploadUrl.message)
- : 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);
- });
- })
- ),
+ 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