19 package org.sleuthkit.autopsy.discovery.ui;
21 import com.google.common.io.Files;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.Image;
25 import java.awt.Point;
26 import java.awt.image.BufferedImage;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.logging.Level;
35 import javax.imageio.ImageIO;
36 import javax.swing.ImageIcon;
37 import javax.swing.JComponent;
38 import javax.swing.JOptionPane;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTextPane;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.io.FilenameUtils;
43 import org.imgscalr.Scalr;
44 import org.netbeans.api.progress.ProgressHandle;
45 import org.opencv.core.Mat;
46 import org.opencv.highgui.VideoCapture;
47 import org.openide.util.ImageUtilities;
48 import org.openide.util.NbBundle;
68 final class DiscoveryUiUtils {
70 private final static Logger logger = Logger.getLogger(DiscoveryUiUtils.class.getName());
71 private static final int BYTE_UNIT_CONVERSION = 1000;
72 private static final int ICON_SIZE = 16;
73 private static final String RED_CIRCLE_ICON_PATH =
"org/sleuthkit/autopsy/images/red-circle-exclamation.png";
74 private static final String YELLOW_CIRCLE_ICON_PATH =
"org/sleuthkit/autopsy/images/yellow-circle-yield.png";
75 private static final String DELETE_ICON_PATH =
"org/sleuthkit/autopsy/images/file-icon-deleted.png";
76 private static final String UNSUPPORTED_DOC_PATH =
"org/sleuthkit/autopsy/images/image-extraction-not-supported.png";
77 private static final ImageIcon INTERESTING_SCORE_ICON =
new ImageIcon(ImageUtilities.loadImage(YELLOW_CIRCLE_ICON_PATH,
false));
78 private static final ImageIcon NOTABLE_SCORE_ICON =
new ImageIcon(ImageUtilities.loadImage(RED_CIRCLE_ICON_PATH,
false));
79 private static final ImageIcon DELETED_ICON =
new ImageIcon(ImageUtilities.loadImage(DELETE_ICON_PATH,
false));
80 private static final ImageIcon UNSUPPORTED_DOCUMENT_THUMBNAIL =
new ImageIcon(ImageUtilities.loadImage(UNSUPPORTED_DOC_PATH,
false));
81 private static final String THUMBNAIL_FORMAT =
"png";
82 private static final String VIDEO_THUMBNAIL_DIR =
"video-thumbnails";
83 private static final BufferedImage VIDEO_DEFAULT_IMAGE = getDefaultVideoThumbnail();
85 @NbBundle.Messages({
"# {0} - fileSize",
87 "DiscoveryUiUtility.sizeLabel.text=Size: {0} {1}",
88 "DiscoveryUiUtility.bytes.text=bytes",
89 "DiscoveryUiUtility.kiloBytes.text=KB",
90 "DiscoveryUiUtility.megaBytes.text=MB",
91 "DiscoveryUiUtility.gigaBytes.text=GB",
92 "DiscoveryUiUtility.terraBytes.text=TB"})
101 static String getFileSizeString(
long bytes) {
103 int unitsSwitchValue = 0;
104 while (size > BYTE_UNIT_CONVERSION && unitsSwitchValue < 4) {
105 size /= BYTE_UNIT_CONVERSION;
109 switch (unitsSwitchValue) {
111 units = Bundle.DiscoveryUiUtility_kiloBytes_text();
114 units = Bundle.DiscoveryUiUtility_megaBytes_text();
117 units = Bundle.DiscoveryUiUtility_gigaBytes_text();
120 units = Bundle.DiscoveryUiUtility_terraBytes_text();
123 units = Bundle.DiscoveryUiUtility_bytes_text();
126 return Bundle.DiscoveryUiUtility_sizeLabel_text(size, units);
135 static ImageIcon getUnsupportedImageThumbnail() {
136 return UNSUPPORTED_DOCUMENT_THUMBNAIL;
151 static List<String> getSetNames(BlackboardArtifact.ARTIFACT_TYPE artifactType, BlackboardAttribute.ATTRIBUTE_TYPE setNameAttribute) throws TskCoreException {
152 List<BlackboardArtifact> arts = Case.getCurrentCase().getSleuthkitCase().getBlackboardArtifacts(artifactType);
153 List<String> setNames =
new ArrayList<>();
154 for (BlackboardArtifact art : arts) {
155 for (BlackboardAttribute attr : art.getAttributes()) {
156 if (attr.getAttributeType().getTypeID() == setNameAttribute.getTypeID()) {
157 String setName = attr.getValueString();
158 if (!setNames.contains(setName)) {
159 setNames.add(setName);
164 Collections.sort(setNames);
176 static boolean isPointOnIcon(Component comp, Point point) {
177 return comp instanceof JComponent && point.x >= comp.getX() && point.x <= comp.getX() + ICON_SIZE && point.y >= comp.getY() && point.y <= comp.getY() + ICON_SIZE;
188 @NbBundle.Messages({
"DiscoveryUiUtils.isDeleted.text=All instances of file are deleted."})
189 static void setDeletedIcon(
boolean isDeleted, javax.swing.JLabel isDeletedLabel) {
191 isDeletedLabel.setIcon(DELETED_ICON);
192 isDeletedLabel.setToolTipText(Bundle.DiscoveryUiUtils_isDeleted_text());
194 isDeletedLabel.setIcon(null);
195 isDeletedLabel.setToolTipText(null);
206 static void setScoreIcon(ResultFile resultFile, javax.swing.JLabel scoreLabel) {
207 switch (resultFile.getScore()) {
209 scoreLabel.setIcon(NOTABLE_SCORE_ICON);
211 case INTERESTING_SCORE:
212 scoreLabel.setIcon(INTERESTING_SCORE_ICON);
216 scoreLabel.setIcon(null);
219 scoreLabel.setToolTipText(resultFile.getScoreDescription());
227 static int getIconSize() {
235 @NbBundle.Messages({
"DiscoveryUiUtils.resultsIncomplete.text=Discovery results may be incomplete"})
236 static void displayErrorMessage(DiscoveryDialog dialog) {
239 SleuthkitCase skCase = Case.getCurrentCaseThrows().getSleuthkitCase();
240 Map<Long, DataSourceModulesWrapper> dataSourceIngestModules =
new HashMap<>();
241 for (DataSource dataSource : skCase.getDataSources()) {
242 dataSourceIngestModules.put(dataSource.getId(),
new DataSourceModulesWrapper(dataSource.getName()));
245 for (IngestJobInfo jobInfo : skCase.getIngestJobs()) {
246 dataSourceIngestModules.get(jobInfo.getObjectId()).updateModulesRun(jobInfo);
249 for (DataSourceModulesWrapper dsmodulesWrapper : dataSourceIngestModules.values()) {
250 message += dsmodulesWrapper.getMessage();
252 if (!message.isEmpty()) {
253 JScrollPane messageScrollPane =
new JScrollPane();
254 JTextPane messageTextPane =
new JTextPane();
255 messageTextPane.setText(message);
256 messageTextPane.setVisible(
true);
257 messageTextPane.setEditable(
false);
258 messageTextPane.setCaretPosition(0);
259 messageScrollPane.setMaximumSize(
new Dimension(600, 100));
260 messageScrollPane.setPreferredSize(
new Dimension(600, 100));
261 messageScrollPane.setViewportView(messageTextPane);
262 JOptionPane.showMessageDialog(dialog, messageScrollPane, Bundle.DiscoveryUiUtils_resultsIncomplete_text(), JOptionPane.PLAIN_MESSAGE);
264 }
catch (NoCurrentCaseException | TskCoreException ex) {
265 logger.log(Level.WARNING,
"Exception while determining which modules have been run for Discovery", ex);
267 dialog.validateDialog();
279 @NbBundle.Messages({
"# {0} - file name",
280 "DiscoveryUiUtils.genVideoThumb.progress.text=extracting temporary file {0}"})
281 static void getVideoThumbnails(VideoThumbnailsWrapper thumbnailWrapper) {
282 AbstractFile file = thumbnailWrapper.getResultFile().getFirstInstance();
283 String cacheDirectory;
285 cacheDirectory = Case.getCurrentCaseThrows().getCacheDirectory();
286 }
catch (NoCurrentCaseException ex) {
287 cacheDirectory = null;
288 logger.log(Level.WARNING,
"Unable to get cache directory, video thumbnails will not be saved", ex);
290 if (cacheDirectory == null || file.getMd5Hash() == null || !Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash()).toFile().exists()) {
291 java.io.File tempFile;
293 tempFile = getVideoFileInTempDir(file);
294 }
catch (NoCurrentCaseException ex) {
295 logger.log(Level.WARNING,
"Exception while getting open case.", ex);
296 int[] framePositions =
new int[]{
301 thumbnailWrapper.setThumbnails(createDefaultThumbnailList(VIDEO_DEFAULT_IMAGE), framePositions);
304 if (tempFile.exists() ==
false || tempFile.length() < file.getSize()) {
305 ProgressHandle progress = ProgressHandle.createHandle(Bundle.DiscoveryUiUtils_genVideoThumb_progress_text(file.getName()));
308 Files.createParentDirs(tempFile);
309 if (Thread.interrupted()) {
310 int[] framePositions =
new int[]{
315 thumbnailWrapper.setThumbnails(createDefaultThumbnailList(VIDEO_DEFAULT_IMAGE), framePositions);
318 ContentUtils.writeToFile(file, tempFile, progress, null,
true);
319 }
catch (IOException ex) {
320 logger.log(Level.WARNING,
"Error extracting temporary file for " + file.getParentPath() +
"/" + file.getName(), ex);
325 VideoCapture videoFile =
new VideoCapture();
326 BufferedImage bufferedImage = null;
329 if (!videoFile.open(tempFile.toString())) {
330 logger.log(Level.WARNING,
"Error opening {0} for preview generation.", file.getParentPath() +
"/" + file.getName());
331 int[] framePositions =
new int[]{
336 thumbnailWrapper.setThumbnails(createDefaultThumbnailList(VIDEO_DEFAULT_IMAGE), framePositions);
339 double fps = videoFile.get(5);
340 double totalFrames = videoFile.get(7);
341 if (fps <= 0 || totalFrames <= 0) {
342 logger.log(Level.WARNING,
"Error getting fps or total frames for {0}", file.getParentPath() +
"/" + file.getName());
343 int[] framePositions =
new int[]{
348 thumbnailWrapper.setThumbnails(createDefaultThumbnailList(VIDEO_DEFAULT_IMAGE), framePositions);
351 if (Thread.interrupted()) {
352 int[] framePositions =
new int[]{
357 thumbnailWrapper.setThumbnails(createDefaultThumbnailList(VIDEO_DEFAULT_IMAGE), framePositions);
361 double duration = 1000 * (totalFrames / fps);
363 int[] framePositions =
new int[]{
364 (int) (duration * .01),
365 (int) (duration * .25),
366 (int) (duration * .5),
367 (int) (duration * .75),};
369 Mat imageMatrix =
new Mat();
370 List<Image> videoThumbnails =
new ArrayList<>();
371 if (cacheDirectory == null || file.getMd5Hash() == null) {
372 cacheDirectory = null;
375 FileUtils.forceMkdir(Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash()).toFile());
376 }
catch (IOException ex) {
377 cacheDirectory = null;
378 logger.log(Level.WARNING,
"Unable to make video thumbnails directory, thumbnails will not be saved", ex);
381 for (
int i = 0; i < framePositions.length; i++) {
382 if (!videoFile.set(0, framePositions[i])) {
383 logger.log(Level.WARNING,
"Error seeking to " + framePositions[i] +
"ms in {0}", file.getParentPath() +
"/" + file.getName());
386 videoThumbnails.add(VIDEO_DEFAULT_IMAGE);
387 if (cacheDirectory != null) {
389 ImageIO.write(VIDEO_DEFAULT_IMAGE, THUMBNAIL_FORMAT,
390 Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash(), i +
"-" + framePositions[i] +
"." + THUMBNAIL_FORMAT).toFile());
391 }
catch (IOException ex) {
392 logger.log(Level.WARNING,
"Unable to save default video thumbnail for " + file.getMd5Hash() +
" at frame position " + framePositions[i], ex);
398 if (!videoFile.read(imageMatrix)) {
399 logger.log(Level.WARNING,
"Error reading frame at " + framePositions[i] +
"ms from {0}", file.getParentPath() +
"/" + file.getName());
401 videoThumbnails.add(VIDEO_DEFAULT_IMAGE);
402 if (cacheDirectory != null) {
404 ImageIO.write(VIDEO_DEFAULT_IMAGE, THUMBNAIL_FORMAT,
405 Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash(), i +
"-" + framePositions[i] +
"." + THUMBNAIL_FORMAT).toFile());
406 }
catch (IOException ex) {
407 logger.log(Level.WARNING,
"Unable to save default video thumbnail for " + file.getMd5Hash() +
" at frame position " + framePositions[i], ex);
414 if (imageMatrix.empty()) {
415 videoThumbnails.add(VIDEO_DEFAULT_IMAGE);
416 if (cacheDirectory != null) {
418 ImageIO.write(VIDEO_DEFAULT_IMAGE, THUMBNAIL_FORMAT,
419 Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash(), i +
"-" + framePositions[i] +
"." + THUMBNAIL_FORMAT).toFile());
420 }
catch (IOException ex) {
421 logger.log(Level.WARNING,
"Unable to save default video thumbnail for " + file.getMd5Hash() +
" at frame position " + framePositions[i], ex);
427 int matrixColumns = imageMatrix.cols();
428 int matrixRows = imageMatrix.rows();
431 if (bufferedImage == null) {
432 bufferedImage =
new BufferedImage(matrixColumns, matrixRows, BufferedImage.TYPE_3BYTE_BGR);
435 byte[] data =
new byte[matrixRows * matrixColumns * (int) (imageMatrix.elemSize())];
436 imageMatrix.get(0, 0, data);
438 if (imageMatrix.channels() == 3) {
439 for (
int k = 0; k < data.length; k += 3) {
441 data[k] = data[k + 2];
446 bufferedImage.getRaster().setDataElements(0, 0, matrixColumns, matrixRows, data);
447 if (Thread.interrupted()) {
448 thumbnailWrapper.setThumbnails(videoThumbnails, framePositions);
450 FileUtils.forceDelete(Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash()).toFile());
451 }
catch (IOException ex) {
452 logger.log(Level.WARNING,
"Unable to delete directory for cancelled video thumbnail process", ex);
456 BufferedImage thumbnail = ScalrWrapper.resize(bufferedImage, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, ImageUtils.ICON_SIZE_LARGE, ImageUtils.ICON_SIZE_MEDIUM, Scalr.OP_ANTIALIAS);
458 videoThumbnails.add(thumbnail);
459 if (cacheDirectory != null) {
461 ImageIO.write(thumbnail, THUMBNAIL_FORMAT,
462 Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, file.getMd5Hash(), i +
"-" + framePositions[i] +
"." + THUMBNAIL_FORMAT).toFile());
463 }
catch (IOException ex) {
464 logger.log(Level.WARNING,
"Unable to save video thumbnail for " + file.getMd5Hash() +
" at frame position " + framePositions[i], ex);
468 thumbnailWrapper.setThumbnails(videoThumbnails, framePositions);
473 loadSavedThumbnails(cacheDirectory, thumbnailWrapper, VIDEO_DEFAULT_IMAGE);
482 private static BufferedImage getDefaultVideoThumbnail() {
484 return ImageIO.read(ImageUtils.class
485 .getResourceAsStream(
"/org/sleuthkit/autopsy/images/failedToCreateVideoThumb.png"));
486 }
catch (IOException ex) {
487 logger.log(Level.SEVERE,
"Failed to load 'failed to create video' placeholder.", ex);
502 private static void loadSavedThumbnails(String cacheDirectory, VideoThumbnailsWrapper thumbnailWrapper, BufferedImage failedVideoThumbImage) {
503 int[] framePositions =
new int[4];
504 List<Image> videoThumbnails =
new ArrayList<>();
505 int thumbnailNumber = 0;
506 String md5 = thumbnailWrapper.getResultFile().getFirstInstance().getMd5Hash();
507 for (String fileName : Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, md5).toFile().list()) {
509 videoThumbnails.add(ImageIO.read(Paths.get(cacheDirectory, VIDEO_THUMBNAIL_DIR, md5, fileName).toFile()));
510 }
catch (IOException ex) {
511 videoThumbnails.add(failedVideoThumbImage);
512 logger.log(Level.WARNING,
"Unable to read saved video thumbnail " + fileName +
" for " + md5, ex);
514 int framePos = Integer.valueOf(FilenameUtils.getBaseName(fileName).substring(2));
515 framePositions[thumbnailNumber] = framePos;
518 thumbnailWrapper.setThumbnails(videoThumbnails, framePositions);
527 private static List<Image> createDefaultThumbnailList(BufferedImage failedVideoThumbImage) {
528 List<Image> videoThumbnails =
new ArrayList<>();
529 videoThumbnails.add(failedVideoThumbImage);
530 videoThumbnails.add(failedVideoThumbImage);
531 videoThumbnails.add(failedVideoThumbImage);
532 videoThumbnails.add(failedVideoThumbImage);
533 return videoThumbnails;
539 private DiscoveryUiUtils() {
static File getVideoFileInTempDir(AbstractFile file)