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 javax.swing.JPanel;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector;
import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.TskCoreException;
/**
* 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
public boolean isSupported(AbstractFile file) {
/*
* TODO (AUT-2042): Is this the logic we want?
*/
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)) {
SortedSet<String> mimeTypes = new TreeSet<>(getMimeTypes());
try {
String mimeType = new FileTypeDetector().getFileType(file);
if (nonNull(mimeType)) {
return mimeTypes.contains(mimeType);
}
} 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;
}
String mimeType = file.getMIMEType();
if (nonNull(mimeType)) {
return mimeTypes.contains(mimeType);
} else {
return getExtensionsList().contains("." + extension);
}
return getExtensionsList().contains("." + extension);
}
return false;
}

View File

@ -149,8 +149,8 @@ public class ImageUtils {
/**
* thread that saves generated thumbnails to disk in the background
*/
private static final Executor imageSaver =
Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
private static final Executor imageSaver
= Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
.namingPattern("icon saver-%d").build()); //NOI18N NON-NLS
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) {
try {
final FileTypeDetector myFileTypeDetector = getFileTypeDetector();
if (nonNull(myFileTypeDetector)) {
String fileType = myFileTypeDetector.getFileType(file);
return IMAGE_GIF_MIME.equalsIgnoreCase(fileType);
}
} 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;
String mimeType = file.getMIMEType();
if (nonNull(mimeType)) {
return IMAGE_GIF_MIME.equalsIgnoreCase(mimeType);
} else {
return "gif".equalsIgnoreCase(file.getNameExtension()); //NOI18N
}
}
@ -283,25 +262,15 @@ public class ImageUtils {
if (file.getSize() == 0) {
return false;
}
final String extension = file.getNameExtension();
try {
String mimeType = getFileTypeDetector().getFileType(file);
if (Objects.nonNull(mimeType)) {
return supportedMimeTypes.contains(mimeType)
|| (conditionalMimes.contains(mimeType.toLowerCase()) && supportedExtension.contains(extension));
}
} catch (FileTypeDetector.FileTypeDetectorInitException | TskCoreException ex) {
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;
}
String mimeType = file.getMIMEType();
String extension = file.getNameExtension();
if (nonNull(mimeType)) {
return supportedMimeTypes.contains(mimeType)
|| (conditionalMimes.contains(mimeType.toLowerCase())
&& supportedExtension.contains(extension));
} else {
return StringUtils.isNotBlank(extension) && supportedExtension.contains(extension);
}
// if we have an extension, check it
return StringUtils.isNotBlank(extension) && supportedExtension.contains(extension);
}
/**
@ -934,7 +903,7 @@ public class ImageUtils {
*
* @return
*/
static String getContentPathSafe(Content content) {
static String getContentPathSafe(Content content) {
try {
return content.getUniquePath();
} 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
* 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.
*

View File

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