diff --git a/components/deepimage/actions/auto-enhance/auto-enhance.mjs b/components/deepimage/actions/auto-enhance/auto-enhance.mjs new file mode 100644 index 0000000000000..22f933424d039 --- /dev/null +++ b/components/deepimage/actions/auto-enhance/auto-enhance.mjs @@ -0,0 +1,30 @@ +import { getUrlOrFile } from "../../common/utils.mjs"; +import deepimage from "../../deepimage.app.mjs"; + +export default { + key: "deepimage-auto-enhance", + name: "Auto Enhance Image", + description: "Improves the provided image. [See the documentation](https://documentation.deep-image.ai/image-processing/auto-enhance)", + version: "0.0.1", + type: "action", + props: { + deepimage, + image: { + propDefinition: [ + deepimage, + "image", + ], + }, + }, + async run({ $ }) { + const response = await this.deepimage.makeRequest({ + data: { + url: getUrlOrFile(this.image), + preset: "auto_enhance", + }, + }); + + $.export("$summary", "Successfully enhanced the image."); + return response; + }, +}; diff --git a/components/deepimage/actions/remove-background/remove-background.mjs b/components/deepimage/actions/remove-background/remove-background.mjs new file mode 100644 index 0000000000000..4fff3c45ced44 --- /dev/null +++ b/components/deepimage/actions/remove-background/remove-background.mjs @@ -0,0 +1,55 @@ +import { + BACKGROUND_COLOR_OPTIONS, CROP_TYPE_OPTIONS, +} from "../../common/constants.mjs"; +import { getUrlOrFile } from "../../common/utils.mjs"; +import deepimage from "../../deepimage.app.mjs"; + +export default { + key: "deepimage-remove-background", + name: "Remove Background", + description: "Removes the background from the provided image using DeepImage. [See the documentation](https://documentation.deep-image.ai/image-processing/background-processing)", + version: "0.0.1", + type: "action", + props: { + deepimage, + image: { + propDefinition: [ + deepimage, + "image", + ], + }, + backgroundColor: { + type: "string", + label: "Background Color", + description: "The background color for the image.", + options: BACKGROUND_COLOR_OPTIONS, + }, + cropType: { + type: "string", + label: "Crop Type", + description: "The crop type for background removal.", + optional: true, + options: CROP_TYPE_OPTIONS, + }, + }, + async run({ $ }) { + const response = await this.deepimage.makeRequest({ + $, + data: { + url: getUrlOrFile(this.image), + background: { + remove: "auto", + color: this.backgroundColor, + }, + fit: this.cropType + ? { + crop: this.cropType, + } + : {}, + }, + }); + + $.export("$summary", "Background removal successful"); + return response; + }, +}; diff --git a/components/deepimage/actions/upscale/upscale.mjs b/components/deepimage/actions/upscale/upscale.mjs new file mode 100644 index 0000000000000..8043b35473bdd --- /dev/null +++ b/components/deepimage/actions/upscale/upscale.mjs @@ -0,0 +1,44 @@ +import { getUrlOrFile } from "../../common/utils.mjs"; +import deepimage from "../../deepimage.app.mjs"; + +export default { + key: "deepimage-upscale", + name: "Upscale Image", + description: "Upscales the provided image using Deep Image. [See the documentation](https://documentation.deep-image.ai/image-processing/resize-and-padding)", + version: "0.0.1", + type: "action", + props: { + deepimage, + image: { + propDefinition: [ + deepimage, + "image", + ], + }, + upscaleMultiplier: { + type: "integer", + label: "Upscale Multiplier", + description: "The factor by which to upscale the image in %.", + }, + generativeUpscale: { + type: "boolean", + label: "Generative Upscale", + description: "Whether to use generative upscale.", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.deepimage.makeRequest({ + $, + data: { + url: getUrlOrFile(this.image), + width: `${this.upscaleMultiplier}%`, + height: `${this.upscaleMultiplier}%`, + generative_upscale: this.generativeUpscale, + }, + }); + + $.export("$summary", "Successfully upscaled the image"); + return response; + }, +}; diff --git a/components/deepimage/common/constants.mjs b/components/deepimage/common/constants.mjs new file mode 100644 index 0000000000000..83057231e4f19 --- /dev/null +++ b/components/deepimage/common/constants.mjs @@ -0,0 +1,37 @@ +export const BACKGROUND_COLOR_OPTIONS = [ + { + label: "White", + value: "#FFFFFF", + }, + { + label: "Transparent", + value: "transparent", + }, +]; + +export const CROP_TYPE_OPTIONS = [ + { + label: "Crop center", + value: "center", + }, + { + label: "Crop item", + value: "item", + }, + { + label: "Crop content", + value: "content", + }, + { + label: "Cover", + value: "cover", + }, + { + label: "Canvas", + value: "canvas", + }, + { + label: "Bounds", + value: "bounds", + }, +]; diff --git a/components/deepimage/common/utils.mjs b/components/deepimage/common/utils.mjs new file mode 100644 index 0000000000000..bde896bb93a73 --- /dev/null +++ b/components/deepimage/common/utils.mjs @@ -0,0 +1,27 @@ +import fs from "fs"; + +export const isValidUrl = (urlString) => { + var urlPattern = new RegExp("^(https?:\\/\\/)?" + // validate protocol +"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name +"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address +"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path +"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string +"(\\#[-a-z\\d_]*)?$", "i"); // validate fragment locator + return !!urlPattern.test(urlString); +}; + +export const checkTmp = (filename) => { + if (filename.indexOf("/tmp") === -1) { + return `/tmp/${filename}`; + } + return filename; +}; + +export const getUrlOrFile = (url) => { + if (!isValidUrl(url)) { + const data = fs.readFileSync(checkTmp(url)); + const base64Image = Buffer.from(data, "binary").toString("base64"); + return `base64,${base64Image}`; + } + return url; +}; diff --git a/components/deepimage/deepimage.app.mjs b/components/deepimage/deepimage.app.mjs index 6d3b6cac93c3f..9d68f6fe63f94 100644 --- a/components/deepimage/deepimage.app.mjs +++ b/components/deepimage/deepimage.app.mjs @@ -1,11 +1,34 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "deepimage", - propDefinitions: {}, + propDefinitions: { + image: { + type: "string", + label: "Image", + description: "The URL of the image or the path to the file saved to the `/tmp` directory (e.g. `/tmp/example.jpg`) to process. [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://deep-image.ai/rest_api/process_result"; + }, + _headers() { + return { + "content-type": "application/json", + "x-api-key": `${this.$auth.api_key}`, + }; + }, + makeRequest({ + $ = this, ...opts + }) { + return axios($, { + method: "POST", + url: this._baseUrl(), + headers: this._headers(), + ...opts, + }); }, }, }; diff --git a/components/deepimage/package.json b/components/deepimage/package.json index 5feea41b0a543..f22362bab2104 100644 --- a/components/deepimage/package.json +++ b/components/deepimage/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/deepimage", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream DeepImage Components", "main": "deepimage.app.mjs", "keywords": [ @@ -11,5 +11,9 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3", + "fs": "^0.0.1-security" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c445c9e62404..ab24a849bf1cf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2430,7 +2430,12 @@ importers: '@pipedream/platform': 1.5.1 components/deepimage: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.3 + fs: ^0.0.1-security + dependencies: + '@pipedream/platform': 3.0.3 + fs: 0.0.1-security components/deepl: specifiers: {} @@ -13116,55 +13121,6 @@ packages: - aws-crt dev: false - /@aws-sdk/client-sso-oidc/3.600.0_tdq3komn4zwyd65w7klbptsu34: - resolution: {integrity: sha512-7+I8RWURGfzvChyNQSyj5/tKrqRbzRl7H+BnTOf/4Vsw1nFOi5ROhlhD4X/Y0QCTacxnaoNcIrqnY7uGGvVRzw==} - engines: {node: '>=16.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sts': 3.600.0 - '@aws-sdk/core': 3.598.0 - '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 - '@aws-sdk/middleware-host-header': 3.598.0 - '@aws-sdk/middleware-logger': 3.598.0 - '@aws-sdk/middleware-recursion-detection': 3.598.0 - '@aws-sdk/middleware-user-agent': 3.598.0 - '@aws-sdk/region-config-resolver': 3.598.0 - '@aws-sdk/types': 3.598.0 - '@aws-sdk/util-endpoints': 3.598.0 - '@aws-sdk/util-user-agent-browser': 3.598.0 - '@aws-sdk/util-user-agent-node': 3.598.0 - '@smithy/config-resolver': 3.0.3 - '@smithy/core': 2.2.3 - '@smithy/fetch-http-handler': 3.2.1 - '@smithy/hash-node': 3.0.2 - '@smithy/invalid-dependency': 3.0.2 - '@smithy/middleware-content-length': 3.0.2 - '@smithy/middleware-endpoint': 3.0.4 - '@smithy/middleware-retry': 3.0.6 - '@smithy/middleware-serde': 3.0.3 - '@smithy/middleware-stack': 3.0.3 - '@smithy/node-config-provider': 3.1.3 - '@smithy/node-http-handler': 3.1.2 - '@smithy/protocol-http': 4.0.3 - '@smithy/smithy-client': 3.1.6 - '@smithy/types': 3.3.0 - '@smithy/url-parser': 3.0.3 - '@smithy/util-base64': 3.0.0 - '@smithy/util-body-length-browser': 3.0.0 - '@smithy/util-body-length-node': 3.0.0 - '@smithy/util-defaults-mode-browser': 3.0.6 - '@smithy/util-defaults-mode-node': 3.0.6 - '@smithy/util-endpoints': 2.0.3 - '@smithy/util-middleware': 3.0.3 - '@smithy/util-retry': 3.0.2 - '@smithy/util-utf8': 3.0.0 - tslib: 2.6.3 - transitivePeerDependencies: - - '@aws-sdk/client-sts' - - aws-crt - dev: false - /@aws-sdk/client-sso/3.423.0: resolution: {integrity: sha512-znIufHkwhCIePgaYciIs3x/+BpzR57CZzbCKHR9+oOvGyufEPPpUT5bFLvbwTgfiVkTjuk6sG/ES3U5Bc+xtrA==} engines: {node: '>=14.0.0'} @@ -13400,7 +13356,55 @@ packages: dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sso-oidc': 3.600.0_tdq3komn4zwyd65w7klbptsu34 + '@aws-sdk/client-sso-oidc': 3.600.0 + '@aws-sdk/core': 3.598.0 + '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 + '@aws-sdk/middleware-host-header': 3.598.0 + '@aws-sdk/middleware-logger': 3.598.0 + '@aws-sdk/middleware-recursion-detection': 3.598.0 + '@aws-sdk/middleware-user-agent': 3.598.0 + '@aws-sdk/region-config-resolver': 3.598.0 + '@aws-sdk/types': 3.598.0 + '@aws-sdk/util-endpoints': 3.598.0 + '@aws-sdk/util-user-agent-browser': 3.598.0 + '@aws-sdk/util-user-agent-node': 3.598.0 + '@smithy/config-resolver': 3.0.3 + '@smithy/core': 2.2.3 + '@smithy/fetch-http-handler': 3.2.1 + '@smithy/hash-node': 3.0.2 + '@smithy/invalid-dependency': 3.0.2 + '@smithy/middleware-content-length': 3.0.2 + '@smithy/middleware-endpoint': 3.0.4 + '@smithy/middleware-retry': 3.0.6 + '@smithy/middleware-serde': 3.0.3 + '@smithy/middleware-stack': 3.0.3 + '@smithy/node-config-provider': 3.1.3 + '@smithy/node-http-handler': 3.1.2 + '@smithy/protocol-http': 4.0.3 + '@smithy/smithy-client': 3.1.6 + '@smithy/types': 3.3.0 + '@smithy/url-parser': 3.0.3 + '@smithy/util-base64': 3.0.0 + '@smithy/util-body-length-browser': 3.0.0 + '@smithy/util-body-length-node': 3.0.0 + '@smithy/util-defaults-mode-browser': 3.0.6 + '@smithy/util-defaults-mode-node': 3.0.6 + '@smithy/util-endpoints': 2.0.3 + '@smithy/util-middleware': 3.0.3 + '@smithy/util-retry': 3.0.2 + '@smithy/util-utf8': 3.0.0 + tslib: 2.6.3 + transitivePeerDependencies: + - aws-crt + dev: false + + /@aws-sdk/client-sts/3.600.0_dseaa2p5u2yk67qiepewcq3hkq: + resolution: {integrity: sha512-KQG97B7LvTtTiGmjlrG1LRAY8wUvCQzrmZVV5bjrJ/1oXAU7DITYwVbSJeX9NWg6hDuSk0VE3MFwIXS2SvfLIA==} + engines: {node: '>=16.0.0'} + dependencies: + '@aws-crypto/sha256-browser': 5.2.0 + '@aws-crypto/sha256-js': 5.2.0 + '@aws-sdk/client-sso-oidc': 3.600.0 '@aws-sdk/core': 3.598.0 '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 '@aws-sdk/middleware-host-header': 3.598.0 @@ -13439,6 +13443,7 @@ packages: '@smithy/util-utf8': 3.0.0 tslib: 2.6.3 transitivePeerDependencies: + - '@aws-sdk/client-sso-oidc' - aws-crt dev: false @@ -17734,7 +17739,7 @@ packages: '@aws-sdk/client-sns': 3.423.0 '@aws-sdk/client-sqs': 3.423.0 '@aws-sdk/client-ssm': 3.423.0 - '@aws-sdk/client-sts': 3.600.0 + '@aws-sdk/client-sts': 3.600.0_dseaa2p5u2yk67qiepewcq3hkq '@aws-sdk/s3-request-presigner': 3.609.0 '@pipedream/helper_functions': 0.3.12 '@pipedream/platform': 1.6.6