Do not call FileTypeDetector.getFileType outside of ingest

This commit is contained in:
Richard Cordovano 2016-03-16 16:01:47 -04:00
parent c9d95d85bc
commit d3dba717e4
4 changed files with 35 additions and 75 deletions

View File

@ -28,9 +28,7 @@ import java.util.TreeSet;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.TskCoreException;
/** /**
* Video viewer part of the Media View layered pane. Uses different engines * Video viewer part of the Media View layered pane. Uses different engines
@ -133,23 +131,18 @@ public abstract class MediaViewVideoPanel extends JPanel implements FrameCapture
@Override @Override
public boolean isSupported(AbstractFile file) { public boolean isSupported(AbstractFile file) {
/*
* TODO (AUT-2042): Is this the logic we want?
*/
String extension = file.getNameExtension(); String extension = file.getNameExtension();
//TODO: is this what we want, to require both extension and mimetype support?
if (AUDIO_EXTENSIONS.contains("." + extension) || getExtensionsList().contains("." + extension)) { if (AUDIO_EXTENSIONS.contains("." + extension) || getExtensionsList().contains("." + extension)) {
SortedSet<String> mimeTypes = new TreeSet<>(getMimeTypes()); SortedSet<String> mimeTypes = new TreeSet<>(getMimeTypes());
try { String mimeType = file.getMIMEType();
String mimeType = new FileTypeDetector().getFileType(file); if (nonNull(mimeType)) {
if (nonNull(mimeType)) { return mimeTypes.contains(mimeType);
return mimeTypes.contains(mimeType); } else {
} return getExtensionsList().contains("." + extension);
} catch (FileTypeDetector.FileTypeDetectorInitException | TskCoreException ex) {
logger.log(Level.WARNING, "Failed to look up mimetype for " + file.getName() + " using FileTypeDetector. Fallingback on AbstractFile.isMimeType", ex);
if (!mimeTypes.isEmpty() && file.isMimeType(mimeTypes) == AbstractFile.MimeMatchEnum.TRUE) {
return true;
}
} }
return getExtensionsList().contains("." + extension);
} }
return false; return false;
} }

View File

@ -149,8 +149,8 @@ public class ImageUtils {
/** /**
* thread that saves generated thumbnails to disk in the background * thread that saves generated thumbnails to disk in the background
*/ */
private static final Executor imageSaver = private static final Executor imageSaver
Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder() = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
.namingPattern("icon saver-%d").build()); //NOI18N NON-NLS .namingPattern("icon saver-%d").build()); //NOI18N NON-NLS
public static List<String> getSupportedImageExtensions() { public static List<String> getSupportedImageExtensions() {
@ -223,40 +223,19 @@ public class ImageUtils {
} }
/** /**
* Does the image have a GIF mimetype. * Checks the MIME type of a file to determine whether it is a GIF. If the
* MIME type is not known, checks for a "gif" extension.
* *
* @param file * @param file The file to be checked.
* *
* @return true if the given file has a GIF mimetype * @return True or false
*/ */
public static boolean isGIF(AbstractFile file) { public static boolean isGIF(AbstractFile file) {
try { String mimeType = file.getMIMEType();
final FileTypeDetector myFileTypeDetector = getFileTypeDetector(); if (nonNull(mimeType)) {
if (nonNull(myFileTypeDetector)) { return IMAGE_GIF_MIME.equalsIgnoreCase(mimeType);
String fileType = myFileTypeDetector.getFileType(file); } else {
return IMAGE_GIF_MIME.equalsIgnoreCase(fileType); return "gif".equalsIgnoreCase(file.getNameExtension()); //NOI18N
}
} catch (FileTypeDetectorInitException ex) {
LOGGER.log(Level.WARNING, "Failed to initialize FileTypeDetector.", ex); //NOI18N NON-NLS
} catch (TskCoreException ex) {
if (ex.getMessage().contains("An SQLException was provoked by the following failure: java.lang.InterruptedException")) { //NON-NLS
LOGGER.log(Level.WARNING, "Mime type look up with FileTypeDetector was interupted."); //NOI18N} NON-NLS
return "gif".equalsIgnoreCase(file.getNameExtension()); //NOI18N
} else {
LOGGER.log(Level.SEVERE, "Failed to get mime type of " + getContentPathSafe(file) + " with FileTypeDetector.", ex); //NOI18N} NON-NLS
}
}
LOGGER.log(Level.WARNING, "Falling back on direct mime type check for {0}.", getContentPathSafe(file)); //NOI18N NON-NLS
switch (file.isMimeType(GIF_MIME_SET)) {
case TRUE:
return true;
case UNDEFINED:
LOGGER.log(Level.WARNING, "Falling back on extension check."); //NOI18N NON-NLS
return "gif".equalsIgnoreCase(file.getNameExtension()); //NOI18N
case FALSE:
default:
return false;
} }
} }
@ -283,25 +262,15 @@ public class ImageUtils {
if (file.getSize() == 0) { if (file.getSize() == 0) {
return false; return false;
} }
final String extension = file.getNameExtension(); String mimeType = file.getMIMEType();
try { String extension = file.getNameExtension();
String mimeType = getFileTypeDetector().getFileType(file); if (nonNull(mimeType)) {
if (Objects.nonNull(mimeType)) { return supportedMimeTypes.contains(mimeType)
return supportedMimeTypes.contains(mimeType) || (conditionalMimes.contains(mimeType.toLowerCase())
|| (conditionalMimes.contains(mimeType.toLowerCase()) && supportedExtension.contains(extension)); && supportedExtension.contains(extension));
} } else {
} catch (FileTypeDetector.FileTypeDetectorInitException | TskCoreException ex) { return StringUtils.isNotBlank(extension) && supportedExtension.contains(extension);
LOGGER.log(Level.WARNING, "Failed to look up mimetype for {0} using FileTypeDetector:{1}", new Object[]{getContentPathSafe(file), ex.toString()}); //NOI18N NON-NLS
LOGGER.log(Level.INFO, "Falling back on AbstractFile.isMimeType"); //NOI18N NON-NLS
AbstractFile.MimeMatchEnum mimeMatch = file.isMimeType(supportedMimeTypes);
if (mimeMatch == AbstractFile.MimeMatchEnum.TRUE) {
return true;
} else if (mimeMatch == AbstractFile.MimeMatchEnum.FALSE) {
return false;
}
} }
// if we have an extension, check it
return StringUtils.isNotBlank(extension) && supportedExtension.contains(extension);
} }
/** /**
@ -934,7 +903,7 @@ public class ImageUtils {
* *
* @return * @return
*/ */
static String getContentPathSafe(Content content) { static String getContentPathSafe(Content content) {
try { try {
return content.getUniquePath(); return content.getUniquePath();
} catch (TskCoreException tskCoreException) { } catch (TskCoreException tskCoreException) {

View File

@ -130,7 +130,10 @@ public class FileTypeDetector {
/** /**
* Gets the MIME type of a file, detecting it if it is not already known. If * Gets the MIME type of a file, detecting it if it is not already known. If
* detection is necessary, the result is added to the case database. * detection is necessary, the result is added to the case database. Note
* that this method should not be called except by ingest modules; all other
* clients should call AbstractFile.getMIMEType, and may call
* FileTypeDetector.detect if AbstractFile.getMIMEType returns null.
* *
* @param file The file. * @param file The file.
* *

View File

@ -23,7 +23,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import static java.util.Objects.isNull; import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
@ -183,11 +182,7 @@ public enum FileTypeUtils {
} }
public static Optional<String> getMimeType(AbstractFile file) throws TskCoreException { public static Optional<String> getMimeType(AbstractFile file) throws TskCoreException {
final FileTypeDetector fileTypeDetector = getFileTypeDetector(); return Optional.ofNullable(file.getMIMEType());
if (nonNull(fileTypeDetector)) {
return Optional.ofNullable(fileTypeDetector.getFileType(file));
}
return Optional.empty();
} }
static boolean isDrawableMimeType(String mimeType) { static boolean isDrawableMimeType(String mimeType) {
@ -227,8 +222,8 @@ public enum FileTypeUtils {
try { try {
return getMimeType(file) return getMimeType(file)
.map(String::toLowerCase) .map(String::toLowerCase)
.map(mimeType -> .map(mimeType
mimeType.startsWith("video/") -> mimeType.startsWith("video/")
|| videoMimeTypes.contains(mimeType)) || videoMimeTypes.contains(mimeType))
.orElseGet(() -> FileTypeUtils.videoExtensions.contains(file.getNameExtension())); .orElseGet(() -> FileTypeUtils.videoExtensions.contains(file.getNameExtension()));
} catch (TskCoreException ex) { } catch (TskCoreException ex) {