Class CustomMimeTypesFileTypeDetector

java.lang.Object
java.nio.file.spi.FileTypeDetector
dev.langchain4j.internal.CustomMimeTypesFileTypeDetector

public class CustomMimeTypesFileTypeDetector extends FileTypeDetector

Utility class to guess the mime-type of a file from its path or URI.

As JDK's built-in doesn't recognize all possible mime-types in particular for newer file types like YAML, or returns odd mime-types like for the TypeScript .ts extension, this class allows you to define your own extension to mime-type mapping, in addition to providing a few extra ones built-in.

When the mime-type can't be found in those mappings, or from JDK's built-in, it tries, as a fallback, to use URLConnection's capability, but it's slower as it needs to connect to the underlying URL.

  • Constructor Details

    • CustomMimeTypesFileTypeDetector

      public CustomMimeTypesFileTypeDetector()
      Create a file mime-type detector, using the internal default mappings, and fallback to JDK's built-in capabilities, when no suitable mapping is found.
    • CustomMimeTypesFileTypeDetector

      public CustomMimeTypesFileTypeDetector(Map<String,String> customMapping)
      Create a file mime-type detector, using your own custom file extension to mime-type mappings, with a fallback to JDK's built-in capabilities, when no suitable mapping is found
      Parameters:
      customMapping - map of custom file extension to mime-type mapping
  • Method Details

    • probeContentType

      public String probeContentType(Path path)
      Guess the mime-type of a given path.
      Specified by:
      probeContentType in class FileTypeDetector
      Parameters:
      path - the path to the file whose mime-type we want to retrieve.
      Returns:
      a string representing the mime-type of the file denoted by the path parameter, returns null if no mapping was found.
    • probeContentType

      public String probeContentType(String path)
    • probeContentType

      public String probeContentType(URI uri)
      Guess the mime-type of a given URI.
      Parameters:
      uri - the URI to the file whose mime-type we want to retrieve.
      Returns:
      a string representing the mime-type of the file denoted by the URI parameter, returns null if no mapping was found.