aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2024-11-22 10:27:33 -0500
committerbobzel <zzzman@gmail.com>2024-11-22 10:27:33 -0500
commit89424e0a8efc6cf3364a2fd1ffc85c9d0d837453 (patch)
tree9f0bb770707b2e4239c0618d7435976bcc1c0f16 /src/server/ApiManagers/FireflyManager.ts
parent7b38bbc4d845fa524e8310a0ec05b0e776b47c82 (diff)
added initial Firefly endpoint and hanged smartDrawHandler to generate an image and an svg.
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r--src/server/ApiManagers/FireflyManager.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts
new file mode 100644
index 000000000..04fa8f065
--- /dev/null
+++ b/src/server/ApiManagers/FireflyManager.ts
@@ -0,0 +1,51 @@
+import { DashUploadUtils } from '../DashUploadUtils';
+import { _invalid, _success, Method } from '../RouteManager';
+import ApiManager, { Registration } from './ApiManager';
+
+export default class FireflyManager extends ApiManager {
+ askFirefly = (prompt: string = 'a realistic illustration of a cat coding') => {
+ const fetched = fetch('https://ims-na1.adobelogin.com/ims/token/v3', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ },
+ body: `grant_type=client_credentials&client_id=${process.env._CLIENT_FIREFLY_CLIENT_ID}&client_secret=${process.env._CLIENT_FIREFLY_SECRET}&scope=openid,AdobeID,session,additional_info,read_organizations,firefly_api,ff_apis`,
+ })
+ .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'],
+ ['Accept', 'application/json'],
+ ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
+ ['Authorization', `Bearer ${data.access_token}`],
+ ],
+ body: `{ "prompt": "${prompt}" }`,
+ })
+ .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image)))
+ .catch(error => {
+ console.error('Error:', error);
+ return '';
+ })
+ )
+ .catch(error => {
+ console.error('Error:', error);
+ return '';
+ });
+ return fetched;
+ };
+ protected initialize(register: Registration): void {
+ register({
+ method: Method.POST,
+ subscription: '/queryFireflyImage',
+ secureHandler: ({ req, res }) =>
+ this.askFirefly(req.body.prompt).then(fire =>
+ DashUploadUtils.UploadImage(JSON.parse(fire).url).then(info => {
+ if (info instanceof Error) _invalid(res, info.message);
+ else _success(res, info.accessPaths.agnostic.client);
+ })
+ ),
+ });
+ }
+}