sqlite_filechooser suggests file name

This commit is contained in:
rishwanth1995 2018-06-28 11:50:25 -04:00
parent 0db86a108d
commit b4dc29c67e

View File

@ -56,7 +56,6 @@ import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.SleuthkitCase;
import org.sleuthkit.datamodel.TskCoreException;
import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil;
import org.sleuthkit.autopsy.coreutils.TimeStampUtils;
/**
* A file content viewer for SQLite database files.
@ -289,28 +288,22 @@ class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setFileFilter(csvFilter);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
String defaultFileName = (String) this.tablesDropdownList.getSelectedItem();
fileChooser.setSelectedFile(new File(defaultFileName));
int choice = fileChooser.showSaveDialog((Component) evt.getSource()); //TODO
if (JFileChooser.APPROVE_OPTION == choice) {
boolean overwrite = false;
File file = fileChooser.getSelectedFile();
if (file == null) {
JOptionPane.showMessageDialog(this,
Bundle.SQLiteViewer_csvExport_fileName_empty(),
Bundle.SQLiteViewer_csvExport_title(),
JOptionPane.WARNING_MESSAGE);
return;
} else if (file.exists() && FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("csv")) {
if (file.exists() && FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("csv")) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
Bundle.SQLiteViewer_csvExport_confirm_msg(),
Bundle.SQLiteViewer_csvExport_title(),
JOptionPane.YES_NO_OPTION)) {
overwrite = true;
} else {
return;
}
}
exportTableToCsv(file, overwrite);
exportTableToCsv(file);
}
}//GEN-LAST:event_exportCsvButtonActionPerformed
@ -567,9 +560,9 @@ class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
"SQLiteViewer.exportTableToCsv.FileName=File name: ",
"SQLiteViewer.exportTableToCsv.TableName=Table name: "
})
private void exportTableToCsv(File file, boolean overwrite) {
private void exportTableToCsv(File file) {
String tableName = (String) this.tablesDropdownList.getSelectedItem();
String csvFileSuffix = "_" + tableName + "_" + TimeStampUtils.createTimeStamp() + ".csv";
FileOutputStream out = null;
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName)) {
@ -578,16 +571,15 @@ class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
if (Objects.isNull(currentTableRows) || currentTableRows.isEmpty()) {
logger.log(Level.INFO, String.format("The table %s is empty. (objId=%d)", tableName, sqliteDbFile.getId())); //NON-NLS
} else {
String fileName = file.getName();
File csvFile;
if (overwrite) {
String fileName = file.getName();
if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("csv")) {
csvFile = file;
} else if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("csv")) {
csvFile = new File(file.getParentFile(), FilenameUtils.removeExtension(fileName) + csvFileSuffix);
} else {
csvFile = new File(file.toString() + csvFileSuffix);
csvFile = new File(file.toString() + ".csv");
}
FileOutputStream out = new FileOutputStream(csvFile, false);
out = new FileOutputStream(csvFile, false);
out.write((Bundle.SQLiteViewer_exportTableToCsv_FileName() + csvFile.getName() + "\n").getBytes());
out.write((Bundle.SQLiteViewer_exportTableToCsv_TableName() + tableName + "\n").getBytes());
@ -622,6 +614,14 @@ class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
} catch (IOException ex) {
logger.log(Level.SEVERE, String.format("Failed to export table %s to file '%s'", tableName, file.getName()), ex); //NON-NLS
MessageNotifyUtil.Message.error(Bundle.SQLiteViewer_exportTableToCsv_write_errText());
}finally{
try {
if (out != null){
out.close();
}
} catch (IOException ex) {
logger.log(Level.WARNING,String.format("unable to properly close the %s after writing to it", file.getName()),ex);
}
}
}