mirror of
https://github.com/RipMeApp/ripme.git
synced 2025-09-03 19:02:47 +02:00
workingdir now java.nio
This commit is contained in:
@@ -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<Path> 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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);
|
||||
Stream<Path> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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,14 +250,10 @@ 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()) {
|
||||
Path file = getJarDirectory().resolve(CONFIG_FILE);
|
||||
if (Files.exists(file) && !Files.isDirectory(file)) {
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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 ".";
|
||||
}
|
||||
|
Reference in New Issue
Block a user