Files
The Files action evaluates its options using file generators, temporarily storing the resulting files and making them available via a randomized Mechanic URL.
This action is most useful in concert with mechanic/actions/perform, by which a task may take the resulting file URLs and pass them on to another service. Used by itself, this action can also be useful for quickly testing file generators.

Options

This action accepts a JSON object, whose keys are filenames and whose values are file generators. In this way, many files may be defined and generated by a single Files action.

Result

This action returns an object having the same keys (i.e. filenames) as its input. Each value is an object, having the following properties:
File property
Description
expires_at
An ISO8601 timestamp, specifying when the file will expire
mime_type
The MIME type of the generated file
name
The filename, as given in the original action options
size
The size of the generated file, in bytes
url
The URL at which this file will be available, until it expires

Example

This task generates a variety of files. It then re-invokes itself (via mechanic/actions/perform), sending an email containing links to each of the generated files.
Subscriptions
1
mechanic/user/trigger
2
mechanic/actions/perform
Copied!
Code
1
{% if event.topic == "mechanic/user/trigger" %}
2
{% action "files" %}
3
{
4
"journal.txt": "hello world!",
5
"table.csv": "Title,SKU\nRed T-Shirt,TEE-R",
6
"invoice.pdf": {
7
"pdf": {
8
"html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
9
}
10
},
11
"secure.zip": {
12
"zip": {
13
"password": "opensesame",
14
"files": {
15
"confirmations.txt": "this data is protected with zipcrypto encryption"
16
}
17
}
18
},
19
"external.jpg": {
20
"url": "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg"
21
}
22
}
23
{% endaction %}
24
{% elsif event.topic == "mechanic/actions/perform" and action.type == "files" %}
25
{% capture email_body %}
26
<p>The following file(s) have been generated:</p>
27
<ul>
28
{% for keyval in action.run.result %}
29
{% assign filename = keyval[0] %}
30
{% assign file = keyval[1] %}
31
<li><a href="{{ file.url }}">{{ filename }}</a> ({{ file.size }} bytes)</li>
32
{% endfor %}
33
</ul>
34
<p>-Mechanic</p>
35
{% endcapture %}
36
​
37
{% action "email" %}
38
{
39
40
"subject": {{ action.run.result | size | append: " file(s) generated" | json }},
41
"body": {{ email_body | json }}
42
}
43
{% endaction %}
44
{% endif %}
Copied!
Last modified 7mo ago
Copy link
Edit on GitHub