|
| 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 | +}; |
0 commit comments