
Filter files
🤖/file/filter directs files to different encoding Steps based on your conditions.
Think of this Robot as your if/else checks to construct advanced file conversion workflows and to not convert certain files that your users uploaded depending on their metadata.
The Robot has two modes of operation:
- Construct conditions out of
Arrays
with 3 members each. Example:["${file.size}", "<=", "720"]
- Write conditions in
JavaScript
. Example:${file.size <= 720}
. See also Dynamic Evaluation.
Passing JavaScript
allows you to implement logic as complex as you wish,
however is slower than combining Arrays
of conditions, and will be charged
for per invocation via 🤖/script/run.
Conditions as arrays
The accepts
and declines
parameters can each be set to an array of arrays with three members:
- A value or job variable, such as
${file.mime}
- One of the following operators:
==, ===, <, >, <=, >=, !=, !==, regex, !regex
- A value or job variable, such as
50
or"foo"
Examples:
[["${file.meta.width}", ">", "${file.meta.height}"]]
[["${file.size}", "<=", "720"]]
[["720", ">=", "${file.size}"]]
[["${file.mime}", "regex", "image"]]
Warning: If you would like to match against a null
value or a value that is not present (like an audio file does not have a video_codec
property in its metadata), please match against ""
(an empty string) instead. We'll support proper matching against null
in the future, but we cannot easily right now without breaking backwards compatibility.
Conditions as JavaScript
The accepts
and declines
parameters can each be set to Strings
of
JavaScript
, which return a Boolean
value.
Examples:
${file.meta.width > file.meta.height}
${file.size <= 720}
${/image/.test(file.mime)}
${Math.max(file.meta.width, file.meta.height) > 100}
As indicated, we charge for this via 🤖/script/run. Also see Dynamic Evaluation for more details on allowed syntax and behavior.
Available job variables
Note: Conditions on properties that a file does not have will be ignored. For example, an image does not have ${file.meta.bitrate}
. Also, note that since ${file.width}
will be ignored, use ${file.meta.width}
instead.
-
${assembly.id}
The ID of the Assembly representing the current upload, which is a UUIDv4 without dashes. -
${unique_prefix}
A unique 33-character prefix used to avoid file name collisions, such as"f2/d3eeeb67479f11f8b091b04f6181ad"
.Please notice the
/
in the prefix. If you use${unique_prefix}
in thepath
parameter of 🤖/s3/store for example, then it will create sub-directories in your S3 bucket. This may or may not be desired. Use${file.id}
if you require a unique prefix without slashes. -
${unique_original_prefix}
This is similar to${unique_prefix}
, with the exception that two different encoding results of the same uploaded file (the original file) will have the same prefix value here. -
${previous_step.name}
The name of the previous Step that produced the current file. -
${file.id}
The ID of the file being processed, which is a UUIDv4 without dashes. -
${file.original_id}
The ID of the original file that a certain file derives from. For example, if you use an import robot to import files and then encode them somehow, the encoding result files will have a${file.original_id}
that matches the${file.id}
of the imported file. -
${file.original_name}
The name of the original file (including file extension) that a certain file derives from. For example, if you use an import robot to import files and then encode them somehow, the encoding result files will have a${file.original_name}
that matches the${file.name}
of the imported file. -
${file.original_basename}
The basename of the original file that a certain file derives from. For example, if you use an import robot to import files and then encode them somehow, the encoding result files will have a${file.original_basename}
that matches the${file.basename}
of the imported file. -
${file.original_path}
The import path of the original file that a certain file derives from. All of our import robots set${file.original_path}
accordingly.For example, if you use 🤖/s3/import to import files from Amazon S3, the imported files, as well a all files that are derived from them, will have a
file.original_path
that equals the path to the file on S3, but without the filename. So if the S3 path was"path/to/file.txt"
, thenfile.original_path
will be"/path/to/"
. If the path was"/a.txt"
,${file.original_path}
will be"/"
.file.original_path
will always have sufficient slashes in order for you to safely use it in thepath
parameter of your export step, like this:"path": "${file.original_path}${file.name}"
. This is handy if you want to import files from, for example, S3, convert them somehow and store them again on S3 in the same (or similar) file structure. -
${file.name}
The name of the file being processed, including the file extension. -
${file.url_name}
The slugged name of the file.Any characters other than
A-Z a-z 0-9 -_.
are replaced with underscores, and spaces are replaced with dashes. This includes the file extension as well.Note that if you have two files
ッッ.jpg
andチチ.jpg
, they will both be called__.jpg
. So you'll want to take extra care to only use${file.url_name}
in conjunction with${unique_prefix}
or${file.md5hash}
. -
${file.basename}
The name of the file being processed, without the file extension. -
${file.url_basename}
The slugged basename of the file (the file name without the file extension).Any characters other than
A-Z a-z 0-9 -_.
are replaced with underscores, and spaces are replaced with dashes.Note that if you have two files
ッッ.jpg
andチチ.jpg
they will both be called__.jpg
. So you'll want to take extra care to only use${file.url_basename}
in conjunction with${unique_prefix}
or${file.md5hash}
. -
${file.ext}
The file extension. -
${file.size}
The file size. -
${file.mime}
The file's mime type. -
${file.md5hash}
The file's MD5 hash. This is a hash over the file's contents, not only over the file's name. -
${file.*}
Any file property available in the final results array, such as${file.meta.width}
. Not all meta keys are available for all file types. -
${fields.*}
The fields submitted together with the upload.For example, in the case of a Form submission where Uppy was set to allow
fields: ['myvar']
, and the form had a tag like<input type="hidden" name="myvar" value="1" />
,${fields.myvar}
would contain a value of1
.Alternatively, fields could also be populated programmatically like so:
json { "steps": { "store": { "use": "encoded", "robot": "/s3/store", "credentials": "YOUR_S3_CREDENTIALS_NAME", "path": "${assembly.id}/${fields.subdir}/356" } }, "fields": { "subdir": "bar" } }
In the case of a conflict, variables derived from form fields take precedence over those derived from the
fields
key.
Available operators for array conditions
Operator | Description | Example |
---|---|---|
|
Equals without type check |
|
|
Strict equals with type check |
|
|
Less than |
|
|
Greater than |
|
|
Less or equal |
|
|
Greater or equal |
|
|
Simple inequality check without type check |
|
|
Strict inequality check with type check |
|
|
Case-insensitive regular expression based on RE2 |
|
|
Case-insensitive regular expression based on RE2 |
|
|
Check if the right element is included in the array, which is represented by the left element |
|
|
Check if the right element is not included in the array, which is represented by the left element |
|
|
Check if the left element is an empty array, an object without properties, an empty string, the number zero or the boolean false. Leave the third element of the array to be an empty string. It won't be evaluated. |
|
|
Check if the left element is an array with members, an object with at least one property, a non-empty string, a number that does not equal zero or the boolean true. Leave the third element of the array to be an empty string. It won't be evaluated. |
|
Parameters
-
use
String / Array of Strings / ObjectrequiredSpecifies which Step(s) to use as input.
-
You can pick any names for Steps except
":original"
(reserved for user uploads handled by Transloadit) -
You can provide several Steps as input with arrays:
"use": [ ":original", "encoded", "resized" ]
💡 That’s likely all you need to know about
use
, but you can view advanced use cases:› Advanced use cases
-
Step bundling. Some Robots can gather several Step results for a single invocation. For example, 🤖/file/compress would normally create one archive for each file passed to it. If you'd set
bundle_steps
to true, however, it will create one archive containing all the result files from all Steps you give it. To enable bundling, provide an object like the one below to theuse
parameter:"use": { "steps": [ ":original", "encoded", "resized" ], "bundle_steps": true }
This is also a crucial parameter for 🤖/video/adaptive, otherwise you'll generate 1 playlist for each viewing quality.
Keep in mind that all input Steps must be present in your Template. If one of them is missing (for instance it is rejected by a filter), no result is generated because the Robot waits indefinitely for all input Steps to be finished.Here’s a demo that showcases Step bundling.
-
Group by original. Sticking with 🤖/file/compress example, you can set
group_by_original
totrue
, in order to create a separate archive for each of your uploaded or imported files, instead of creating one archive containing all originals (or one per resulting file). This is important for for 🤖/media/playlist where you'd typically set:"use": { "steps": [ "segmented" ], "bundle_steps": true, "group_by_original": true }
-
Fields. You can be more discriminatory by only using files that match a field name by setting the
fields
property. When this array is specified, the corresponding Step will only be executed for files submitted through one of the given field names, which correspond with the strings in thename
attribute of the HTML file input field tag for instance. When using a back-end SDK, it corresponds withmyFieldName1
in e.g.:$transloadit->addFile('myFieldName1', './chameleon.jpg')
.This parameter is set to
true
by default, meaning all fields are accepted.Example:
"use": { "steps": [ ":original" ], "fields": [ "myFieldName1" ] }
-
Use as. Sometimes Robots take several inputs. For instance, 🤖/video/merge can create a slideshow from audio and images. You can map different Steps to the appropriate inputs.
Example:
"use": { "steps": [ { "name": "audio_encoded", "as": "audio" }, { "name": "images_resized", "as": "image" } ] }
Sometimes the ordering is important, for instance, with our concat Robots. In these cases, you can add an index that starts at 1. You can also optionally filter by the multipart field name. Like in this example, where all files are coming from the same source (end-user uploads), but with different
<input>
names:Example:
"use": { "steps": [ { "name": ":original", "fields": "myFirstVideo", "as": "video_1" }, { "name": ":original", "fields": "mySecondVideo", "as": "video_2" }, { "name": ":original", "fields": "myThirdVideo", "as": "video_3" } ] }
For times when it is not apparent where we should put the file, you can use Assembly Variables to be specific. For instance, you may want to pass a text file to 🤖/image/resize to burn the text in an image, but you are burning multiple texts, so where do we put the text file? We specify it via
${use.text_1}
, to indicate the first text file that was passed.Example:
"watermarked": { "robot": "/image/resize", "use" : { "steps": [ { "name": "resized", "as": "base" }, { "name": "transcribed", "as": "text" }, ], }, "text": [ { "text" : "Hi there", "valign": "top", "align" : "left", }, { "text" : "From the 'transcribed' Step: ${use.text_1}", "valign" : "bottom", "align" : "right", "x_offset": 16, "y_offset": -10, } ] }
-
-
accepts
Array of Arrays / String ⋅ default:[]
Files that match at least one requirement will be accepted, or declined otherwise. If the array is empty, all files will be accepted. Example:
[["${file.mime}", "==", "image/gif"]]
.If the
condition_type
parameter is set to"and"
, then all requirements must match for the file to be accepted. -
declines
Array of Arrays / String ⋅ default:[]
Files that match at least one requirement will be declined, or accepted otherwise. Example:
[["${file.size}",">","1024"]]
.If the
condition_type
parameter is set to"and"
, then all requirements must match for the file to be declined. -
condition_type
String ⋅ default:"or"
Specifies the condition type according to which the members of the
accepts
ordeclines
arrays should be evaluated. Can be"or"
or"and"
. -
error_on_decline
Boolean ⋅ default:false
If this is set to
true
and one or more files are declined, the Assembly will be stopped and marked with an error. -
error_msg
String ⋅ default:"One of your files was declined"
The error message shown to your users (such as by Uppy) when a file is declined and
error_on_decline
is set totrue
.
Demos
- Extract a preview from a video
- Extract audio from video files
- Filter out videos that are larger than 20MB or longer than 5 minutes
- Rotate an image to portrait mode if it's horizontal
- Filter out all audio files with a bit rate lower than 64K
- Filter out files that are smaller than 1KB
- Filter out all image files without a full HD resolution
- Filter out anything other than image files
- Filter out anything other than video or image files
- Only resize larger images when resizing files
- Automatically reject files containing copyright
- Filter out portrait-oriented images
- Reject videos that do not have an audio track
- Import your Dropbox files to Transloadit for encoding
- Properly preserve transparency across image types
- Burn subtitles into a video
- Encode letterboxing onto your videos
- Merge an audio file with a video file, keeping the shortest input stream duration intact
Related blog posts
- New /file/filter Robot Released December 6, 2011
- Two New Robots and More Features March 30, 2012
- New jQuery SDK Version 2.1.0 Released! August 8, 2013
- Announcing Version 2.4.0 of Our jQuery SDK March 18, 2014
- Post Mortem: jQuery SDK Problems October 23, 2014
- Kicking Transloadit Into Gear for the New Year February 1, 2015
- Performance Upgrades March 4, 2015
- New Robot for Virus Detection July 21, 2015
- Raising prices (for new customers) February 7, 2018
- Launching Turbo Mode in public beta November 15, 2018
- Re-loadit: the /dropbox/import Robot January 28, 2019
- How to filter input files using the /file/filter and /file/virusscan Robots February 6, 2019
- 🧠 Tech Preview of our new AI bots February 17, 2020
- Transloadit Milestones of 2021 January 31, 2022