Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
SaveSnapshot.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.timeline.actions;
20 
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Writer;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.function.Consumer;
32 import java.util.logging.Level;
33 import javafx.embed.swing.SwingFXUtils;
34 import javafx.event.ActionEvent;
35 import javafx.scene.image.WritableImage;
36 import javafx.stage.DirectoryChooser;
37 import javafx.util.Pair;
38 import javax.imageio.ImageIO;
39 import org.controlsfx.control.action.Action;
40 import org.openide.util.NbBundle;
46 
49 public class SaveSnapshot extends Action {
50 
51  private static final String HTML_EXT = ".html";
52 
53  private static final String REPORT_IMAGE_EXTENSION = ".png";
54 
55  private static final Logger LOGGER = Logger.getLogger(SaveSnapshot.class.getName());
56 
58 
59  private final WritableImage snapshot;
60 
61  public SaveSnapshot(TimeLineController controller, WritableImage snapshot) {
62  super(NbBundle.getMessage(SaveSnapshot.class, "SaveSnapshot.action.name.text"));
63  this.controller = controller;
64  this.snapshot = snapshot;
65  setEventHandler(new Consumer<ActionEvent>() {
66 
67  @Override
68  public void accept(ActionEvent t) {
69  //choose location/name
70  DirectoryChooser fileChooser = new DirectoryChooser();
71  fileChooser.setTitle(NbBundle.getMessage(this.getClass(), "SaveSnapshot.fileChoose.title.text"));
72  fileChooser.setInitialDirectory(new File(Case.getCurrentCase().getCaseDirectory() + File.separator + "Reports")); // NON-NLS
73  File outFolder = fileChooser.showDialog(null);
74  if (outFolder == null) {
75  return;
76  }
77  outFolder.mkdir();
78  String name = outFolder.getName();
79 
80  //gather metadata
81  List<Pair<String, String>> reportMetaData = new ArrayList<>();
82 
83  reportMetaData.add(new Pair<>("Case", Case.getCurrentCase().getName())); // NON-NLS
84 
85  ZoomParams get = controller.getEventsModel().getRequestedZoomParamters().get();
86  reportMetaData.add(new Pair<>("Time Range", get.getTimeRange().toString())); // NON-NLS
87  reportMetaData.add(new Pair<>("Description Level of Detail", get.getDescrLOD().getDisplayName())); // NON-NLS
88  reportMetaData.add(new Pair<>("Event Type Zoom Level", get.getTypeZoomLevel().getDisplayName())); // NON-NLS
89  reportMetaData.add(new Pair<>("Filters", get.getFilter().getHTMLReportString())); // NON-NLS
90 
91  //save snapshot as png
92  try {
93  ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", new File(outFolder.getPath() + File.separator + outFolder.getName() + REPORT_IMAGE_EXTENSION)); // NON-NLS
94  } catch (IOException ex) {
95  LOGGER.log(Level.WARNING, "failed to write snapshot to disk", ex); // NON-NLS
96  return;
97  }
98 
99  //build html string
100  StringBuilder wrapper = new StringBuilder();
101  wrapper.append("<html>\n<head>\n\t<title>").append("timeline snapshot").append("</title>\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"index.css\" />\n</head>\n<body>\n"); // NON-NLS
102  wrapper.append("<div id=\"content\">\n<h1>").append(outFolder.getName()).append("</h1>\n"); // NON-NLS
103  wrapper.append("<img src = \"").append(outFolder.getName()).append(REPORT_IMAGE_EXTENSION + "\" alt = \"snaphot\">"); // NON-NLS
104  wrapper.append("<table>\n"); // NON-NLS
105  for (Pair<String, String> pair : reportMetaData) {
106  wrapper.append("<tr><td>").append(pair.getKey()).append(": </td><td>").append(pair.getValue()).append("</td></tr>\n"); // NON-NLS
107  }
108  wrapper.append("</table>\n"); // NON-NLS
109  wrapper.append("</div>\n</body>\n</html>"); // NON-NLS
110 
111  //write html wrapper
112  try (Writer htmlWriter = new FileWriter(new File(outFolder, name + HTML_EXT))) {
113  htmlWriter.write(wrapper.toString());
114  } catch (FileNotFoundException ex) {
115  LOGGER.log(Level.WARNING, "failed to open html wrapper file for writing ", ex); // NON-NLS
116  return;
117  } catch (IOException ex) {
118  LOGGER.log(Level.WARNING, "failed to write html wrapper file", ex); // NON-NLS
119  return;
120  }
121 
122  //copy css
123  try (InputStream resource = this.getClass().getResourceAsStream("/org/sleuthkit/autopsy/timeline/index.css")) { // NON-NLS
124  Files.copy(resource, Paths.get(outFolder.getPath(), "index.css")); // NON-NLS
125  } catch (IOException ex) {
126  LOGGER.log(Level.WARNING, "failed to copy css file", ex); // NON-NLS
127  }
128 
129  //add html file as report to case
130  try {
131  Case.getCurrentCase().addReport(outFolder.getPath() + File.separator + outFolder.getName() + HTML_EXT, "Timeline", outFolder.getName() + HTML_EXT); // NON-NLS
132  } catch (TskCoreException ex) {
133  LOGGER.log(Level.WARNING, "failed add html wrapper as a report", ex); // NON-NLS
134  }
135  }
136  });
137  }
138 }
SaveSnapshot(TimeLineController controller, WritableImage snapshot)
void addReport(String localPath, String srcModuleName, String reportName)
Definition: Case.java:1165
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.