Skip to content

Commit 9c211ba

Browse files
committed
[Components] transloadit #16556
Sources - New Assembly Completed - New Assembly Error Actions - Create Assembly - Cancel Assembly - Get Assembly Status
1 parent bde6e0d commit 9c211ba

File tree

15 files changed

+305
-390
lines changed

15 files changed

+305
-390
lines changed

common/constants.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LIMIT = 5000;

components/transloadit/actions/cancel-assembly/cancel-assembly.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "transloadit-cancel-assembly",
55
name: "Cancel Assembly",
66
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.{{ts}}",
7+
version: "0.0.1",
88
type: "action",
99
props: {
1010
transloadit,
@@ -16,11 +16,9 @@ export default {
1616
},
1717
},
1818
async run({ $ }) {
19-
const response = await this.transloadit.cancelAssembly({
20-
assemblyId: this.assemblyId,
21-
});
19+
const response = await this.transloadit.cancelAssembly(this.assemblyId);
2220

23-
$.export("$summary", `Successfully canceled assembly with ID ${response.assembly_id}`);
21+
$.export("$summary", `Successfully canceled assembly with ID ${this.assemblyId}`);
2422
return response;
2523
},
2624
};
Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
13
import transloadit from "../../transloadit.app.mjs";
2-
import { axios } from "@pipedream/platform";
34

45
export default {
56
key: "transloadit-create-assembly",
67
name: "Create Assembly",
78
description: "Create a new assembly to process files using a specified template and steps. [See the documentation](https://transloadit.com/docs/api/assemblies-post/)",
8-
version: "0.0.{{ts}}",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
1112
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+
},
1224
templateId: {
1325
propDefinition: [
1426
transloadit,
@@ -17,41 +29,57 @@ export default {
1729
optional: true,
1830
},
1931
steps: {
20-
propDefinition: [
21-
transloadit,
22-
"steps",
23-
],
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.",
2435
optional: true,
2536
},
26-
files: {
27-
propDefinition: [
28-
transloadit,
29-
"files",
30-
],
31-
},
3237
notifyUrl: {
33-
propDefinition: [
34-
transloadit,
35-
"notifyUrl",
36-
],
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.",
3747
optional: true,
3848
},
3949
},
4050
async run({ $ }) {
51+
if (!this.allowStepsOverride && this.templateId && this.steps) {
52+
throw new Error("Either 'templateId' or 'steps' must be provided, not both.");
53+
}
4154
if (!this.templateId && !this.steps) {
4255
throw new Error("Either 'templateId' or 'steps' must be provided.");
4356
}
4457

45-
const response = await this.transloadit.createAssembly({
46-
templateId: this.templateId,
47-
steps: this.steps
48-
? JSON.parse(this.steps)
49-
: undefined,
50-
files: this.files,
51-
notifyUrl: this.notifyUrl,
52-
});
58+
try {
59+
const response = await this.transloadit.createAssembly({
60+
params: {
61+
template_id: this.templateId,
62+
steps: parseObject(this.steps),
63+
fields: this.fields,
64+
notify_url: this.notifyUrl,
65+
allow_steps_override: this.allowStepsOverride,
66+
quiet: this.quiet,
67+
},
68+
});
5369

54-
$.export("$summary", `Assembly created successfully with ID ${response.assembly_id}`);
55-
return response;
70+
if (response.results.resize) {
71+
$.export("$summary", `Assembly created successfully with ID ${response.assembly_id}`);
72+
} else {
73+
$.export("$summary", "The Assembly didn't produce any output. Make sure you used a valid image file");
74+
}
75+
76+
return response;
77+
} catch (err) {
78+
let message = `Unable to process Assembly. ${err}`;
79+
if (err.assemblyId) {
80+
message += `More info: https://transloadit.com/assemblies/${err.assemblyId}`;
81+
}
82+
throw new ConfigurationError(message);
83+
}
5684
},
5785
};

components/transloadit/actions/get-assembly-status/get-assembly-status.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "transloadit-get-assembly-status",
55
name: "Get Assembly Status",
66
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.{{ts}}",
7+
version: "0.0.1",
88
type: "action",
99
props: {
1010
transloadit,
@@ -16,9 +16,7 @@ export default {
1616
},
1717
},
1818
async run({ $ }) {
19-
const response = await this.transloadit.getAssemblyStatus({
20-
assemblyId: this.assemblyId,
21-
});
19+
const response = await this.transloadit.getAssemblyStatus(this.assemblyId);
2220
$.export("$summary", `Successfully retrieved assembly status for ${this.assemblyId}`);
2321
return response;
2422
},
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: 5 additions & 1 deletion
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
}
1519
}
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+
};

components/transloadit/sources/new-assembly-completed-instant/new-assembly-completed-instant.mjs

Lines changed: 0 additions & 83 deletions
This file was deleted.
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+
}

0 commit comments

Comments
 (0)