Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExternalViewerAction.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2011-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 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 */
19package org.sleuthkit.autopsy.directorytree;
20
21import org.sleuthkit.autopsy.coreutils.Desktop;
22import java.awt.event.ActionEvent;
23import java.io.File;
24import java.io.IOException;
25import java.nio.file.Files;
26import java.nio.file.Path;
27import java.nio.file.Paths;
28import java.net.URI;
29import java.net.URISyntaxException;
30import java.util.logging.Level;
31import javax.swing.AbstractAction;
32import javax.swing.JOptionPane;
33import org.openide.nodes.Node;
34import org.openide.util.NbBundle.Messages;
35import org.openide.windows.WindowManager;
36import org.sleuthkit.autopsy.casemodule.Case;
37import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
38import org.sleuthkit.autopsy.coreutils.Logger;
39import org.sleuthkit.autopsy.datamodel.ContentUtils;
40import org.sleuthkit.autopsy.datamodel.SlackFileNode;
41import org.sleuthkit.datamodel.AbstractFile;
42
48public class ExternalViewerAction extends AbstractAction {
49
50 private final static Logger logger = Logger.getLogger(ExternalViewerAction.class.getName());
51 private final AbstractFile fileObject;
52 private String fileObjectExt;
53 final static String[] EXECUTABLE_EXT = {".exe", ".dll", ".com", ".bat", ".msi", ".reg", ".scr", ".cmd"}; //NON-NLS
54 private boolean isExecutable;
55
56 ExternalViewerAction(String title, AbstractFile file, boolean isSlackFile) {
57 super(title);
58 this.fileObject = file;
59
60 long size = fileObject.getSize();
61 String fileName = fileObject.getName();
62 int extPos = fileName.lastIndexOf('.');
63
64 isExecutable = false;
65 if (extPos != -1) {
66 String extension = fileName.substring(extPos, fileName.length()).toLowerCase();
67 fileObjectExt = extension;
68 for (int i = 0; i < EXECUTABLE_EXT.length; ++i) {
69 if (EXECUTABLE_EXT[i].equals(extension)) {
70 isExecutable = true;
71 break;
72 }
73 }
74 } else {
75 fileObjectExt = "";
76 }
77
78 // no point opening a file if it's empty, and java doesn't know how to
79 // find an application for files without an extension
80 // or if file is executable (for security reasons)
81 // Also skip slack files since their extension is the original extension + "-slack"
82 if (!(size > 0) || extPos == -1 || isExecutable || isSlackFile) {
83 this.setEnabled(false);
84 }
85 }
86
92 public ExternalViewerAction (String title, Node fileNode) {
93 this(title, fileNode.getLookup().lookup(org.sleuthkit.datamodel.AbstractFile.class), fileNode instanceof SlackFileNode);
94 }
95
96 @Override
97 @Messages({
98 "# {0} - file name",
99 "ExternalViewerAction.actionPerformed.failure.title=Open File Failure {0}",
100 "ExternalViewerAction.actionPerformed.failure.exe.message=The file is an executable and will not be opened."
101 })
102 public void actionPerformed(ActionEvent e) {
103 if (isExecutable) {
104 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
105 Bundle.ExternalViewerAction_actionPerformed_failure_exe_message(),
106 Bundle.ExternalViewerAction_actionPerformed_failure_title(this.fileObject.getName()),
107 JOptionPane.ERROR_MESSAGE);
108 return;
109 }
110
111 // Get the temp folder path of the case
112 Case openCase;
113 try {
114 openCase = Case.getCurrentCaseThrows();
115 } catch (NoCurrentCaseException ex) {
116 logger.log(Level.WARNING, "Exception while getting open case.", ex); //NON-NLS
117 return;
118 }
119 // Create a temp file atomically to avoid TOCTOU race conditions.
120 // fileObjectExt already includes the leading dot (e.g. ".pdf"), or is empty.
121 Path tempFilePath;
122 try {
123 tempFilePath = Files.createTempFile(
124 Paths.get(openCase.getTempDirectory()),
125 this.fileObject.getName(),
127 } catch (IOException ex) {
128 logger.log(Level.WARNING, "Can't create temporary file.", ex); //NON-NLS
129 return;
130 }
131 File tempFile = tempFilePath.toFile();
132 tempFile.deleteOnExit();
133
134 try {
136 } catch (IOException ex) {
137 logger.log(Level.WARNING, "Can't save to temporary file.", ex); //NON-NLS
138 }
139
140 ExternalViewerAction.openFile(fileObject.getMIMEType(), fileObjectExt, tempFile);
141 }
142
151 @Messages({
152 "ExternalViewerAction.actionPerformed.failure.IO.message=There is no associated editor for files of this type or the associated application failed to launch.",
153 "ExternalViewerAction.actionPerformed.failure.support.message=This platform (operating system) does not support opening a file in an editor this way.",
154 "ExternalViewerAction.actionPerformed.failure.missingFile.message=The file no longer exists.",
155 "ExternalViewerAction.actionPerformed.failure.permission.message=Permission to open the file was denied.",
156 "ExternalViewerAction.actionPerformed.failure.open.url=Cannot open URL"})
157 public static void openFile(String mimeType, String ext, File file) {
162 String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(mimeType);
163 if (exePath.equals("")) {
164 exePath = ExternalViewerRulesManager.getInstance().getExePathForName(ext);
165 }
166 if (!exePath.equals("")) {
167 Runtime runtime = Runtime.getRuntime();
168 String[] execArray = new String[]{exePath, file.getAbsolutePath()};
169 try {
170 runtime.exec(execArray);
171 } catch (IOException ex) {
172 logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + file.getName(), ex); //NON-NLS
173 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()), JOptionPane.ERROR_MESSAGE);
174 }
175 } else {
176 try {
177 String localpath = file.getPath();
178 if (localpath.toLowerCase().contains("http")) {
179 String url_path = file.getPath().replaceAll("\\\\", "/");
180 Desktop.getDesktop().browse(new URI(url_path.replaceFirst("/", "//")));
181 } else {
182 Desktop.getDesktop().open(file);
183 }
184
185 } catch (IOException ex) {
186 logger.log(Level.WARNING, "Could not find a viewer for the given file: " + file.getName(), ex); //NON-NLS
187 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
188 Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
189 Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()),
190 JOptionPane.ERROR_MESSAGE);
191 } catch (UnsupportedOperationException ex) {
192 logger.log(Level.WARNING, "Platform cannot open " + file.getName() + " in the defined editor.", ex); //NON-NLS
193 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
194 Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
195 Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()),
196 JOptionPane.ERROR_MESSAGE);
197 } catch (IllegalArgumentException ex) {
198 logger.log(Level.WARNING, "Could not find the given file: " + file.getName(), ex); //NON-NLS
199 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
200 Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
201 Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()),
202 JOptionPane.ERROR_MESSAGE);
203 } catch (SecurityException ex) {
204 logger.log(Level.WARNING, "Could not get permission to open the given file: " + file.getName(), ex); //NON-NLS
205 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
206 Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
207 Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()),
208 JOptionPane.ERROR_MESSAGE);
209 } catch (URISyntaxException ex) {
210 logger.log(Level.WARNING, "Could not open URL provided: " + file.getPath(), ex);
211 JOptionPane.showMessageDialog(null,
212 Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
213 Bundle.ExternalViewerAction_actionPerformed_failure_title(file.getName()),
214 JOptionPane.ERROR_MESSAGE);
215 }
216 }
217 }
218
224 @Messages({
225 "ExternalViewerAction.actionPerformed.urlFailure.title=Open URL Failure"})
226 public static void openURL(String path) {
227 String url_path = path.replaceAll("\\\\", "/");
228 try {
229 Desktop.getDesktop().browse(new URI(url_path.replaceFirst("/", "//")));
230 } catch (IOException ex) {
231 logger.log(Level.WARNING, "Could not find a viewer for the given URL: " + url_path, ex); //NON-NLS
232 JOptionPane.showMessageDialog(null,
233 Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
234 Bundle.ExternalViewerAction_actionPerformed_urlFailure_title(),
235 JOptionPane.ERROR_MESSAGE);
236 } catch (UnsupportedOperationException ex) {
237 logger.log(Level.WARNING, "Platform cannot open " + url_path + " in the defined editor.", ex); //NON-NLS
238 JOptionPane.showMessageDialog(null,
239 Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
240 Bundle.ExternalViewerAction_actionPerformed_urlFailure_title(),
241 JOptionPane.ERROR_MESSAGE);
242 } catch (IllegalArgumentException ex) {
243 logger.log(Level.WARNING, "Could not find the given URL: " + url_path, ex); //NON-NLS
244 JOptionPane.showMessageDialog(null,
245 Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
246 Bundle.ExternalViewerAction_actionPerformed_urlFailure_title(),
247 JOptionPane.ERROR_MESSAGE);
248 } catch (SecurityException ex) {
249 logger.log(Level.WARNING, "Could not get permission to open the given URL: " + url_path, ex); //NON-NLS
250 JOptionPane.showMessageDialog(null,
251 Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
252 Bundle.ExternalViewerAction_actionPerformed_urlFailure_title(),
253 JOptionPane.ERROR_MESSAGE);
254 } catch (URISyntaxException ex) {
255 logger.log(Level.WARNING, "Could not open URL provided: " + url_path, ex);
256 JOptionPane.showMessageDialog(null,
257 Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
258 Bundle.ExternalViewerAction_actionPerformed_urlFailure_title(),
259 JOptionPane.ERROR_MESSAGE);
260 }
261 }
262}
synchronized static Logger getLogger(String name)
Definition Logger.java:124
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static void openFile(String mimeType, String ext, File file)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.