19 package org.sleuthkit.autopsy.modules.filetypeid;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26 import java.util.stream.Collectors;
27 import org.apache.tika.Tika;
28 import org.apache.tika.io.TikaInputStream;
29 import org.apache.tika.mime.MimeTypes;
46 private static final Tika
tika =
new Tika();
63 TreeSet<String> detectedTypes =
new TreeSet<>((String string1, String string2) -> {
64 int result = String.CASE_INSENSITIVE_ORDER.compare(string1, string2);
66 result = string1.compareTo(string2);
72 for (FileType fileType : CustomFileTypesManager.getInstance().getAutopsyDefinedFileTypes()) {
73 detectedTypes.add(fileType.getMimeType());
75 }
catch (CustomFileTypesManager.CustomFileTypesException ex) {
76 throw new FileTypeDetectorInitException(
"Error loading Autopsy custom file types", ex);
79 for (FileType fileType : CustomFileTypesManager.getInstance().getUserDefinedFileTypes()) {
80 detectedTypes.add(fileType.getMimeType());
82 }
catch (CustomFileTypesManager.CustomFileTypesException ex) {
83 throw new FileTypeDetectorInitException(
"Error loading user custom file types", ex);
96 if (null == tikaDetectedTypes) {
97 tikaDetectedTypes =
org.apache.tika.mime.MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes()
98 .stream().filter(t -> !t.hasParameters()).map(s -> s.toString().replace(
"tika-",
"")).collect(Collectors.toCollection(TreeSet::new));
100 return Collections.unmodifiableSortedSet(tikaDetectedTypes);
117 userDefinedFileTypes = CustomFileTypesManager.getInstance().getUserDefinedFileTypes();
118 autopsyDefinedFileTypes = CustomFileTypesManager.getInstance().getAutopsyDefinedFileTypes();
119 }
catch (CustomFileTypesManager.CustomFileTypesException ex) {
120 throw new FileTypeDetectorInitException(
"Error loading custom file types", ex);
148 for (FileType fileType : customTypes) {
149 if (fileType.getMimeType().equals(mimeType)) {
182 String mimeType = file.getMIMEType();
183 if (null != mimeType) {
195 if (!file.isFile() || file.getSize() <= 0
196 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
197 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
198 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.VIRTUAL_DIR)
199 || ((file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.SLACK) && file.getSize() <
SLACK_FILE_THRESHOLD)) {
200 mimeType = MimeTypes.OCTET_STREAM;
207 if (null == mimeType) {
215 if (null == mimeType) {
223 if (null == mimeType) {
224 ReadContentInputStream stream =
new ReadContentInputStream(file);
226 try (TikaInputStream tikaInputStream = TikaInputStream.get(stream)) {
227 String tikaType = tika.detect(tikaInputStream, file.getName());
232 mimeType = tikaType.replace(
"tika-",
"");
238 }
catch (Exception ignored) {
246 mimeType = MimeTypes.OCTET_STREAM;
253 file.setMIMEType(mimeType);
266 int indexOfSemicolon = mimeType.indexOf(
';');
267 if (indexOfSemicolon != -1) {
268 return mimeType.substring(0, indexOfSemicolon).trim();
282 String retValue = null;
284 for (FileType fileType : userDefinedFileTypes) {
285 if (fileType.matches(file)) {
286 retValue = fileType.getMimeType();
301 for (FileType fileType : autopsyDefinedFileTypes) {
302 if (fileType.matches(file)) {
303 return fileType.getMimeType();
335 super(message, throwable);
350 List<String> customFileTypes =
new ArrayList<>();
351 userDefinedFileTypes.forEach((fileType) -> {
352 customFileTypes.add(fileType.getMimeType());
354 autopsyDefinedFileTypes.forEach((fileType) -> {
355 customFileTypes.add(fileType.getMimeType());
357 return customFileTypes;
377 file.setMIMEType(fileType);
399 public String
getFileType(AbstractFile file)
throws TskCoreException {
401 file.setMIMEType(fileType);
419 public String
detect(AbstractFile file)
throws TskCoreException {
String removeOptionalParameter(String mimeType)
static final long serialVersionUID
final List< FileType > userDefinedFileTypes
static final int SLACK_FILE_THRESHOLD
boolean isDetectable(String mimeType)
String detectUserDefinedType(AbstractFile file)
String getMIMEType(AbstractFile file)
boolean isDetectableAsCustomType(List< FileType > customTypes, String mimeType)
final List< FileType > autopsyDefinedFileTypes
static SortedSet< String > tikaDetectedTypes
String detect(AbstractFile file)
String detectAutopsyDefinedType(AbstractFile file)
synchronized static Logger getLogger(String name)
static final Logger logger
List< String > getUserDefinedTypes()
static SortedSet< String > getTikaDetectedTypes()
String getFileType(AbstractFile file)
static synchronized SortedSet< String > getDetectedTypes()
boolean isDetectableByTika(String mimeType)
String detectAndPostToBlackboard(AbstractFile file)