From 22f1831ee5172201893bc234a1bd934f8d76d187 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Thu, 6 Feb 2025 15:49:22 -0500 Subject: [PATCH 1/2] new component --- .../actions/optimize-image/optimize-image.mjs | 118 ++++++++++++++++++ components/shortpixel/package.json | 7 +- components/shortpixel/shortpixel.app.mjs | 30 ++++- 3 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 components/shortpixel/actions/optimize-image/optimize-image.mjs diff --git a/components/shortpixel/actions/optimize-image/optimize-image.mjs b/components/shortpixel/actions/optimize-image/optimize-image.mjs new file mode 100644 index 0000000000000..3c0fffd097499 --- /dev/null +++ b/components/shortpixel/actions/optimize-image/optimize-image.mjs @@ -0,0 +1,118 @@ +import shortpixel from "../../shortpixel.app.mjs"; +import { ConfigurationError } from "@pipedream/platform"; +import fs from "fs"; + +export default { + key: "shortpixel-optimize-image", + name: "Optimize Image", + description: "Optimize and/or adjust an image using ShortPixel. [See the documentation](https://shortpixel.com/knowledge-base/article/shortpixel-adaptive-images-api-parameters/)", + version: "0.0.1", + type: "action", + props: { + shortpixel, + url: { + type: "string", + label: "URL", + description: "The URL of the image to optimize", + }, + width: { + type: "integer", + label: "Width", + description: "The width in pixels of the new image", + optional: true, + }, + height: { + type: "integer", + label: "Height", + description: "The height in pixels of the new image", + optional: true, + }, + cropStyle: { + type: "string", + label: "Crop Style", + description: "The crop style, useful when both width and height are specified", + options: [ + "top", + "right", + "bottom", + "left", + "center", + ], + optional: true, + }, + quality: { + type: "string", + label: "Quality", + description: "The quality setting of the new image", + options: [ + "lqip", + "lossless", + "glossy", + "lossy", + ], + optional: true, + }, + filename: { + type: "string", + label: "Filename", + description: "Optionally, enter a filename that will be used to save the image in /tmp", + optional: true, + }, + }, + methods: { + buildParams() { + const paramArray = [ + "ret_wait ", + ]; + if (this.width) { + paramArray.push(`w_${this.width}`); + } + if (this.height) { + paramArray.push(`h_${this.height}`); + } + if (this.cropStyle) { + paramArray.push(`c_${this.cropStyle}`); + } + if (this.quality) { + paramArray.push(`q_${this.quality}`); + } + return paramArray.join(","); + }, + downloadFileToTmp(file, filePath) { + const rawcontent = file.toString("base64"); + const buffer = Buffer.from(rawcontent, "base64"); + fs.writeFileSync(filePath, buffer); + }, + }, + async run({ $ }) { + if (!this.width && !this.height && !this.cropStyle && !this.quality) { + throw new ConfigurationError("Must enter at least one of `width`, `height`, `cropStyle`, or `quality`"); + } + + const params = this.buildParams(); + + let response = { + url: `${this.shortpixel._baseUrl()}/client/${params}/${this.url}`, + }; + + try { + const image = await this.shortpixel.optimizeImage({ + $, + params, + url: this.url, + responseType: "arraybuffer", + }); + if (this.filename) { + const filePath = this.filename.includes("tmp/") + ? this.filename + : `/tmp/${this.filename}`; + this.downloadFileToTmp(image, filePath); + response.filePath = filePath; + } + } catch { + throw new Error(`Unable to process image at URL: ${this.url}`); + } + + return response; + }, +}; diff --git a/components/shortpixel/package.json b/components/shortpixel/package.json index bbe027ce85535..2f3f6e60c303b 100644 --- a/components/shortpixel/package.json +++ b/components/shortpixel/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/shortpixel", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream ShortPixel Components", "main": "shortpixel.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/components/shortpixel/shortpixel.app.mjs b/components/shortpixel/shortpixel.app.mjs index 91d9ff22d0ee0..9a5ad65e85713 100644 --- a/components/shortpixel/shortpixel.app.mjs +++ b/components/shortpixel/shortpixel.app.mjs @@ -1,11 +1,35 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "shortpixel", propDefinitions: {}, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://cdn.shortpixel.ai"; + }, + _makeRequest({ + $ = this, + path, + params, + ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + params: { + ...params, + key: this.$auth.api_key, + }, + ...opts, + }); + }, + optimizeImage({ + params, url, ...opts + }) { + return this._makeRequest({ + path: `/client/${params}/${url}`, + ...opts, + }); }, }, }; From 901588693a3c16eb312c7a0dbafd0db333ddb2ed Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Thu, 6 Feb 2025 15:52:14 -0500 Subject: [PATCH 2/2] pnpm-lock.yaml --- pnpm-lock.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ad98518188904..fbaa60df6a854 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9759,7 +9759,11 @@ importers: specifier: ^1.6.0 version: 1.6.6 - components/shortpixel: {} + components/shortpixel: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/shotstack: dependencies: