Class FileTypes

java.lang.Object
atessera.sensei.util.FileTypes

public final class FileTypes extends Object
Utility class for determining file types and classifying files as binary or text-based.

This class is security-sensitive: misclassifying a binary file as text could lead to binary data leaking into LLM prompts or other text-processing pipelines. Conversely, misclassifying a text file as binary may cause useful content to be skipped.

The classification relies on Apache Tika for MIME type detection based on file content (not extension), which provides resilience against extension-spoofing attacks.

Additionally, this class provides a method to detect hidden files (those whose path contains a dot-prefixed component), which is important for preventing accidental exposure of sensitive configuration files like .git/config, .env, or .ssh/id_rsa.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isBinary(Path path)
    Determines whether the given file is binary by detecting its MIME type via Apache Tika and then classifying that MIME type.
    static boolean
    Classifies a MIME type string as binary or text-based.
    static boolean
    isHidden(Path path)
    Checks whether any component of the given path starts with a dot (.), indicating a hidden file or directory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileTypes

      public FileTypes()
  • Method Details

    • isBinary

      public static boolean isBinary(Path path)
      Determines whether the given file is binary by detecting its MIME type via Apache Tika and then classifying that MIME type.

      Non-regular files (directories, symlinks pointing to directories, device files, etc.) are treated as non-binary. If the MIME type cannot be detected due to an I/O error, the file is conservatively treated as non-binary and a warning is logged.

      This method reads only the file header (magic bytes), not the entire file content, making it efficient for large files.

      Parameters:
      path - the path to the file to inspect; must not be null
      Returns:
      true if the file is a regular file and its detected MIME type is classified as binary, false otherwise
      See Also:
    • isBinaryMimeType

      public static boolean isBinaryMimeType(String rawMime)
      Classifies a MIME type string as binary or text-based.

      The classification rules are as follows:

      • null, blank, or malformed strings (no slash) — non-binary.
      • Any type under the text top-level type — non-binary.
      • image, audio, video, font, model — binary.
      • For application: subtypes ending with +xml, +json, +yaml, or +csv are non-binary. A set of well-known text-based subtypes (e.g. json, xml, javascript, x-sh, x-python, sql, yaml, toml, graphql, and others) are non-binary. All other application subtypes are binary.
      • Any other top-level type — binary.

      Comparison is case-insensitive. Leading and trailing whitespace is stripped.

      Examples of binary MIME types: application/octet-stream, application/pdf, application/zip, application/x-executable, image/png, audio/mpeg.

      Examples of non-binary MIME types: text/plain, text/html, application/json, application/xml, application/javascript, application/x-sh.

      Parameters:
      rawMime - the MIME type string to classify; may be null
      Returns:
      true if the MIME type represents a binary format, false otherwise
    • isHidden

      public static boolean isHidden(Path path)
      Checks whether any component of the given path starts with a dot (.), indicating a hidden file or directory.

      This is important for security: hidden files and directories often contain sensitive configuration data (e.g. .git/config, .env, .ssh/id_rsa, .aws/credentials) that should not be exposed to text-processing pipelines or LLM prompts.

      The check iterates over all path components. Both . (current directory) and .. (parent directory) are considered hidden, as are files like .gitignore and directories like .git.

      Note: this method inspects the path string itself. It does not resolve symlinks or normalize the path. Callers should consider whether normalization is needed before invoking this method.

      Parameters:
      path - the path to inspect; must not be null
      Returns:
      true if any component of the path starts with a dot, false otherwise