Skip to content

Commit 0c5ad79

Browse files
New Components - transloadit (#16579)
* transloadit init * [Components] transloadit #16556 Sources - New Assembly Completed - New Assembly Error Actions - Create Assembly - Cancel Assembly - Get Assembly Status * pnpm update * some adjusts * Update components/transloadit/transloadit.app.mjs Co-authored-by: michelle0927 <[email protected]> * Update components/transloadit/transloadit.app.mjs Co-authored-by: michelle0927 <[email protected]> --------- Co-authored-by: michelle0927 <[email protected]>
1 parent 2b7246a commit 0c5ad79

File tree

13 files changed

+537
-15
lines changed

13 files changed

+537
-15
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import transloadit from "../../transloadit.app.mjs";
2+
3+
export default {
4+
key: "transloadit-cancel-assembly",
5+
name: "Cancel Assembly",
6+
description: "Cancel a running assembly by its assembly ID. Useful for aborting processing jobs that are no longer needed. [See the documentation](https://transloadit.com/docs/api/assemblies-assembly-id-delete/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
transloadit,
11+
assemblyId: {
12+
propDefinition: [
13+
transloadit,
14+
"assemblyId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.transloadit.cancelAssembly(this.assemblyId);
20+
21+
$.export("$summary", `Successfully canceled assembly with ID ${this.assemblyId}`);
22+
return response;
23+
},
24+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import transloadit from "../../transloadit.app.mjs";
4+
5+
export default {
6+
key: "transloadit-create-assembly",
7+
name: "Create Assembly",
8+
description: "Create a new assembly to process files using a specified template and steps. [See the documentation](https://transloadit.com/docs/api/assemblies-post/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
transloadit,
13+
info: {
14+
type: "alert",
15+
alertType: "info",
16+
content: "Note: By default, the `steps` parameter allows you to override Template Steps at runtime. However, if `Allow Steps Override` is set to `false`, then steps and `Template Id` become mutually exclusive. In this case, you can only supply one of these parameters. See [Overruling Templates at Runtime](https://transloadit.com/docs/topics/templates/#overruling-templates-at-runtime).",
17+
},
18+
allowStepsOverride: {
19+
type: "boolean",
20+
label: "Allow Steps Override",
21+
description: "Set this to `false` to disallow [Overruling Templates at Runtime](https://transloadit.com/docs/topics/templates/#overruling-templates-at-runtime). If you set this to `false` then `Template Id` and `Steps` will be mutually exclusive and you may only supply one of those parameters. Recommended when deploying Transloadit in untrusted environments. This makes sense to set as part of a Template, rather than on the Assembly itself when creating it.",
22+
default: true,
23+
},
24+
templateId: {
25+
propDefinition: [
26+
transloadit,
27+
"templateId",
28+
],
29+
optional: true,
30+
},
31+
steps: {
32+
type: "object",
33+
label: "Steps",
34+
description: "Assembly Instructions comprise all the Steps executed on uploaded/imported files by the Transloadit back-end during file conversion or encoding. [See the documentation](https://transloadit.com/docs/topics/assembly-instructions/) for more information.",
35+
optional: true,
36+
},
37+
notifyUrl: {
38+
type: "string",
39+
label: "Notify URL",
40+
description: "Transloadit can send a Pingback to your server when the Assembly is completed. We'll send the Assembly status in a form url-encoded JSON string inside of a `transloadit` field in a multipart POST request to the URL supplied here.",
41+
optional: true,
42+
},
43+
quiet: {
44+
type: "boolean",
45+
label: "Quiet",
46+
description: "Set this to `true` to reduce the response from an Assembly POST request to only the necessary fields. This prevents any potentially confidential information being leaked to the end user who is making the Assembly request. A successful Assembly will only include the `ok` and `assembly_id` fields. An erroneous Assembly will only include the `error`, `http_code`, `message` and `assembly_id` fields. The full Assembly Status will then still be sent to the `Notify URL` if one was specified.",
47+
optional: true,
48+
},
49+
},
50+
async run({ $ }) {
51+
if (!this.allowStepsOverride && this.templateId && this.steps) {
52+
throw new ConfigurationError("Either 'templateId' or 'steps' must be provided, not both.");
53+
}
54+
if (!this.templateId && !this.steps) {
55+
throw new ConfigurationError("Either 'templateId' or 'steps' must be provided.");
56+
}
57+
58+
try {
59+
const response = await this.transloadit.createAssembly({
60+
params: {
61+
template_id: this.templateId,
62+
steps: parseObject(this.steps),
63+
notify_url: this.notifyUrl,
64+
allow_steps_override: this.allowStepsOverride,
65+
quiet: this.quiet,
66+
},
67+
});
68+
69+
if (response.results.resize) {
70+
$.export("$summary", `Assembly created successfully with ID ${response.assembly_id}`);
71+
} else {
72+
$.export("$summary", "The Assembly didn't produce any output. Make sure you used a valid image file");
73+
}
74+
75+
return response;
76+
} catch (err) {
77+
let message = `Unable to process Assembly. ${err}`;
78+
if (err.assemblyId) {
79+
message += `More info: https://transloadit.com/assemblies/${err.assemblyId}`;
80+
}
81+
throw new ConfigurationError(message);
82+
}
83+
},
84+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import transloadit from "../../transloadit.app.mjs";
2+
3+
export default {
4+
key: "transloadit-get-assembly-status",
5+
name: "Get Assembly Status",
6+
description: "Retrieve the current status and results of an existing assembly. [See the documentation](https://transloadit.com/docs/api/assemblies-assembly-id-get/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
transloadit,
11+
assemblyId: {
12+
propDefinition: [
13+
transloadit,
14+
"assemblyId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.transloadit.getAssemblyStatus(this.assemblyId);
20+
$.export("$summary", `Successfully retrieved assembly status for ${this.assemblyId}`);
21+
return response;
22+
},
23+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LIMIT = 5000;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/transloadit/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/transloadit",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Transloadit Components",
55
"main": "transloadit.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"transloadit": "^3.0.2"
1418
}
15-
}
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import transloadit from "../../transloadit.app.mjs";
3+
4+
export default {
5+
props: {
6+
transloadit,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getLastDate() {
17+
return this.db.get("lastDate") || "1970-01-01 00:00:00";
18+
},
19+
_setLastDate(lastDate) {
20+
this.db.set("lastDate", lastDate);
21+
},
22+
async emitEvent(maxResults = false) {
23+
const lastDate = this._getLastDate();
24+
25+
const response = this.transloadit.paginate({
26+
fn: this.transloadit.listAssemblies,
27+
params: {
28+
fromdate: lastDate,
29+
type: this.getType(),
30+
},
31+
maxResults,
32+
});
33+
34+
let responseArray = [];
35+
for await (const item of response) {
36+
if (Date.parse(item.created) <= Date.parse(lastDate)) break;
37+
responseArray.push(item);
38+
}
39+
40+
if (responseArray.length) {
41+
this._setLastDate(responseArray[0].created);
42+
}
43+
44+
for (const item of responseArray.reverse()) {
45+
this.$emit(item, {
46+
id: item.id,
47+
summary: this.getSummary(item),
48+
ts: item.created_ts,
49+
});
50+
}
51+
},
52+
},
53+
hooks: {
54+
async deploy() {
55+
await this.emitEvent(25);
56+
},
57+
},
58+
async run() {
59+
await this.emitEvent();
60+
},
61+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "transloadit-new-assembly-completed",
7+
name: "New Assembly Completed",
8+
description: "Emit new event when a Transloadit assembly finishes processing. [See the documentation](https://transloadit.com/docs/api/assemblies-get/)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getType() {
15+
return "completed";
16+
},
17+
getSummary(item) {
18+
return `New assembly completed with ID: ${item.id}`;
19+
},
20+
},
21+
sampleEmit,
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
"id": "195c60855fda4fb9a3b2f848374ca4e1",
3+
"parent_id": null,
4+
"account_id": "7c91e50845204dea9ed4791f7d8440f1",
5+
"template_id": "40b0efc6b37f432dbe9485017b52a7b7",
6+
"instance": "tam.transloadit.com",
7+
"notify_url": null,
8+
"redirect_url": null,
9+
"files": "[\"Saturn as seen from the Cas....jpg\"]",
10+
"region": "eu-west-1",
11+
"warning_count": 0,
12+
"execution_duration": 2.337,
13+
"execution_start": "2023-02-11T16:04:24.000Z",
14+
"ok": "ASSEMBLY_COMPLETED",
15+
"error": null,
16+
"created": "2023-02-11T16:04:22.000Z",
17+
"created_ts": 1676131462,
18+
"template_name": "my-template-1676131432311"
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "transloadit-new-assembly-error",
7+
name: "New Assembly Failed",
8+
description: "Emit new event when a failed occurs during assembly processing. [See the documentation](https://transloadit.com/docs/api/assemblies-get/)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getType() {
15+
return "failed";
16+
},
17+
getSummary(item) {
18+
return `New assembly failed with ID: ${item.id}`;
19+
},
20+
},
21+
sampleEmit,
22+
};

0 commit comments

Comments
 (0)