Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExtractActionHelper.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2019 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 content 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.directorytree.actionhelpers;
20 
21 import java.awt.Component;
22 import java.awt.event.ActionEvent;
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.concurrent.ExecutionException;
31 import java.util.logging.Level;
32 import javax.swing.JFileChooser;
33 import javax.swing.JOptionPane;
34 import javax.swing.SwingWorker;
35 import org.netbeans.api.progress.ProgressHandle;
36 import org.openide.util.Cancellable;
37 import org.openide.util.NbBundle;
44 import org.sleuthkit.datamodel.AbstractFile;
45 
49 public class ExtractActionHelper {
50 
51  private final Logger logger = Logger.getLogger(ExtractActionHelper.class.getName());
52  private String userDefinedExportPath;
53 
62  public void extract(ActionEvent event, Collection<? extends AbstractFile> selectedFiles) {
63  if (selectedFiles.size() > 1) {
64  extractFiles(event, selectedFiles);
65  } else if (selectedFiles.size() == 1) {
66  AbstractFile source = selectedFiles.iterator().next();
67  if (source.isDir()) {
68  extractFiles(event, selectedFiles);
69  } else {
70  extractFile(event, selectedFiles.iterator().next());
71  }
72  }
73  }
74 
81  @NbBundle.Messages({"ExtractActionHelper.noOpenCase.errMsg=No open case available."})
82  private void extractFile(ActionEvent event, AbstractFile selectedFile) {
83  Case openCase;
84  try {
85  openCase = Case.getCurrentCaseThrows();
86  } catch (NoCurrentCaseException ex) {
87  JOptionPane.showMessageDialog((Component) event.getSource(), Bundle.ExtractActionHelper_noOpenCase_errMsg());
88  logger.log(Level.INFO, "Exception while getting open case.", ex); //NON-NLS
89  return;
90  }
91  JFileChooser fileChooser = new JFileChooser();
92  fileChooser.setCurrentDirectory(new File(getExportDirectory(openCase)));
93  // If there is an attribute name, change the ":". Otherwise the extracted file will be hidden
94  fileChooser.setSelectedFile(new File(FileUtil.escapeFileName(selectedFile.getName())));
95  if (fileChooser.showSaveDialog((Component) event.getSource()) == JFileChooser.APPROVE_OPTION) {
96  updateExportDirectory(fileChooser.getSelectedFile().getParent(), openCase);
97 
98  ArrayList<FileExtractionTask> fileExtractionTasks = new ArrayList<>();
99  fileExtractionTasks.add(new FileExtractionTask(selectedFile, fileChooser.getSelectedFile()));
100  runExtractionTasks(event, fileExtractionTasks);
101  }
102  }
103 
110  private void extractFiles(ActionEvent event, Collection<? extends AbstractFile> selectedFiles) {
111  Case openCase;
112  try {
113  openCase = Case.getCurrentCaseThrows();
114  } catch (NoCurrentCaseException ex) {
115  JOptionPane.showMessageDialog((Component) event.getSource(), Bundle.ExtractActionHelper_noOpenCase_errMsg());
116  logger.log(Level.INFO, "Exception while getting open case.", ex); //NON-NLS
117  return;
118  }
119  JFileChooser folderChooser = new JFileChooser();
120  folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
121  folderChooser.setCurrentDirectory(new File(getExportDirectory(openCase)));
122  if (folderChooser.showSaveDialog((Component) event.getSource()) == JFileChooser.APPROVE_OPTION) {
123  File destinationFolder = folderChooser.getSelectedFile();
124  if (!destinationFolder.exists()) {
125  try {
126  destinationFolder.mkdirs();
127  } catch (Exception ex) {
128  JOptionPane.showMessageDialog((Component) event.getSource(), NbBundle.getMessage(this.getClass(),
129  "ExtractAction.extractFiles.cantCreateFolderErr.msg"));
130  logger.log(Level.INFO, "Unable to create folder(s) for user " + destinationFolder.getAbsolutePath(), ex); //NON-NLS
131  return;
132  }
133  }
134  updateExportDirectory(destinationFolder.getPath(), openCase);
135 
136  /*
137  * get the unique set of files from the list. A user once reported
138  * extraction taking days because it was extracting the same PST
139  * file 20k times. They selected 20k email messages in the tree and
140  * chose to extract them.
141  */
142  Set<AbstractFile> uniqueFiles = new HashSet<>(selectedFiles);
143 
144  // make a task for each file
145  ArrayList<FileExtractionTask> fileExtractionTasks = new ArrayList<>();
146  for (AbstractFile source : uniqueFiles) {
147  // If there is an attribute name, change the ":". Otherwise the extracted file will be hidden
148  fileExtractionTasks.add(new FileExtractionTask(source, new File(destinationFolder, source.getId() + "-" + FileUtil.escapeFileName(source.getName()))));
149  }
150  runExtractionTasks(event, fileExtractionTasks);
151  }
152  }
153 
161  private String getExportDirectory(Case openCase) {
162  String caseExportPath = openCase.getExportDirectory();
163 
164  if (userDefinedExportPath == null) {
165  return caseExportPath;
166  }
167 
168  File file = new File(userDefinedExportPath);
169  if (file.exists() == false || file.isDirectory() == false) {
170  return caseExportPath;
171  }
172 
173  return userDefinedExportPath;
174  }
175 
185  private void updateExportDirectory(String exportPath, Case openCase) {
186  if (exportPath.equalsIgnoreCase(openCase.getExportDirectory())) {
187  userDefinedExportPath = null;
188  } else {
189  userDefinedExportPath = exportPath;
190  }
191  }
192 
200  private void runExtractionTasks(ActionEvent event, List<FileExtractionTask> fileExtractionTasks) {
201 
202  // verify all of the sources and destinations are OK
203  for (Iterator<FileExtractionTask> it = fileExtractionTasks.iterator(); it.hasNext();) {
204  FileExtractionTask task = it.next();
205 
206  if (ContentUtils.isDotDirectory(task.source)) {
207  it.remove();
208  continue;
209  }
210 
211  /*
212  * This code assumes that each destination is unique. We previously
213  * satisfied that by adding the unique ID.
214  */
215  if (task.destination.exists()) {
216  if (JOptionPane.showConfirmDialog((Component) event.getSource(),
217  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.confDlg.destFileExist.msg", task.destination.getAbsolutePath()),
218  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.confDlg.destFileExist.title"),
219  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
220  if (!FileUtil.deleteFileDir(task.destination)) {
221  JOptionPane.showMessageDialog((Component) event.getSource(),
222  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.msgDlg.cantOverwriteFile.msg", task.destination.getAbsolutePath()));
223  it.remove();
224  }
225  } else {
226  it.remove();
227  }
228  }
229  }
230 
231  // launch a thread to do the work
232  if (!fileExtractionTasks.isEmpty()) {
233  try {
234  FileExtracter extracter = new FileExtracter(fileExtractionTasks);
235  extracter.execute();
236  } catch (Exception ex) {
237  logger.log(Level.WARNING, "Unable to start background file extraction thread", ex); //NON-NLS
238  }
239  } else {
241  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.notifyDlg.noFileToExtr.msg"));
242  }
243  }
244 
248  private class FileExtractionTask {
249 
250  AbstractFile source;
251  File destination;
252 
259  FileExtractionTask(AbstractFile source, File destination) {
260  this.source = source;
261  this.destination = destination;
262  }
263  }
264 
268  private class FileExtracter extends SwingWorker<Object, Void> {
269 
270  private final Logger logger = Logger.getLogger(FileExtracter.class.getName());
271  private ProgressHandle progress;
272  private final List<FileExtractionTask> extractionTasks;
273 
279  FileExtracter(List<FileExtractionTask> extractionTasks) {
280  this.extractionTasks = extractionTasks;
281  }
282 
283  @Override
284  protected Object doInBackground() throws Exception {
285  if (extractionTasks.isEmpty()) {
286  return null;
287  }
288 
289  // Setup progress bar.
290  final String displayName = NbBundle.getMessage(this.getClass(), "ExtractActionHelper.progress.extracting");
291  progress = ProgressHandle.createHandle(displayName, new Cancellable() {
292  @Override
293  public boolean cancel() {
294  if (progress != null) {
295  progress.setDisplayName(
296  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.progress.cancellingExtraction", displayName));
297  }
298  return ExtractActionHelper.FileExtracter.this.cancel(true);
299  }
300  });
301  progress.start();
302  progress.switchToIndeterminate();
303 
304  /*
305  * @@@ Add back in -> Causes exceptions int workUnits = 0; for
306  * (FileExtractionTask task : extractionTasks) { workUnits +=
307  * calculateProgressBarWorkUnits(task.source); }
308  * progress.switchToDeterminate(workUnits);
309  */
310  // Do the extraction tasks.
311  for (FileExtractionTask task : this.extractionTasks) {
312  // @@@ Note, we are no longer passing in progress
313  ContentUtils.ExtractFscContentVisitor.extract(task.source, task.destination, null, this);
314  }
315 
316  return null;
317  }
318 
319  @Override
320  protected void done() {
321  boolean msgDisplayed = false;
322  try {
323  super.get();
324  } catch (InterruptedException | ExecutionException ex) {
325  logger.log(Level.SEVERE, "Fatal error during file extraction", ex); //NON-NLS
327  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.done.notifyMsg.extractErr", ex.getMessage()));
328  msgDisplayed = true;
329  } finally {
330  progress.finish();
331  if (!this.isCancelled() && !msgDisplayed) {
333  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.done.notifyMsg.fileExtr.text"));
334  }
335  }
336  }
337 
346  /*
347  * private int calculateProgressBarWorkUnits(AbstractFile file) { int
348  * workUnits = 0; if (file.isFile()) { workUnits += file.getSize(); }
349  * else { try { for (Content child : file.getChildren()) { if (child
350  * instanceof AbstractFile) { workUnits +=
351  * calculateProgressBarWorkUnits((AbstractFile) child); } } } catch
352  * (TskCoreException ex) { logger.log(Level.SEVERE, "Could not get
353  * children of content", ex); //NON-NLS } } return workUnits; }
354  */
355  }
356 }
void extractFiles(ActionEvent event, Collection<?extends AbstractFile > selectedFiles)
static boolean deleteFileDir(File path)
Definition: FileUtil.java:87
static< T, V > void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker< T, V > worker)
void runExtractionTasks(ActionEvent event, List< FileExtractionTask > fileExtractionTasks)
void extract(ActionEvent event, Collection<?extends AbstractFile > selectedFiles)
static String escapeFileName(String fileName)
Definition: FileUtil.java:169
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
void extractFile(ActionEvent event, AbstractFile selectedFile)
static boolean isDotDirectory(AbstractFile dir)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.