Skip to content

Commit a57dd3d

Browse files
authored
New Components - shortpixel (#15516)
* new component * pnpm-lock.yaml
1 parent 22e9810 commit a57dd3d

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import shortpixel from "../../shortpixel.app.mjs";
2+
import { ConfigurationError } from "@pipedream/platform";
3+
import fs from "fs";
4+
5+
export default {
6+
key: "shortpixel-optimize-image",
7+
name: "Optimize Image",
8+
description: "Optimize and/or adjust an image using ShortPixel. [See the documentation](https://shortpixel.com/knowledge-base/article/shortpixel-adaptive-images-api-parameters/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
shortpixel,
13+
url: {
14+
type: "string",
15+
label: "URL",
16+
description: "The URL of the image to optimize",
17+
},
18+
width: {
19+
type: "integer",
20+
label: "Width",
21+
description: "The width in pixels of the new image",
22+
optional: true,
23+
},
24+
height: {
25+
type: "integer",
26+
label: "Height",
27+
description: "The height in pixels of the new image",
28+
optional: true,
29+
},
30+
cropStyle: {
31+
type: "string",
32+
label: "Crop Style",
33+
description: "The crop style, useful when both width and height are specified",
34+
options: [
35+
"top",
36+
"right",
37+
"bottom",
38+
"left",
39+
"center",
40+
],
41+
optional: true,
42+
},
43+
quality: {
44+
type: "string",
45+
label: "Quality",
46+
description: "The quality setting of the new image",
47+
options: [
48+
"lqip",
49+
"lossless",
50+
"glossy",
51+
"lossy",
52+
],
53+
optional: true,
54+
},
55+
filename: {
56+
type: "string",
57+
label: "Filename",
58+
description: "Optionally, enter a filename that will be used to save the image in /tmp",
59+
optional: true,
60+
},
61+
},
62+
methods: {
63+
buildParams() {
64+
const paramArray = [
65+
"ret_wait ",
66+
];
67+
if (this.width) {
68+
paramArray.push(`w_${this.width}`);
69+
}
70+
if (this.height) {
71+
paramArray.push(`h_${this.height}`);
72+
}
73+
if (this.cropStyle) {
74+
paramArray.push(`c_${this.cropStyle}`);
75+
}
76+
if (this.quality) {
77+
paramArray.push(`q_${this.quality}`);
78+
}
79+
return paramArray.join(",");
80+
},
81+
downloadFileToTmp(file, filePath) {
82+
const rawcontent = file.toString("base64");
83+
const buffer = Buffer.from(rawcontent, "base64");
84+
fs.writeFileSync(filePath, buffer);
85+
},
86+
},
87+
async run({ $ }) {
88+
if (!this.width && !this.height && !this.cropStyle && !this.quality) {
89+
throw new ConfigurationError("Must enter at least one of `width`, `height`, `cropStyle`, or `quality`");
90+
}
91+
92+
const params = this.buildParams();
93+
94+
let response = {
95+
url: `${this.shortpixel._baseUrl()}/client/${params}/${this.url}`,
96+
};
97+
98+
try {
99+
const image = await this.shortpixel.optimizeImage({
100+
$,
101+
params,
102+
url: this.url,
103+
responseType: "arraybuffer",
104+
});
105+
if (this.filename) {
106+
const filePath = this.filename.includes("tmp/")
107+
? this.filename
108+
: `/tmp/${this.filename}`;
109+
this.downloadFileToTmp(image, filePath);
110+
response.filePath = filePath;
111+
}
112+
} catch {
113+
throw new Error(`Unable to process image at URL: ${this.url}`);
114+
}
115+
116+
return response;
117+
},
118+
};

components/shortpixel/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/shortpixel",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream ShortPixel Components",
55
"main": "shortpixel.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "shortpixel",
46
propDefinitions: {},
57
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
8+
_baseUrl() {
9+
return "https://cdn.shortpixel.ai";
10+
},
11+
_makeRequest({
12+
$ = this,
13+
path,
14+
params,
15+
...opts
16+
}) {
17+
return axios($, {
18+
url: `${this._baseUrl()}${path}`,
19+
params: {
20+
...params,
21+
key: this.$auth.api_key,
22+
},
23+
...opts,
24+
});
25+
},
26+
optimizeImage({
27+
params, url, ...opts
28+
}) {
29+
return this._makeRequest({
30+
path: `/client/${params}/${url}`,
31+
...opts,
32+
});
933
},
1034
},
1135
};

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)