Autopsy  4.19.1
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-2021 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;
38 import org.openide.util.NbBundle.Messages;
46 import org.sleuthkit.datamodel.AbstractFile;
47 
51 public class ExtractActionHelper {
52 
53  private final Logger logger = Logger.getLogger(ExtractActionHelper.class.getName());
54  private String userDefinedExportPath;
55 
58 
67  public void extract(ActionEvent event, Collection<? extends AbstractFile> selectedFiles) {
68  if (selectedFiles.size() > 1) {
69  extractFiles(event, selectedFiles);
70  } else if (selectedFiles.size() == 1) {
71  AbstractFile source = selectedFiles.iterator().next();
72  if (source.isDir()) {
73  extractFiles(event, selectedFiles);
74  } else {
75  extractFile(event, selectedFiles.iterator().next());
76  }
77  }
78  }
79 
86  @NbBundle.Messages({"ExtractActionHelper.noOpenCase.errMsg=No open case available."})
87  private void extractFile(ActionEvent event, AbstractFile selectedFile) {
88  Case openCase;
89  try {
90  openCase = Case.getCurrentCaseThrows();
91  } catch (NoCurrentCaseException ex) {
92  JOptionPane.showMessageDialog((Component) event.getSource(), Bundle.ExtractActionHelper_noOpenCase_errMsg());
93  logger.log(Level.INFO, "Exception while getting open case.", ex); //NON-NLS
94  return;
95  }
96  JFileChooser fileChooser = extractFileHelper.getChooser();
97  fileChooser.setCurrentDirectory(new File(getExportDirectory(openCase)));
98  // If there is an attribute name, change the ":". Otherwise the extracted file will be hidden
99  fileChooser.setSelectedFile(new File(FileUtil.escapeFileName(selectedFile.getName())));
100  if (fileChooser.showSaveDialog((Component) event.getSource()) == JFileChooser.APPROVE_OPTION) {
101  updateExportDirectory(fileChooser.getSelectedFile().getParent(), openCase);
102 
103  ArrayList<FileExtractionTask> fileExtractionTasks = new ArrayList<>();
104  fileExtractionTasks.add(new FileExtractionTask(selectedFile, fileChooser.getSelectedFile()));
105  runExtractionTasks(event, fileExtractionTasks, fileChooser.getSelectedFile().getName());
106  }
107  }
108 
115  private void extractFiles(ActionEvent event, Collection<? extends AbstractFile> selectedFiles) {
116  Case openCase;
117  try {
118  openCase = Case.getCurrentCaseThrows();
119  } catch (NoCurrentCaseException ex) {
120  JOptionPane.showMessageDialog((Component) event.getSource(), Bundle.ExtractActionHelper_noOpenCase_errMsg());
121  logger.log(Level.INFO, "Exception while getting open case.", ex); //NON-NLS
122  return;
123  }
124  JFileChooser folderChooser = extractFilesHelper.getChooser();
125  folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
126  folderChooser.setCurrentDirectory(new File(getExportDirectory(openCase)));
127  if (folderChooser.showSaveDialog((Component) event.getSource()) == JFileChooser.APPROVE_OPTION) {
128  File destinationFolder = folderChooser.getSelectedFile();
129  if (!destinationFolder.exists()) {
130  try {
131  destinationFolder.mkdirs();
132  } catch (Exception ex) {
133  JOptionPane.showMessageDialog((Component) event.getSource(), NbBundle.getMessage(this.getClass(),
134  "ExtractAction.extractFiles.cantCreateFolderErr.msg"));
135  logger.log(Level.INFO, "Unable to create folder(s) for user " + destinationFolder.getAbsolutePath(), ex); //NON-NLS
136  return;
137  }
138  }
139  updateExportDirectory(destinationFolder.getPath(), openCase);
140 
141  /*
142  * get the unique set of files from the list. A user once reported
143  * extraction taking days because it was extracting the same PST
144  * file 20k times. They selected 20k email messages in the tree and
145  * chose to extract them.
146  */
147  Set<AbstractFile> uniqueFiles = new HashSet<>(selectedFiles);
148 
149  // make a task for each file
150  ArrayList<FileExtractionTask> fileExtractionTasks = new ArrayList<>();
151  for (AbstractFile source : uniqueFiles) {
152  // If there is an attribute name, change the ":". Otherwise the extracted file will be hidden
153  fileExtractionTasks.add(new FileExtractionTask(source, new File(destinationFolder, source.getId() + "-" + FileUtil.escapeFileName(source.getName()))));
154  }
155  runExtractionTasks(event, fileExtractionTasks, destinationFolder.getName());
156  }
157  }
158 
166  private String getExportDirectory(Case openCase) {
167  String caseExportPath = openCase.getExportDirectory();
168 
169  if (userDefinedExportPath == null) {
170  return caseExportPath;
171  }
172 
173  File file = new File(userDefinedExportPath);
174  if (file.exists() == false || file.isDirectory() == false) {
175  return caseExportPath;
176  }
177 
178  return userDefinedExportPath;
179  }
180 
190  private void updateExportDirectory(String exportPath, Case openCase) {
191  if (exportPath.equalsIgnoreCase(openCase.getExportDirectory())) {
192  userDefinedExportPath = null;
193  } else {
194  userDefinedExportPath = exportPath;
195  }
196  }
197 
207  private void runExtractionTasks(ActionEvent event, List<FileExtractionTask> fileExtractionTasks, String destName) {
208 
209  // verify all of the sources and destinations are OK
210  for (Iterator<FileExtractionTask> it = fileExtractionTasks.iterator(); it.hasNext();) {
211  FileExtractionTask task = it.next();
212 
213  if (ContentUtils.isDotDirectory(task.source)) {
214  it.remove();
215  continue;
216  }
217 
218  /*
219  * This code assumes that each destination is unique. We previously
220  * satisfied that by adding the unique ID.
221  */
222  if (task.destination.exists()) {
223  if (JOptionPane.showConfirmDialog((Component) event.getSource(),
224  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.confDlg.destFileExist.msg", task.destination.getAbsolutePath()),
225  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.confDlg.destFileExist.title"),
226  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
227  if (!FileUtil.deleteFileDir(task.destination)) {
228  JOptionPane.showMessageDialog((Component) event.getSource(),
229  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.msgDlg.cantOverwriteFile.msg", task.destination.getAbsolutePath()));
230  it.remove();
231  }
232  } else {
233  it.remove();
234  }
235  }
236  }
237 
238  // launch a thread to do the work
239  if (!fileExtractionTasks.isEmpty()) {
240  try {
241  FileExtracter extracter = new FileExtracter(fileExtractionTasks, destName);
242  extracter.execute();
243  } catch (Exception ex) {
244  logger.log(Level.WARNING, "Unable to start background file extraction thread", ex); //NON-NLS
245  }
246  } else {
248  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.notifyDlg.noFileToExtr.msg"));
249  }
250  }
251 
255  private class FileExtractionTask {
256 
257  AbstractFile source;
258  File destination;
259 
266  FileExtractionTask(AbstractFile source, File destination) {
267  this.source = source;
268  this.destination = destination;
269  }
270  }
271 
275  private class FileExtracter extends SwingWorker<Object, Void> {
276 
277  private final Logger logger = Logger.getLogger(FileExtracter.class.getName());
278  private final String destName;
279  private ProgressHandle progress;
280  private final List<FileExtractionTask> extractionTasks;
281 
289  FileExtracter(List<FileExtractionTask> extractionTasks, String destName) {
290  this.extractionTasks = extractionTasks;
291  this.destName = destName;
292  }
293 
294  @Override
295  @Messages({
296  "# {0} - outputFolderName",
297  "ExtractActionHelper.progress.extracting=Extracting to {0}",
298  "# {0} - fileName",
299  "ExtractActionHelper.progress.fileExtracting=Extracting file: {0}"
300  })
301  protected Object doInBackground() throws Exception {
302  if (extractionTasks.isEmpty()) {
303  return null;
304  }
305 
306  // Setup progress bar.
307  final String displayName = Bundle.ExtractActionHelper_progress_extracting(destName);
308  progress = ProgressHandle.createHandle(displayName, new Cancellable() {
309  @Override
310  public boolean cancel() {
311  if (progress != null) {
312  progress.setDisplayName(
313  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.progress.cancellingExtraction", displayName));
314  }
315  return ExtractActionHelper.FileExtracter.this.cancel(true);
316  }
317  });
318  progress.start();
319  progress.switchToIndeterminate();
320 
321  // Do the extraction tasks.
322  for (FileExtractionTask task : this.extractionTasks) {
323  progress.progress(Bundle.ExtractActionHelper_progress_fileExtracting(task.destination.getName()));
324 
325  ContentUtils.ExtractFscContentVisitor.extract(task.source, task.destination, null, this);
326  }
327 
328  return null;
329  }
330 
331  @Override
332  protected void done() {
333  boolean msgDisplayed = false;
334  try {
335  super.get();
336  } catch (InterruptedException | ExecutionException ex) {
337  logger.log(Level.SEVERE, "Fatal error during file extraction", ex); //NON-NLS
339  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.done.notifyMsg.extractErr", ex.getMessage()));
340  msgDisplayed = true;
341  } finally {
342  progress.finish();
343  if (!this.isCancelled() && !msgDisplayed) {
345  NbBundle.getMessage(this.getClass(), "ExtractActionHelper.done.notifyMsg.fileExtr.text"));
346  }
347  }
348  }
349 
358  /*
359  * private int calculateProgressBarWorkUnits(AbstractFile file) { int
360  * workUnits = 0; if (file.isFile()) { workUnits += file.getSize(); }
361  * else { try { for (Content child : file.getChildren()) { if (child
362  * instanceof AbstractFile) { workUnits +=
363  * calculateProgressBarWorkUnits((AbstractFile) child); } } } catch
364  * (TskCoreException ex) { logger.log(Level.SEVERE, "Could not get
365  * children of content", ex); //NON-NLS } } return workUnits; }
366  */
367  }
368 }
void extractFiles(ActionEvent event, Collection<?extends AbstractFile > selectedFiles)
void runExtractionTasks(ActionEvent event, List< FileExtractionTask > fileExtractionTasks, String destName)
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 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-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.