aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2024-12-16 11:52:42 -0500
committerbobzel <zzzman@gmail.com>2024-12-16 11:52:42 -0500
commitb7105063030ee29ba644d8fbb5878c8ee41fd389 (patch)
tree519c6949b7d3f7d5c018b4075845cfe53cd99bd5 /src/server/ApiManagers/FireflyManager.ts
parent4826ac1b3223376493ea22c0f23095bc0797cd9c (diff)
fixed api for creating an image using another image as a structure reference.
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r--src/server/ApiManagers/FireflyManager.ts132
1 files changed, 50 insertions, 82 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts
index 73cf94206..c84e9e8fa 100644
--- a/src/server/ApiManagers/FireflyManager.ts
+++ b/src/server/ApiManagers/FireflyManager.ts
@@ -20,21 +20,10 @@ export default class FireflyManager extends ApiManager {
return undefined;
});
- askFireflyOld = (prompt: string = 'a realistic illustration of a cat coding', uploadId: string, strength: number) => {
- const fetched = this.getBearerToken().then(response =>
- response?.json().then((data: { access_token: string }) => {
- const body: any = {
- prompt: prompt,
- structure: {
- strength: strength,
- imageReference: {
- source: {
- uploadId: uploadId,
- },
- },
- },
- };
- return fetch('https://firefly-api.adobe.io/v3/images/generate', {
+ generateImageFromStructure = (prompt: string = 'a realistic illustration of a cat coding', structureUrl: string, strength: number) =>
+ this.getBearerToken().then(response =>
+ response?.json().then((data: { access_token: string }) =>
+ fetch('https://firefly-api.adobe.io/v3/images/generate', {
method: 'POST',
headers: [
['Content-Type', 'application/json'],
@@ -42,40 +31,40 @@ export default class FireflyManager extends ApiManager {
['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
['Authorization', `Bearer ${data.access_token}`],
],
- body: JSON.stringify(body),
+ body: JSON.stringify({
+ prompt,
+ structure: !structureUrl
+ ? undefined
+ : {
+ strength,
+ imageReference: {
+ source: { url: structureUrl },
+ },
+ },
+ }),
})
- .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image)))
+ .then(response2 => response2.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image)))
.catch(error => {
console.error('Error:', error);
return '';
+ })
+ )
+ );
+
+ uploadImageToDropbox = (fileUrl: string, dbx = new Dropbox({ accessToken: process.env.DROPBOX_TOKEN })) =>
+ new Promise<string>((res, rej) =>
+ 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));
});
+ }
})
);
- return fetched;
- };
- uploadImageToFirefly = (image: Blob) => {
- const fetched = this.getBearerToken().then(response =>
- response?.json().then((data: { access_token: string }) =>
- fetch('https://firefly-api.adobe.io/v3/uploads', {
- method: 'POST',
- headers: [
- ['Content-Type', image.type],
- ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
- ['Authorization', `Bearer ${data.access_token}`],
- ],
- body: image,
- })
- .then(response => response.json())
- .then(data => data.uploadId) // Extract the uploadId from the response
- .catch(error => {
- console.error('Error:', error);
- return '';
- })
- )
- );
- return fetched;
- };
generateImage = (prompt: string = 'a realistic illustration of a cat coding') => {
const fetched = this.getBearerToken().then(response =>
response?.json().then((data: { access_token: string }) =>
@@ -222,17 +211,16 @@ export default class FireflyManager extends ApiManager {
protected initialize(register: Registration): void {
register({
method: Method.POST,
- subscription: '/oldQueryFireflyImage',
- secureHandler: async ({ req, res }) => {
- const { prompt, imageBlob, strength = 0.5 } = req.body;
- const uploadId = imageBlob ? await this.uploadImageToFirefly(imageBlob) : null;
- this.askFireflyOld(prompt, uploadId, strength).then(fire =>
- DashUploadUtils.UploadImage(JSON.parse(fire ?? '').url).then(info => {
- if (info instanceof Error) _invalid(res, info.message);
- else _success(res, info);
- })
- );
- },
+ subscription: '/queryFireflyImageFromStructure',
+ secureHandler: async ({ req, res }) =>
+ this.uploadImageToDropbox(req.body.structureUrl).then(uploadUrl =>
+ this.generateImageFromStructure(req.body.prompt, uploadUrl, req.body.strength).then(fire =>
+ DashUploadUtils.UploadImage(JSON.parse(fire ?? '').url).then(info => {
+ if (info instanceof Error) _invalid(res, info.message);
+ else _success(res, info);
+ })
+ )
+ ),
});
register({
method: Method.POST,
@@ -263,36 +251,16 @@ export default class FireflyManager extends ApiManager {
method: Method.POST,
subscription: '/expandImage',
secureHandler: ({ req, res }) =>
- new Promise<void>((resolve, reject) => {
- const dbx = new Dropbox({ accessToken: process.env.DROPBOX_TOKEN });
- fs.readFile(path.join(filesDirectory, `${Directory.images}/${path.basename(req.body.file)}`), undefined, (err, contents) => {
- if (err) {
- console.log('Error: ', err);
- reject();
- } else {
- dbx.filesUpload({ path: `/Apps/browndash/${path.basename(req.body.file)}`, contents })
- .then(response => {
- dbx.filesGetTemporaryLink({ path: response.result.path_display ?? '' }).then(link => {
- console.log(link.result);
- this.expandImage(link.result.link, 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);
- resolve();
- });
- });
- });
- })
- .catch(uploadErr => {
- console.log(uploadErr);
- _error(res, 'upload to dropbox failed');
- reject();
- });
- }
- });
- }),
+ this.uploadImageToDropbox(req.body.file).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);
+ });
+ })
+ ),
});
}
}