19 package org.sleuthkit.autopsy.directorytree;
21 import java.awt.Desktop;
22 import java.awt.event.ActionEvent;
24 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import java.util.logging.Level;
28 import javax.swing.AbstractAction;
29 import javax.swing.JOptionPane;
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle.Messages;
32 import org.openide.windows.WindowManager;
49 final static String[] EXECUTABLE_EXT = {
".exe",
".dll",
".com",
".bat",
".msi",
".reg",
".scr",
".cmd"};
62 int extPos = fileName.lastIndexOf(
'.');
64 boolean isExecutable =
false;
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)) {
82 if (!(size > 0) || extPos == -1 || isExecutable || (fileNode instanceof
SlackFileNode)) {
83 this.setEnabled(
false);
94 logger.log(Level.WARNING,
"Exception while getting open case.", ex);
98 tempPath = tempPath + File.separator + this.
fileObject.getName();
101 File tempFile =
new File(tempPath);
102 if (tempFile.exists()) {
106 tempFile.createNewFile();
108 }
catch (IOException ex) {
109 logger.log(Level.WARNING,
"Can't save to temporary file.", ex);
115 tempFile.deleteOnExit();
127 "ExternalViewerAction.actionPerformed.failure.title=Open File Failure",
128 "ExternalViewerAction.actionPerformed.failure.IO.message=There is no associated editor for files of this type or the associated application failed to launch.",
129 "ExternalViewerAction.actionPerformed.failure.support.message=This platform (operating system) does not support opening a file in an editor this way.",
130 "ExternalViewerAction.actionPerformed.failure.missingFile.message=The file no longer exists.",
131 "ExternalViewerAction.actionPerformed.failure.permission.message=Permission to open the file was denied.",
132 "ExternalViewerAction.actionPerformed.failure.open.url=Cannot open URL"})
133 public static void openFile(String mimeType, String ext, File file) {
138 String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(mimeType);
139 if (exePath.equals(
"")) {
140 exePath = ExternalViewerRulesManager.getInstance().getExePathForName(ext);
142 if (!exePath.equals(
"")) {
143 Runtime runtime = Runtime.getRuntime();
144 String[] execArray =
new String[]{exePath, file.getAbsolutePath()};
146 runtime.exec(execArray);
147 }
catch (IOException ex) {
148 logger.log(Level.WARNING,
"Could not open the specified viewer for the given file: " + file.getName(), ex);
149 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
153 String localpath = file.getPath();
154 if (localpath.toLowerCase().contains(
"http")) {
155 String url_path = file.getPath().replaceAll(
"\\\\",
"/");
156 Desktop.getDesktop().browse(
new URI(url_path.replaceFirst(
"/",
"//")));
158 Desktop.getDesktop().open(file);
161 }
catch (IOException ex) {
162 logger.log(Level.WARNING,
"Could not find a viewer for the given file: " + file.getName(), ex);
163 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
164 Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
165 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
166 JOptionPane.ERROR_MESSAGE);
167 }
catch (UnsupportedOperationException ex) {
168 logger.log(Level.WARNING,
"Platform cannot open " + file.getName() +
" in the defined editor.", ex);
169 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
170 Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
171 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
172 JOptionPane.ERROR_MESSAGE);
173 }
catch (IllegalArgumentException ex) {
174 logger.log(Level.WARNING,
"Could not find the given file: " + file.getName(), ex);
175 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
176 Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
177 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
178 JOptionPane.ERROR_MESSAGE);
179 }
catch (SecurityException ex) {
180 logger.log(Level.WARNING,
"Could not get permission to open the given file: " + file.getName(), ex);
181 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
182 Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
183 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
184 JOptionPane.ERROR_MESSAGE);
185 }
catch (URISyntaxException ex) {
186 logger.log(Level.WARNING,
"Could not open URL provided: " + file.getPath(), ex);
187 JOptionPane.showMessageDialog(null,
188 Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
189 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
190 JOptionPane.ERROR_MESSAGE);
201 String url_path = path.replaceAll(
"\\\\",
"/");
203 Desktop.getDesktop().browse(
new URI(url_path.replaceFirst(
"/",
"//")));
204 }
catch (IOException ex) {
205 logger.log(Level.WARNING,
"Could not find a viewer for the given URL: " + url_path, ex);
206 JOptionPane.showMessageDialog(null,
207 Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
208 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
209 JOptionPane.ERROR_MESSAGE);
210 }
catch (UnsupportedOperationException ex) {
211 logger.log(Level.WARNING,
"Platform cannot open " + url_path +
" in the defined editor.", ex);
212 JOptionPane.showMessageDialog(null,
213 Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
214 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
215 JOptionPane.ERROR_MESSAGE);
216 }
catch (IllegalArgumentException ex) {
217 logger.log(Level.WARNING,
"Could not find the given URL: " + url_path, ex);
218 JOptionPane.showMessageDialog(null,
219 Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
220 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
221 JOptionPane.ERROR_MESSAGE);
222 }
catch (SecurityException ex) {
223 logger.log(Level.WARNING,
"Could not get permission to open the given URL: " + url_path, ex);
224 JOptionPane.showMessageDialog(null,
225 Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
226 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
227 JOptionPane.ERROR_MESSAGE);
228 }
catch (URISyntaxException ex) {
229 logger.log(Level.WARNING,
"Could not open URL provided: " + url_path, ex);
230 JOptionPane.showMessageDialog(null,
231 Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
232 Bundle.ExternalViewerAction_actionPerformed_failure_title(),
233 JOptionPane.ERROR_MESSAGE);
final org.sleuthkit.datamodel.AbstractFile fileObject
static void openURL(String path)
String getTempDirectory()
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static final Logger logger
static void openFile(String mimeType, String ext, File file)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
ExternalViewerAction(String title, Node fileNode)
void actionPerformed(ActionEvent e)