
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
50or"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.
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
output_metaRecord<string, boolean> | boolean | Array<string>Allows you to specify a set of metadata that is more expensive on CPU power to calculate, and thus is disabled by default to keep your Assemblies processing fast.
For images, you can add
"has_transparency": truein this object to extract if the image contains transparent parts and"dominant_colors": trueto extract an array of hexadecimal color codes from the image.For videos, you can add the
"colorspace: true"parameter to extract the colorspace of the output video.For audio, you can add
"mean_volume": trueto get a single value representing the mean average volume of the audio file.You can also set this to
falseto skip metadata extraction and speed up transcoding.resultboolean(default:false)Whether the results of this Step should be present in the Assembly Status JSON
queuebatchSetting the queue to 'batch', manually downgrades the priority of jobs for this step to avoid consuming Priority job slots for jobs that don't need zero queue waiting times
force_acceptboolean(default:false)Force a Robot to accept a file type it would have ignored.
By default, Robots ignore files they are not familiar with. 🤖/video/encode, for example, will happily ignore input images.
With the
force_acceptparameter set totrue, you can force Robots to accept all files thrown at them. This will typically lead to errors and should only be used for debugging or combatting edge cases.usestring | Array<string> | Array<object> | objectSpecifies 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" ] }
Tip
That's likely all you need to know about
use, but you can view Advanced use cases.- You can pick any names for Steps except
acceptsnull | string | Array<[string | string | number | null | Array<string | string | number | null>, "=" | "==" | "===" | "<" | ">" | "<=" | ">=" | , string | string | number | null | Array<string | string | number | null>]>Files that match at least one requirement will be accepted, or declined otherwise. If the value is
null, all files will be accepted. If the array is empty, no files will be accepted. Example:[["${file.mime}", "==", "image/gif"]].If the
condition_typeparameter is set to"and", then all requirements must match for the file to be accepted.If
acceptsanddeclinesare both provided, the requirements inacceptswill be evaluated first, before the conditions indeclines.declinesnull | string | Array<[string | string | number | null | Array<string | string | number | null>, "=" | "==" | "===" | "<" | ">" | "<=" | ">=" | , string | string | number | null | Array<string | string | number | null>]>Files that match at least one requirement will be declined, or accepted otherwise. If the value is
nullor an empty array, no files will be declined. Example:[["${file.size}",">","1024"]].If the
condition_typeparameter is set to"and", then all requirements must match for the file to be declined.If
acceptsanddeclinesare both provided, the requirements inacceptswill be evaluated first, before the conditions indeclines.condition_typeand | or(default:"or")Specifies the condition type according to which the members of the
acceptsordeclinesarrays should be evaluated. Can be"or"or"and".error_on_declineboolean(default:false)If this is set to
trueand one or more files are declined, the Assembly will be stopped and marked with an error.error_msgstring(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_declineis set totrue.
Demos
- Service to generate a slideshow from AI-filtered images
- Automatic Nudity Detection Service
- Automatic Image Recognition Service
- Service to automatically filter out large video files
- Service to automatically rotate an image to portrait mode if it's horizontal
- Service to automatically filter files to separate encoding steps
- Service to automatically filter out files smaller than 1KB
- Service to only resize larger images when resizing files
- Service to reject files containing copyright
- Service to preserve transparency across image types
Related blog posts
- Launch of new /file/filter Robot for file filtering
- Introducing new Robots & features for file handling
- jQuery SDK 2.1.0: new features & progressbar redesign
- jQuery SDK 2.4.0: key fixes for better stability
- Enhancing jQuery SDK with tests and a critical patch
- Kicking Transloadit into gear for the new year
- Major performance enhancements for faster Assemblies
- Introducing our new virus scanning Robot for safer uploads
- New pricing model for future Transloadit customers
- Transloadit launches Turbo Mode for faster video encoding
- Efficient Dropbox to SFTP file transfer with optimization
- Tutorial: file filtering & virus scanning with Transloadit
- Tech preview: new AI Robots for enhanced media processing
- Celebrating transloadit’s 2021 milestones and progress
- Styling subtitles with Transloadit: 3 creative ways
- Major performance improvements for audio and video concatenation
- How to check images for copyright using Transloadit
- Use Transloadit to automatically filter NSFW images