|
1 | 1 | import alttextAi from "../../alttext_ai.app.mjs"; |
2 | | -import fs from "fs"; |
| 2 | +import { |
| 3 | + ConfigurationError, getFileStream, |
| 4 | +} from "@pipedream/platform"; |
3 | 5 | import { LANGUAGE_OPTIONS } from "../../commons/constants.mjs"; |
4 | | -import { ConfigurationError } from "@pipedream/platform"; |
5 | 6 |
|
6 | 7 | export default { |
7 | 8 | key: "alttext_ai-generate-alt-text", |
8 | 9 | name: "Generate Alt Text", |
9 | 10 | description: |
10 | 11 | "Generates a descriptive alt text for a given image. [See the documentation](https://alttext.ai/apidocs#tag/Images/operation/create-image)", |
11 | | - version: "0.0.1", |
| 12 | + version: "0.1.0", |
12 | 13 | type: "action", |
13 | 14 | props: { |
14 | 15 | alttextAi, |
| 16 | + fileInfo: { |
| 17 | + type: "alert", |
| 18 | + alertType: "warning", |
| 19 | + content: "Either `Image Data` or `Image File Path or URL` should be provided. If both are provided, `Image Data` will be used.", |
| 20 | + }, |
15 | 21 | imageData: { |
16 | 22 | type: "string", |
17 | 23 | label: "Image Data", |
18 | 24 | description: |
19 | | - "The image data in base64 format. Only one of `Image Data`, `Image File Path` or `Image URL` should be specified.", |
20 | | - optional: true, |
21 | | - }, |
22 | | - imageUrl: { |
23 | | - type: "string", |
24 | | - label: "Image URL", |
25 | | - description: |
26 | | - "The public URL to an image. Only one of `Image URL`, `Image Data` or `Image File Path` should be specified.", |
| 25 | + "The image data in base64 format", |
27 | 26 | optional: true, |
28 | 27 | }, |
29 | 28 | imageFilePath: { |
30 | 29 | type: "string", |
31 | | - label: "Image File Path", |
| 30 | + label: "Image File Path or URL", |
32 | 31 | description: |
33 | | - "The path to an image file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#the-tmp-directory). Only one of `Image File Path`, `Image URL` or `Image Data` should be specified.", |
| 32 | + "The image to process. Provide either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/myImage.jpg`)", |
34 | 33 | optional: true, |
35 | 34 | }, |
36 | 35 | keywords: { |
@@ -64,31 +63,29 @@ export default { |
64 | 63 | }, |
65 | 64 | }, |
66 | 65 | async run({ $ }) { |
67 | | - if ( |
68 | | - (!this.imageData && !this.imageFilePath && !this.imageUrl) |
69 | | - || (this.imageData && this.imageFilePath) |
70 | | - || (this.imageData && this.imageUrl) |
71 | | - || (this.imageFilePath && this.imageUrl) |
72 | | - ) { |
73 | | - throw new ConfigurationError("Only one of `Image Data`, `Image File Path` or `Image URL` should be specified."); |
| 66 | + const { |
| 67 | + imageData, imageFilePath, |
| 68 | + } = this; |
| 69 | + if (!imageData && !imageFilePath) { |
| 70 | + throw new ConfigurationError("Either `Image Data` or `Image File Path or URL` should be specified."); |
| 71 | + } |
| 72 | + |
| 73 | + let rawData = imageData; |
| 74 | + if (!rawData) { |
| 75 | + const stream = await getFileStream(imageFilePath); |
| 76 | + const chunks = []; |
| 77 | + for await (const chunk of stream) { |
| 78 | + chunks.push(chunk); |
| 79 | + } |
| 80 | + const buffer = Buffer.concat(chunks); |
| 81 | + rawData = buffer.toString("base64"); |
74 | 82 | } |
75 | 83 |
|
76 | 84 | const response = await this.alttextAi.generateAltText({ |
77 | 85 | $, |
78 | 86 | data: { |
79 | 87 | image: { |
80 | | - url: this.imageUrl, |
81 | | - raw: |
82 | | - this.imageData ?? |
83 | | - (this.imageFilePath && |
84 | | - fs.readFileSync( |
85 | | - this.imageFilePath.includes("tmp/") |
86 | | - ? this.imageFilePath |
87 | | - : `/tmp/${this.imageFilePath}`, |
88 | | - { |
89 | | - encoding: "base64", |
90 | | - }, |
91 | | - )), |
| 88 | + raw: rawData, |
92 | 89 | }, |
93 | 90 | keywords: this.keywords, |
94 | 91 | negative_keywords: this.negativeKeywords, |
|
0 commit comments