Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions components/convertapi/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import FormData from "form-data";
import { saveFile } from "../../common/utils.mjs";
import convertapi from "../../convertapi.app.mjs";

export default {
key: "convertapi-convert-base64-encoded-file",
name: "Convert Base64 Encoded File",
description: "This action converts a base64-string-encoded file into the user-specified format. [See the documentation](https://v2.convertapi.com/info/openapi)",
version: "0.0.1",
type: "action",
props: {
convertapi,
base64String: {
propDefinition: [
convertapi,
"base64String",
],
},
filename: {
type: "string",
label: "Filename",
description: "Converted output file name without extension. The extension will be added automatically.",
},
formatFrom: {
propDefinition: [
convertapi,
"formatFrom",
],
reloadProps: true,
},
},
async additionalProps() {
const props = {};
if (this.formatFrom) {
const { paths } = await this.convertapi.getAllowedFormats({
formatFrom: this.formatFrom,
});

const str = `/convert/${this.formatFrom}/to/`;

const allowedFormats = Object.keys(paths).filter((format) => {
if (format.startsWith(str)) {
return true;
}
})
.map((format) => format.slice(str.length));

props.formatTo = {
type: "string",
label: "Format To",
description: "The format to convert the file to.",
options: allowedFormats,
};
}
return props;
},
async run({ $ }) {
const buffer = Buffer.from(this.base64String, "base64");
const data = new FormData();
data.append("File", buffer, `${this.filename}.${this.formatFrom}`);

const { Files } = await this.convertapi.convertFileToFormat({
$,
data,
maxBodyLength: Infinity,
headers: data.getHeaders(),
formatFrom: this.formatFrom,
formatTo: this.formatTo,
});

await saveFile(Files);

$.export("$summary", `Successfully converted base64 encoded file to ${this.formatTo} and saved in /tmp directory as **${Files[0].FileName}**.`);
return;
},
};
77 changes: 77 additions & 0 deletions components/convertapi/actions/convert-file/convert-file.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import FormData from "form-data";
import fs from "fs";
import {
checkTmp, saveFile,
} from "../../common/utils.mjs";
import convertapi from "../../convertapi.app.mjs";

export default {
key: "convertapi-convert-file",
name: "Convert File",
description: "Use this action to convert files to the chosen format. [See the documentation](https://v2.convertapi.com/info/openapi)",
version: "0.0.1",
type: "action",
props: {
convertapi,
file: {
type: "string",
label: "File",
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)",
},
formatFrom: {
propDefinition: [
convertapi,
"formatFrom",
],
reloadProps: true,
},
},
async additionalProps() {
const props = {};
if (this.formatFrom) {
const { paths } = await this.convertapi.getAllowedFormats({
formatFrom: this.formatFrom,
});

const str = `/convert/${this.formatFrom}/to/`;

const allowedFormats = Object.keys(paths).filter((format) => {
if (format.startsWith(str)) {
return true;
}
})
.map((format) => format.slice(str.length));

props.formatTo = {
type: "string",
label: "Format To",
description: "The format to convert the file to.",
options: allowedFormats,
};
}
return props;
},
async run({ $ }) {
try {
const file = fs.createReadStream(checkTmp(this.file));
const data = new FormData();
data.append("File", file);

const { Files } = await this.convertapi.convertFileToFormat({
$,
data,
maxBodyLength: Infinity,
headers: data.getHeaders(),
formatFrom: this.formatFrom,
formatTo: this.formatTo,
});

await saveFile(Files);

$.export("$summary", `Successfully converted file to ${this.formatTo} format and saved in /tmp directory as **${Files[0].FileName}**.`);
return;
} catch (error) {
throw new Error(`Failed to convert file: ${error.message}`);
}
},
};
Loading
Loading