Class FileTypeMagicUtil

  • All Implemented Interfaces:
    com.j256.simplemagic.ContentInfoUtil.ErrorCallBack

    public class FileTypeMagicUtil
    extends Object
    implements com.j256.simplemagic.ContentInfoUtil.ErrorCallBack
    Utility class to guess the mime type of file based on its magic number.
    • Method Detail

      • assertConfigSetFolderLegal

        public static void assertConfigSetFolderLegal​(Path confPath)
                                               throws IOException
        Asserts that an entire configset folder is legal to upload.
        Parameters:
        confPath - the path to the folder
        Throws:
        org.apache.solr.common.SolrException - if an illegal file is found in the folder structure
        IOException
      • guessMimeType

        public String guessMimeType​(Path file)
        Guess the mime type of file based on its magic number.
        Parameters:
        file - file to check
        Returns:
        string with content-type or "application/octet-stream" if unknown
      • guessMimeType

        public String guessMimeType​(byte[] bytes)
        Guess the mime type of file bytes based on its magic number.
        Parameters:
        bytes - the first bytes at start of the file
        Returns:
        string with content-type or "application/octet-stream" if unknown
      • error

        public void error​(String line,
                          String details,
                          Exception e)
        Specified by:
        error in interface com.j256.simplemagic.ContentInfoUtil.ErrorCallBack
      • isFileForbiddenInConfigset

        public static boolean isFileForbiddenInConfigset​(Path file)
        Determine forbidden file type based on magic bytes matching of the file itself. Forbidden types are:
        • application/x-java-applet: java class file
        • application/zip: jar or zip archives
        • application/x-tar: tar archives
        • text/x-shellscript: shell or bash script
        Parameters:
        file - file to check
        Returns:
        true if file is among the forbidden mime-types
      • isFileForbiddenInConfigset

        public static boolean isFileForbiddenInConfigset​(byte[] bytes)
        Determine forbidden file type based on magic bytes matching of the first bytes of the file.
        Parameters:
        bytes - byte array of the file content
        Returns:
        true if file is among the forbidden mime-types