Filter files
🤖/file/filter directs files to different encoding Steps based on your conditions.
Think of this Robot as an if/else
condition for building advanced file conversion
workflows. With it, you can filter and direct certain uploaded files depending on their metadata.
The Robot has two modes of operation:
- Constructing conditions out of arrays with 3 members each. For example,
["${file.size}", "<=", "720"]
- Writing conditions in JavaScript. For example,
${file.size <= 720}
. See also Dynamic Evaluation.
Passing JavaScript allows you to implement logic as complex as you wish, however it’s 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), match against ""
(an empty
string) instead. We’ll support proper matching against null
in the future, but we cannot easily do
so 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. See also Dynamic Evaluation for more details on allowed syntax and behavior.
Available job variables
Note: Conditions on properties that a file doesn’t have will be ignored. For example, an image
doesn’t 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.
-
${assembly.region}
The AWS region where the Assembly is being processed. You could use this to import files from a bucket in the same region, to reduce data transfer costs and latencies.
-
${assembly.parent_id}
The ID of the parent Assembly when replaying that parent Assembly.
-
${date.now}
The current date and time represented as the number of milliseconds elapsed since the UNIX epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
-
${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.user_meta}
Tus uploads, which you use to upload to Transloadit, can carry meta data values. All the extra values sent along will end up in
user_meta
. This allows you to do things dynamically per file instead of per assembly withfields
. -
${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 in bytes.
-
${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:
{ "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. |
|
Usage example
Reject files that are larger than 20 MB:
{
"steps": {
"filtered": {
"robot": "/file/filter",
"use": ":original",
"declines": [["${file.size}", ">", "20971520"]],
"error_on_decline": true,
"error_msg": "File size must not exceed 20 MB"
}
}
}
Parameters
-
use
String / Array of Strings / Object requiredSpecifies 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. -
-
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
- Automatically decompress uploaded archives
- Automatically filter files to separate encoding steps
- Automatically reject files containing copyright
- Burn subtitles into a video
- Encode letterboxing onto your videos
- Extract a preview from a video
- Extract audio from video files
- Filter out all audio files with a bit rate lower than 64K
- 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
- Filter out files that are smaller than 1KB
- Filter out portrait-oriented images
- Filter out videos that are larger than 20MB or longer than 5 minutes
- Merge an audio file with a video file, keeping the shortest input stream duration intact
- Only resize larger images when resizing files
- Properly preserve transparency across image types
- Reject videos that do not have an audio track
- Resize and apply transparency, based on a clipping path inside an image
- Rotate an image to portrait mode if it's horizontal
Related blog posts
- Launch of new /file/filter Robot for file filtering December 6, 2011
- Introducing new Robots & features for file handling March 30, 2012
- New jQuery SDK version 2.1.0 released! August 8, 2013
- jQuery SDK 2.4.0: key fixes for better stability March 18, 2014
- Enhancing jQuery SDK with tests and a critical patch October 23, 2014
- Kicking Transloadit into gear for the new year February 1, 2015
- Major performance enhancements for faster Assemblies March 4, 2015
- Introducing our new virus scanning Robot for safer uploads July 14, 2015
- New pricing model for future Transloadit customers February 7, 2018
- Transloadit launches Turbo Mode for faster video encoding November 15, 2018
- Efficient Dropbox to SFTP file transfer with optimization January 28, 2019
- Tutorial: file filtering & virus scanning with Transloadit February 6, 2019
- Tech preview: new AI Robots for enhanced media processing February 17, 2020
- Celebrating transloadit’s 2021 milestones and progress January 31, 2022
- Styling subtitles with Transloadit: 3 creative ways May 17, 2023
- Major performance improvements for audio and video concatenation March 1, 2024
- How to check images for copyright using Transloadit July 7, 2024
- Use Transloadit to automatically filter NSFW images July 17, 2024