Package org.apache.solr.util
Class FileTypeMagicUtil
- java.lang.Object
-
- org.apache.solr.util.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.
-
-
Field Summary
Fields Modifier and Type Field Description static FileTypeMagicUtil
INSTANCE
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
assertConfigSetFolderLegal(Path confPath)
Asserts that an entire configset folder is legal to upload.void
error(String line, String details, Exception e)
String
guessMimeType(byte[] bytes)
Guess the mime type of file bytes based on its magic number.String
guessMimeType(Path file)
Guess the mime type of file based on its magic number.static boolean
isFileForbiddenInConfigset(byte[] bytes)
Determine forbidden file type based on magic bytes matching of the first bytes of the file.static boolean
isFileForbiddenInConfigset(Path file)
Determine forbidden file type based on magic bytes matching of the file itself.
-
-
-
Field Detail
-
INSTANCE
public static FileTypeMagicUtil INSTANCE
-
-
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 structureIOException
-
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 interfacecom.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 fileapplication/zip
: jar or zip archivesapplication/x-tar
: tar archivestext/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
-
-