diff --git a/src/main/java/com/rarchives/ripme/App.java b/src/main/java/com/rarchives/ripme/App.java index c933dec4..5456a312 100644 --- a/src/main/java/com/rarchives/ripme/App.java +++ b/src/main/java/com/rarchives/ripme/App.java @@ -1,29 +1,5 @@ package com.rarchives.ripme; -import java.awt.*; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; - -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; - -import javax.swing.SwingUtilities; - -import org.apache.commons.cli.BasicParser; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.apache.commons.lang.SystemUtils; - import com.rarchives.ripme.ripper.AbstractRipper; import com.rarchives.ripme.ui.History; import com.rarchives.ripme.ui.HistoryEntry; @@ -32,9 +8,30 @@ import com.rarchives.ripme.ui.UpdateUtils; import com.rarchives.ripme.utils.Proxy; import com.rarchives.ripme.utils.RipUtils; import com.rarchives.ripme.utils.Utils; +import org.apache.commons.cli.BasicParser; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.lang.SystemUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import javax.swing.*; +import java.awt.*; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.stream.Stream; + /** * Entry point to application. * This is where all the fun happens, with the main method. @@ -54,7 +51,7 @@ public class App { * * @param args Array of command line arguments. */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { CommandLine cl = getArgs(args); if (args.length > 0 && cl.hasOption('v')){ @@ -122,7 +119,7 @@ public class App { * For dealing with command-line arguments. * @param args Array of Command-line arguments */ - private static void handleArguments(String[] args) { + private static void handleArguments(String[] args) throws IOException { CommandLine cl = getArgs(args); //Help (list commands) @@ -350,7 +347,7 @@ public class App { /** * Loads history from history file into memory. */ - private static void loadHistory() { + private static void loadHistory() throws IOException { Path historyFile = Paths.get(Utils.getConfigDir() + "/history.json"); HISTORY.clear(); if (Files.exists(historyFile)) { @@ -371,17 +368,18 @@ public class App { if (HISTORY.toList().isEmpty()) { // Loaded from config, still no entries. // Guess rip history based on rip folder - String[] dirs = Utils.getWorkingDirectory().list((dir, file) -> new File(dir.getAbsolutePath() + File.separator + file).isDirectory()); - assert dirs != null; - for (String dir : dirs) { - String url = RipUtils.urlFromDirectoryName(dir); + Stream stream = Files.list(Utils.getWorkingDirectory()) + .filter(Files::isDirectory); + + stream.forEach(dir -> { + String url = RipUtils.urlFromDirectoryName(dir.toString()); if (url != null) { // We found one, add it to history HistoryEntry entry = new HistoryEntry(); entry.url = url; HISTORY.add(entry); } - } + }); } } } diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java index 30784f59..5afaf936 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java @@ -6,6 +6,7 @@ import java.io.FileWriter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -436,7 +437,9 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { */ @Override public void setWorkingDir(URL url) throws IOException { - String path = Utils.getWorkingDirectory().getCanonicalPath(); + Path wd = Utils.getWorkingDirectory(); + // TODO - change to nio + String path = wd.toAbsolutePath().toString(); if (!path.endsWith(File.separator)) { path += File.separator; } diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractJSONRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractJSONRipper.java index b63c44c3..6be88472 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractJSONRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractJSONRipper.java @@ -10,6 +10,7 @@ import java.io.FileWriter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -275,7 +276,9 @@ public abstract class AbstractJSONRipper extends AbstractRipper { */ @Override public void setWorkingDir(URL url) throws IOException { - String path = Utils.getWorkingDirectory().getCanonicalPath(); + Path wd = Utils.getWorkingDirectory(); + // TODO - change to nio + String path = wd.toAbsolutePath().toString(); if (!path.endsWith(File.separator)) { path += File.separator; } diff --git a/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java b/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java index f245ba62..554c0e54 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java @@ -5,6 +5,7 @@ import java.io.FileWriter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -184,7 +185,9 @@ public abstract class AlbumRipper extends AbstractRipper { */ @Override public void setWorkingDir(URL url) throws IOException { - String path = Utils.getWorkingDirectory().getCanonicalPath(); + Path wd = Utils.getWorkingDirectory(); + // TODO - change to nio + String path = wd.toAbsolutePath().toString(); if (!path.endsWith(File.separator)) { path += File.separator; } diff --git a/src/main/java/com/rarchives/ripme/ripper/VideoRipper.java b/src/main/java/com/rarchives/ripme/ripper/VideoRipper.java index 4fb0f32a..9e935d62 100644 --- a/src/main/java/com/rarchives/ripme/ripper/VideoRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/VideoRipper.java @@ -9,6 +9,7 @@ import java.io.FileWriter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.Map; @@ -83,7 +84,9 @@ public abstract class VideoRipper extends AbstractRipper { */ @Override public void setWorkingDir(URL url) throws IOException { - String path = Utils.getWorkingDirectory().getCanonicalPath(); + Path wd = Utils.getWorkingDirectory(); + // TODO - change to nio + String path = wd.toAbsolutePath().toString(); if (!path.endsWith(File.separator)) { path += File.separator; diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index dbe16245..430c5460 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -1,54 +1,5 @@ package com.rarchives.ripme.ui; -import java.awt.*; -import java.awt.TrayIcon.MessageType; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.*; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.*; -import java.util.List; - -import javax.imageio.ImageIO; -import javax.swing.DefaultListModel; -import javax.swing.ImageIcon; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JFileChooser; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JProgressBar; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.JTextField; -import javax.swing.JTextPane; -import javax.swing.ListSelectionModel; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.border.EmptyBorder; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.ListDataEvent; -import javax.swing.event.ListDataListener; -import javax.swing.table.AbstractTableModel; -import javax.swing.text.BadLocationException; -import javax.swing.text.SimpleAttributeSet; -import javax.swing.text.StyleConstants; -import javax.swing.text.StyledDocument; - import com.rarchives.ripme.ripper.AbstractRipper; import com.rarchives.ripme.utils.RipUtils; import com.rarchives.ripme.utils.Utils; @@ -59,7 +10,42 @@ import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; -import javax.swing.UnsupportedLookAndFeelException; +import javax.imageio.ImageIO; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.ListDataEvent; +import javax.swing.event.ListDataListener; +import javax.swing.table.AbstractTableModel; +import javax.swing.text.BadLocationException; +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.StyleConstants; +import javax.swing.text.StyledDocument; +import java.awt.*; +import java.awt.TrayIcon.MessageType; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.Date; +import java.util.Enumeration; +import java.util.List; +import java.util.stream.Stream; /** * Everything UI-related starts and ends here. @@ -176,7 +162,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { queueListModel.addElement(url); } - public MainWindow() { + public MainWindow() throws IOException { mainFrame = new JFrame("RipMe v" + UpdateUtils.getThisJarVersion()); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setLayout(new GridBagLayout()); @@ -548,7 +534,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { setLogLevel(configLogLevelCombobox.getSelectedItem().toString()); configSaveDirLabel = new JLabel(); try { - String workingDir = (Utils.shortenPath(Utils.getWorkingDirectory())); + String workingDir = (Utils.shortenPath(Utils.getWorkingDirectory().toString())); configSaveDirLabel.setText(workingDir); configSaveDirLabel.setForeground(Color.BLUE); configSaveDirLabel.setCursor(new Cursor(Cursor.HAND_CURSOR)); @@ -883,17 +869,23 @@ public final class MainWindow implements Runnable, RipStatusHandler { configSaveDirLabel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - File file = new File(Utils.getWorkingDirectory().toString()); - Desktop desktop = Desktop.getDesktop(); + Path file = null; try { - desktop.open(file); - } catch (Exception e1) { + file = Utils.getWorkingDirectory(); + Desktop desktop = Desktop.getDesktop(); + desktop.open(file.toFile()); + } catch (IOException ex) { } } }); configSaveDirButton.addActionListener(arg0 -> { UIManager.put("FileChooser.useSystemExtensionHiding", false); - JFileChooser jfc = new JFileChooser(Utils.getWorkingDirectory()); + JFileChooser jfc = null; + try { + jfc = new JFileChooser(Utils.getWorkingDirectory().toString()); + } catch (IOException e) { + e.printStackTrace(); + } jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = jfc.showDialog(null, "select directory"); if (returnVal != JFileChooser.APPROVE_OPTION) { @@ -912,7 +904,12 @@ public final class MainWindow implements Runnable, RipStatusHandler { }); configUrlFileChooserButton.addActionListener(arg0 -> { UIManager.put("FileChooser.useSystemExtensionHiding", false); - JFileChooser jfc = new JFileChooser(Utils.getWorkingDirectory()); + JFileChooser jfc = null; + try { + jfc = new JFileChooser(Utils.getWorkingDirectory().toAbsolutePath().toString()); + } catch (IOException e) { + e.printStackTrace(); + } jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); int returnVal = jfc.showDialog(null, "Open"); if (returnVal != JFileChooser.APPROVE_OPTION) { @@ -1157,7 +1154,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { LOGGER.error(line); } - private void loadHistory() { + private void loadHistory() throws IOException { File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json"); HISTORY.clear(); if (historyFile.exists()) { @@ -1177,19 +1174,18 @@ public final class MainWindow implements Runnable, RipStatusHandler { if (HISTORY.toList().isEmpty()) { // Loaded from config, still no entries. // Guess rip history based on rip folder - String[] dirs = Utils.getWorkingDirectory() - .list((dir, file) -> new File(dir.getAbsolutePath() + File.separator + file).isDirectory()); - if (dirs != null) { - for (String dir : dirs) { - String url = RipUtils.urlFromDirectoryName(dir); - if (url != null) { - // We found one, add it to history - HistoryEntry entry = new HistoryEntry(); - entry.url = url; - HISTORY.add(entry); - } + Stream stream = Files.list(Utils.getWorkingDirectory()) + .filter(Files::isDirectory); + + stream.forEach(dir -> { + String url = RipUtils.urlFromDirectoryName(dir.toString()); + if (url != null) { + // We found one, add it to history + HistoryEntry entry = new HistoryEntry(); + entry.url = url; + HISTORY.add(entry); } - } + }); } } } diff --git a/src/main/java/com/rarchives/ripme/utils/Utils.java b/src/main/java/com/rarchives/ripme/utils/Utils.java index 7352c4ac..6edcc71e 100644 --- a/src/main/java/com/rarchives/ripme/utils/Utils.java +++ b/src/main/java/com/rarchives/ripme/utils/Utils.java @@ -109,21 +109,16 @@ public class Utils { * * @return Root directory to save rips to. */ - public static File getWorkingDirectory() { - String currentDir = ""; - try { - currentDir = getJarDirectory().getCanonicalPath() + File.separator + RIP_DIRECTORY + File.separator; - } catch (IOException e) { - LOGGER.error("Error while finding working dir: ", e); - } + public static Path getWorkingDirectory() throws IOException { + String currentDir = getJarDirectory() + File.separator + RIP_DIRECTORY + File.separator; if (config != null) { currentDir = getConfigString("rips.directory", currentDir); } - File workingDir = new File(currentDir); - if (!workingDir.exists()) { - workingDir.mkdirs(); + Path workingDir = Paths.get(currentDir); + if (!Files.exists(workingDir)) { + Files.createDirectory(workingDir); } return workingDir; } @@ -240,13 +235,13 @@ public class Utils { + File.separator + "ripme"; } - private static File getJarDirectory() { - File jarDirectory = Objects.requireNonNull(Utils.class.getResource("/rip.properties")).toString().contains("jar:") - ? new File(System.getProperty("java.class.path")).getParentFile() - : new File(System.getProperty("user.dir")); + private static Path getJarDirectory() { + Path jarDirectory = Objects.requireNonNull(Utils.class.getResource("/rip.properties")).toString().contains("jar:") + ? Paths.get(System.getProperty("java.class.path")).getParent() + : Paths.get(System.getProperty("user.dir")); if (jarDirectory == null) - jarDirectory = new File("."); + jarDirectory = Paths.get("."); return jarDirectory; } @@ -255,13 +250,9 @@ public class Utils { * Determines if the app is running in a portable mode. i.e. on a USB stick */ private static boolean portableMode() { - try { - File file = new File(getJarDirectory().getCanonicalPath() + File.separator + CONFIG_FILE); - if (file.exists() && !file.isDirectory()) { - return true; - } - } catch (IOException e) { - return false; + Path file = getJarDirectory().resolve(CONFIG_FILE); + if (Files.exists(file) && !Files.isDirectory(file)) { + return true; } return false; @@ -273,7 +264,7 @@ public class Utils { public static String getConfigDir() { if (portableMode()) { try { - return getJarDirectory().getCanonicalPath(); + return getJarDirectory().toAbsolutePath().toString(); } catch (Exception e) { return "."; } @@ -287,7 +278,7 @@ public class Utils { return getUnixConfigDir(); try { - return getJarDirectory().getCanonicalPath(); + return getJarDirectory().toAbsolutePath().toString(); } catch (Exception e) { return "."; }