Skip to content

Commit 159d440

Browse files
New Components - convertapi (#16425)
* convertapi init * [Components] convertapi #13324 Actions - Convert Base64 Encoded File - Convert File - Convert Web URL * pnpm update * some adjusts * Update components/convertapi/actions/convert-web-url/convert-web-url.mjs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 60d5310 commit 159d440

File tree

10 files changed

+891
-26
lines changed

10 files changed

+891
-26
lines changed

components/convertapi/.gitignore

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import FormData from "form-data";
2+
import { saveFile } from "../../common/utils.mjs";
3+
import convertapi from "../../convertapi.app.mjs";
4+
5+
export default {
6+
key: "convertapi-convert-base64-encoded-file",
7+
name: "Convert Base64 Encoded File",
8+
description: "This action converts a base64-string-encoded file into the user-specified format. [See the documentation](https://v2.convertapi.com/info/openapi)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
convertapi,
13+
base64String: {
14+
propDefinition: [
15+
convertapi,
16+
"base64String",
17+
],
18+
},
19+
filename: {
20+
type: "string",
21+
label: "Filename",
22+
description: "Converted output file name without extension. The extension will be added automatically.",
23+
},
24+
formatFrom: {
25+
propDefinition: [
26+
convertapi,
27+
"formatFrom",
28+
],
29+
reloadProps: true,
30+
},
31+
},
32+
async additionalProps() {
33+
const props = {};
34+
if (this.formatFrom) {
35+
const { paths } = await this.convertapi.getAllowedFormats({
36+
formatFrom: this.formatFrom,
37+
});
38+
39+
const str = `/convert/${this.formatFrom}/to/`;
40+
41+
const allowedFormats = Object.keys(paths).filter((format) => {
42+
if (format.startsWith(str)) {
43+
return true;
44+
}
45+
})
46+
.map((format) => format.slice(str.length));
47+
48+
props.formatTo = {
49+
type: "string",
50+
label: "Format To",
51+
description: "The format to convert the file to.",
52+
options: allowedFormats,
53+
};
54+
}
55+
return props;
56+
},
57+
async run({ $ }) {
58+
const buffer = Buffer.from(this.base64String, "base64");
59+
const data = new FormData();
60+
data.append("File", buffer, `${this.filename}.${this.formatFrom}`);
61+
62+
const { Files } = await this.convertapi.convertFileToFormat({
63+
$,
64+
data,
65+
maxBodyLength: Infinity,
66+
headers: data.getHeaders(),
67+
formatFrom: this.formatFrom,
68+
formatTo: this.formatTo,
69+
});
70+
71+
await saveFile(Files);
72+
const filename = Files[0].FileName;
73+
74+
$.export("$summary", `Successfully converted base64 encoded file to ${this.formatTo} and saved in /tmp directory as **${filename}**.`);
75+
return {
76+
filepath: `/tmp/${filename}`,
77+
};
78+
},
79+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import FormData from "form-data";
2+
import fs from "fs";
3+
import {
4+
checkTmp, saveFile,
5+
} from "../../common/utils.mjs";
6+
import convertapi from "../../convertapi.app.mjs";
7+
8+
export default {
9+
key: "convertapi-convert-file",
10+
name: "Convert File",
11+
description: "Use this action to convert files to the chosen format. [See the documentation](https://v2.convertapi.com/info/openapi)",
12+
version: "0.0.1",
13+
type: "action",
14+
props: {
15+
convertapi,
16+
file: {
17+
type: "string",
18+
label: "File",
19+
description: "The path to the file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
20+
},
21+
formatFrom: {
22+
propDefinition: [
23+
convertapi,
24+
"formatFrom",
25+
],
26+
reloadProps: true,
27+
},
28+
},
29+
async additionalProps() {
30+
const props = {};
31+
if (this.formatFrom) {
32+
const { paths } = await this.convertapi.getAllowedFormats({
33+
formatFrom: this.formatFrom,
34+
});
35+
36+
const str = `/convert/${this.formatFrom}/to/`;
37+
38+
const allowedFormats = Object.keys(paths).filter((format) => {
39+
if (format.startsWith(str)) {
40+
return true;
41+
}
42+
})
43+
.map((format) => format.slice(str.length));
44+
45+
props.formatTo = {
46+
type: "string",
47+
label: "Format To",
48+
description: "The format to convert the file to.",
49+
options: allowedFormats,
50+
};
51+
}
52+
return props;
53+
},
54+
async run({ $ }) {
55+
try {
56+
const file = fs.createReadStream(checkTmp(this.file));
57+
const data = new FormData();
58+
data.append("File", file);
59+
60+
const { Files } = await this.convertapi.convertFileToFormat({
61+
$,
62+
data,
63+
maxBodyLength: Infinity,
64+
headers: data.getHeaders(),
65+
formatFrom: this.formatFrom,
66+
formatTo: this.formatTo,
67+
});
68+
69+
await saveFile(Files);
70+
const filename = Files[0].FileName;
71+
72+
$.export("$summary", `Successfully converted file to ${this.formatTo} format and saved in /tmp directory as **${filename}**.`);
73+
return {
74+
filepath: `/tmp/${filename}`,
75+
};
76+
} catch (error) {
77+
throw new Error(`Failed to convert file: ${error.message}`);
78+
}
79+
},
80+
};

0 commit comments

Comments
 (0)