Merge pull request #1695 from millmanorama/ig-stop-population-on-db-error

adjust error handling to shut down db transfer task when db can not b…
This commit is contained in:
Richard Cordovano
2015-11-04 09:11:36 -05:00
3 changed files with 48 additions and 50 deletions
@@ -51,9 +51,9 @@ public enum FileTypeUtils {
private static final Logger LOGGER = Logger.getLogger(FileTypeUtils.class.getName());
/**
* Set of specific mimetypes (as strings) that we should support(ie,
* include in db and show to user).
* These are in addition to all image/* or video/* types
* Set of specific mimetypes (as strings) that we should support(ie, include
* in db and show to user). These are in addition to all image/* or video/*
* types
*/
private static final Set<String> supportedMimeTypes = new HashSet<>();
/**
@@ -76,8 +76,8 @@ public enum FileTypeUtils {
*/
private static final Set<String> supportedExtensions;
/**
* Lazily instantiated FileTypeDetector to use when the mimetype of a
* file is needed
* Lazily instantiated FileTypeDetector to use when the mimetype of a file
* is needed
*/
private static FileTypeDetector FILE_TYPE_DETECTOR;
@@ -86,8 +86,8 @@ public enum FileTypeUtils {
private static final TreeSet<String> GIF_MIME_SET = new TreeSet<>(Arrays.asList(IMAGE_GIF_MIME));
/**
* static initalizer block to initialize sets of extensions and mimetypes
* to be supported
* static initalizer block to initialize sets of extensions and mimetypes to
* be supported
*/
static {
ImageIO.scanForPlugins();
@@ -169,7 +169,7 @@ public enum FileTypeUtils {
*
* @return true if this file is supported or false if not
*/
public static boolean isDrawable(AbstractFile file) {
public static boolean isDrawable(AbstractFile file) throws TskCoreException {
return hasDrawableMimeType(file).orElseGet(() -> {
final boolean contains = FileTypeUtils.supportedExtensions.contains(file.getNameExtension());
final boolean jpegFileHeader = ImageUtils.isJpegFileHeader(file);
@@ -209,28 +209,25 @@ public enum FileTypeUtils {
*
* @param file
*
* @return an Optional containg:
* True if the file has an image or video mime type.
* False if a non image/video mimetype.
* null empty Optional if a mimetype could not be detected.
* @return an Optional containg: True if the file has an image or video mime
* type. False if a non image/video mimetype. empty Optional if
* a mimetype could not be detected.
*/
static Optional<Boolean> hasDrawableMimeType(AbstractFile file) {
try {
final FileTypeDetector fileTypeDetector = getFileTypeDetector();
if (nonNull(fileTypeDetector)) {
String mimeType = fileTypeDetector.getFileType(file);
if (isNull(mimeType)) {
return Optional.empty();
} else {
mimeType = mimeType.toLowerCase();
return Optional.of(mimeType.startsWith("image/")
|| mimeType.startsWith("video/")
|| supportedMimeTypes.contains(mimeType));
}
static Optional<Boolean> hasDrawableMimeType(AbstractFile file) throws TskCoreException {
final FileTypeDetector fileTypeDetector = getFileTypeDetector();
if (nonNull(fileTypeDetector)) {
String mimeType = fileTypeDetector.getFileType(file);
if (isNull(mimeType)) {
return Optional.empty();
} else {
mimeType = mimeType.toLowerCase();
return Optional.of(mimeType.startsWith("image/")
|| mimeType.startsWith("video/")
|| supportedMimeTypes.contains(mimeType));
}
} catch (TskCoreException ex) {
LOGGER.log(Level.INFO, "failed to get mime type for " + file.getName(), ex);
}
return Optional.empty();
}
@@ -480,13 +480,19 @@ public final class ImageGalleryController {
if (isListeningEnabled()) {
if (file.isFile()) {
if (ImageGalleryModule.isDrawableAndNotKnown(file)) {
//this file should be included and we don't already know about it from hash sets (NSRL)
queueDBWorkerTask(new UpdateFileTask(file, db));
} else if (FileTypeUtils.getAllSupportedExtensions().contains(file.getNameExtension())) {
//doing this check results in fewer tasks queued up, and faster completion of db update
//this file would have gotten scooped up in initial grab, but actually we don't need it
queueDBWorkerTask(new RemoveFileTask(file, db));
try {
if (ImageGalleryModule.isDrawableAndNotKnown(file)) {
//this file should be included and we don't already know about it from hash sets (NSRL)
queueDBWorkerTask(new UpdateFileTask(file, db));
} else if (FileTypeUtils.getAllSupportedExtensions().contains(file.getNameExtension())) {
//doing this check results in fewer tasks queued up, and faster completion of db update
//this file would have gotten scooped up in initial grab, but actually we don't need it
queueDBWorkerTask(new RemoveFileTask(file, db));
}
} catch (TskCoreException ex) {
//TODO: What to do here?
LOGGER.log(Level.WARNING, "Unable to determine if file is drawable and not known. Not making any changes to DB", ex);
throw new RuntimeException(ex);
}
}
} else { //TODO: keep track of what we missed for later
@@ -776,8 +782,8 @@ public final class ImageGalleryController {
+ StringUtils.join(FileTypeUtils.getAllSupportedExtensions(),
"' or name LIKE '%.")
+ "')";
static private final String MIMETYPE_CLAUSE =
"blackboard_attributes.value_text LIKE '"
static private final String MIMETYPE_CLAUSE
= "blackboard_attributes.value_text LIKE '"
+ StringUtils.join(FileTypeUtils.getAllSupportedMimeTypes(),
"' OR blackboard_attributes.value_text LIKE '") + "' ";
@@ -857,13 +863,18 @@ public final class ImageGalleryController {
taskDB.commitTransaction(tr, true);
} catch (TskCoreException ex) {
Logger.getLogger(CopyAnalyzedFiles.class.getName()).log(Level.WARNING, "failed to transfer all database contents", ex);
progressHandle.progress("Stopping copy to drawable db task.");
Logger.getLogger(CopyAnalyzedFiles.class.getName()).log(Level.WARNING, "Stopping copy to drawable db task. Failed to transfer all database contents: " + ex.getMessage());
progressHandle.finish();
updateMessage("");
updateProgress(-1.0);
controller.setStale(true);
return;
}
progressHandle.finish();
progressHandle.finish();
updateMessage("");
updateProgress(-1.0);
controller.setStale(false);
}
}
@@ -18,23 +18,13 @@
*/
package org.sleuthkit.autopsy.imagegallery;
import com.google.common.collect.Sets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
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;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.ImageUtils;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableDB;
import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector;
import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.TskCoreException;
import org.sleuthkit.datamodel.TskData;
@@ -112,7 +102,7 @@ public class ImageGalleryModule {
* @return true if the given {@link AbstractFile} is "drawable" and not
* 'known', else false
*/
public static boolean isDrawableAndNotKnown(AbstractFile abstractFile) {
public static boolean isDrawableAndNotKnown(AbstractFile abstractFile) throws TskCoreException {
return (abstractFile.getKnown() != TskData.FileKnown.KNOWN) && FileTypeUtils.isDrawable(abstractFile);
}
}