Skip to content

Commit

Permalink
New Components - fileforge (#13989)
Browse files Browse the repository at this point in the history
* fileforge init

* [Components] fileforge #13975
Actions
 - Generate PDF

* pnpm update

* some adjusts

* add form-data

* pnpm update
  • Loading branch information
luancazarine authored Sep 20, 2024
1 parent 004872e commit d83f8ee
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 8 deletions.
88 changes: 88 additions & 0 deletions components/fileforge/actions/generate-pdf/generate-pdf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import FormData from "form-data";
import fs from "fs";
import {
checkTmp,
parseObject,
} from "../../common/utils.mjs";
import fileforge from "../../fileforge.app.mjs";

export default {
key: "fileforge-generate-pdf",
name: "Generate PDF",
description: "Generate a PDF from provided HTML. [See the documentation](https://docs.fileforge.com/api-reference/api-reference/pdf/generate)",
version: "0.0.1",
type: "action",
props: {
fileforge,
alert: {
type: "alert",
alertType: "warning",
content: `An **\`index.html\`** file is required, and will be used as the main document.
Other documents may also be attached, such as stylesheets or images.
The path in the **\`filename\`** part of the multipart attachement will be respected during generation.
**Important notice:** during generation, the **\`index.html\`** file will be processed to include the base URL of the document.
This is required for assets to be loaded correctly.
To link your assets from the HTML file, you should not use a leading slash in the URL.
For example, use **\`<img src="image.jpg" />\`** instead of **\`<img src="/image.jpg" />\`**.`,
},
files: {
type: "string[]",
label: "HTML Files",
description: "The HTML files to convert to PDF. Each file should be a valid path to an HTML file saved to the `/tmp` directory (e.g. `/tmp/image.png`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory)..",
},
test: {
type: "boolean",
label: "Test",
description: "Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true.",
optional: true,
},
expiresAt: {
type: "string",
label: "Expires At",
description: "The expiration timestamp for the PDF in ISO 8601 format",
optional: true,
},
fileName: {
type: "string",
label: "Filename",
description: "The desired filename for the generated PDF.",
optional: true,
},
allowViewing: {
type: "boolean",
label: "Allow Viewing",
description: "Specifies whether viewing is allowed.",
optional: true,
},
},
async run({ $ }) {
const {
fileforge,
files,
...data
} = this;

const formData = new FormData();
const parsedFiles = parseObject(files);

for (const file of parsedFiles) {
formData.append("files", fs.createReadStream(checkTmp(file)));
}

formData.append("options", JSON.stringify({
...data,
host: true,
}), {
contentType: "application/json",
});

const response = await fileforge.generatePDF({
$,
data: formData,
headers: formData.getHeaders(),
});

$.export("$summary", "Successfully generated the PDF file.");
return response;
},
};
31 changes: 31 additions & 0 deletions components/fileforge/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};

export const checkTmp = (filename) => {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
};
32 changes: 27 additions & 5 deletions components/fileforge/fileforge.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "fileforge",
propDefinitions: {},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.fileforge.com";
},
_headers(headers = {}) {
return {
"X-API-Key": this.$auth.api_key,
...headers,
};
},
_makeRequest({
$ = this, path, headers, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(headers),
...opts,
});
},
generatePDF(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/pdf/generate/",
...opts,
});
},
},
};
};
8 changes: 6 additions & 2 deletions components/fileforge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/fileforge",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Fileforge Components",
"main": "fileforge.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.1",
"form-data": "^4.0.0"
}
}
}
7 changes: 6 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d83f8ee

Please sign in to comment.