Skip to content

Commit 5517033

Browse files
committed
[Components] convertapi #13324
Actions - Convert Base64 Encoded File - Convert File - Convert Web URL
1 parent 82e2584 commit 5517033

File tree

9 files changed

+725
-370
lines changed

9 files changed

+725
-370
lines changed

components/convertapi/.gitignore

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import FormData from "form-data";
2+
import { saveFile } from "../../common/utils.mjs";
13
import convertapi from "../../convertapi.app.mjs";
2-
import { axios } from "@pipedream/platform";
34

45
export default {
56
key: "convertapi-convert-base64-encoded-file",
67
name: "Convert Base64 Encoded File",
78
description: "This action converts a base64-string-encoded file into the user-specified format. [See the documentation](https://v2.convertapi.com/info/openapi)",
8-
version: "0.0.{{ts}}",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
1112
convertapi,
@@ -15,31 +16,61 @@ export default {
1516
"base64String",
1617
],
1718
},
18-
format: {
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: {
1925
propDefinition: [
2026
convertapi,
21-
"format",
27+
"formatFrom",
2228
],
23-
optional: true,
29+
reloadProps: true,
2430
},
2531
},
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+
},
2657
async run({ $ }) {
27-
const response = await this.convertapi.convertBase64ToFormat({
28-
base64String: this.base64String,
29-
format: this.format,
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,
3069
});
3170

32-
const files = response.Files || [];
33-
for (const file of files) {
34-
const filePath = `/tmp/${file.FileName}`;
35-
await axios($, {
36-
method: "GET",
37-
url: file.Url,
38-
responseType: "arraybuffer",
39-
}).then((buffer) => require("fs").promises.writeFile(filePath, buffer));
40-
}
71+
await saveFile(Files);
4172

42-
$.export("$summary", `Successfully converted base64 encoded file to ${this.format || "default format"}`);
43-
return response;
73+
$.export("$summary", `Successfully converted base64 encoded file to ${this.formatTo} and saved in /tmp directory as **${Files[0].FileName}**.`);
74+
return;
4475
},
4576
};

components/convertapi/actions/convert-file/convert-file.mjs

+52-14
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,75 @@
1+
import FormData from "form-data";
2+
import fs from "fs";
3+
import {
4+
checkTmp, saveFile,
5+
} from "../../common/utils.mjs";
16
import convertapi from "../../convertapi.app.mjs";
2-
import { axios } from "@pipedream/platform";
37

48
export default {
59
key: "convertapi-convert-file",
610
name: "Convert File",
711
description: "Use this action to convert files to the chosen format. [See the documentation](https://v2.convertapi.com/info/openapi)",
8-
version: "0.0.{{ts}}",
12+
version: "0.0.1",
913
type: "action",
1014
props: {
1115
convertapi,
1216
file: {
13-
propDefinition: [
14-
convertapi,
15-
"file",
16-
],
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)",
1720
},
18-
format: {
21+
formatFrom: {
1922
propDefinition: [
2023
convertapi,
21-
"format",
24+
"formatFrom",
2225
],
23-
optional: true,
26+
reloadProps: true,
2427
},
2528
},
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+
},
2654
async run({ $ }) {
2755
try {
28-
const response = await this.convertapi.convertFileToFormat({
29-
file: this.file,
30-
format: this.format,
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,
3167
});
3268

33-
$.export("$summary", `Successfully converted file to ${this.format || "default"} format.`);
34-
return response;
69+
await saveFile(Files);
70+
71+
$.export("$summary", `Successfully converted file to ${this.formatTo} format and saved in /tmp directory as **${Files[0].FileName}**.`);
72+
return;
3573
} catch (error) {
3674
throw new Error(`Failed to convert file: ${error.message}`);
3775
}

0 commit comments

Comments
 (0)