Flag of Ukraine

Filter files

The /file/filter Robot 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:

  1. Construct conditions out of Arrays with 3 members each. Example: ["${file.size}", "<=", "720"]
  2. 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 the /script/run Robot.

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"]]

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 the /script/run Robot. 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 the path parameter of the /s3/store Robot 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 the /s3/import Robot 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", then file.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 the path 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 of 1.

    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

["${file.ext}", "==", "jpg"]

===

Strict equals with type check

["${file.ext}", "===", "jpg"]

<

Less than

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

>

Greater than

["${file.meta.width}", ">", "${file.meta.height}"]

<=

Less or equal

["${file.meta.audio_bitrate}", "<=", "256000"]

>=

Greater or equal

["${file.meta.audio_samplerate}", ">=", "44100"]

!=

Simple inequality check without type check

["${file.ext}", "!=", "jpg"]

!==

Strict inequality check with type check

["${file.ext}", "!==", "jpg"]

regex

Case-insensitive regular expression based on RE2 .match()

["${file.mime}", "regex", "image"]

!regex

Case-insensitive regular expression based on RE2 !.match()

["${file.mime}", "!regex", "image"]

includes

Check if the right element is included in the array, which is represented by the left element

['${file.meta.descriptions}', 'includes', 'Bridge']

!includes

Check if the right element is not included in the array, which is represented by the left element

['${file.meta.descriptions}', '!includes', 'Bridge']

empty

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.

['${file.meta.descriptions}', 'empty', '']

!empty

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.

['${file.meta.descriptions}', '!empty', '']

Parameters

  • use

    String / Array of Strings / Object required

    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"
      ]
      

    :bulb: 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, the /file/compress Robot 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 the use parameter:

      "use": {
        "steps": [
          ":original",
          "encoded",
          "resized"
        ],
        "bundle_steps": true
      }
      

      This is also a crucial parameter for the /video/adaptive Robot, 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 the /file/compress Robot example, you can set group_by_original to true, 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 the /media/playlist Robot 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 the name attribute of the HTML file input field tag for instance. When using a back-end SDK, it corresponds with myFieldName1 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, the /video/merge Robot 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 the /image/resize Robot 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 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

Uppy
20% off any plan for the Uppy community
Use the UPPY20 code when upgrading.
Sign up
tus
20% off any plan for the tus community
Use the TUS20 code when upgrading.
Sign up
Product Hunt
20% off any plan for Product Hunters
Use the PRH20 code when upgrading.
Sign up