Transloadit

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:

  1. A value or job variable, such as ${file.mime}
  2. One of the following operators: ==, ===, <, >, <=, >=, !=, !==, regex, !regex
  3. 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"]]

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_meta

    Record<string, boolean> | boolean

    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": true in this object to extract if the image contains transparent parts and "dominant_colors": true to 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": true to get a single value representing the mean average volume of the audio file.

    You can also set this to false to skip metadata extraction and speed up transcoding.

  • result

    boolean (default: false)

    Whether the results of this Step should be present in the Assembly Status JSON

  • queue

    "batch"

    Setting 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_accept

    boolean (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_accept parameter set to true 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.

  • use

    string | Array<string> | Array<object> | object

    Specifies 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"
        ]
      }
      
  • accepts

    string | Array<[, , ]>

    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_type parameter is set to "and", then all requirements must match for the file to be accepted.

    If accepts and declines are both provided, the requirements in accepts will be evaluated first, before the conditions in declines.

  • declines

    string | Array<[, , ]>

    Files that match at least one requirement will be declined, or accepted otherwise. If the value is null or an empty array, no files will be declined. Example:

    [["${file.size}",">","1024"]].

    If the condition_type parameter is set to "and", then all requirements must match for the file to be declined.

    If accepts and declines are both provided, the requirements in accepts will be evaluated first, before the conditions in declines.

  • condition_type

    "and" | "or" (default: "or")

    Specifies the condition type according to which the members of the accepts or declines 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 to true.

Demos

Related blog posts