commit
a1996cc01e
@ -1,87 +1,102 @@
|
|||||||
package com.rarchives.ripme.utils;
|
package com.rarchives.ripme.utils;
|
||||||
|
|
||||||
import java.io.*;
|
import com.rarchives.ripme.ripper.AbstractRipper;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLDecoder;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.jar.JarEntry;
|
|
||||||
import java.util.jar.JarFile;
|
|
||||||
|
|
||||||
import javax.sound.sampled.AudioSystem;
|
|
||||||
import javax.sound.sampled.Clip;
|
|
||||||
import javax.sound.sampled.Line;
|
|
||||||
import javax.sound.sampled.LineEvent;
|
|
||||||
|
|
||||||
import org.apache.commons.configuration.ConfigurationException;
|
import org.apache.commons.configuration.ConfigurationException;
|
||||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||||
import org.apache.log4j.LogManager;
|
import org.apache.log4j.LogManager;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.apache.log4j.PropertyConfigurator;
|
import org.apache.log4j.PropertyConfigurator;
|
||||||
|
|
||||||
import com.rarchives.ripme.ripper.AbstractRipper;
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
import javax.sound.sampled.Clip;
|
||||||
|
import javax.sound.sampled.Line;
|
||||||
|
import javax.sound.sampled.LineEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.MissingResourceException;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common utility functions used in various places throughout the project.
|
* Common utility functions used in various places throughout the project.
|
||||||
*/
|
*/
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
private static final String RIP_DIRECTORY = "rips";
|
private static final String RIP_DIRECTORY = "rips";
|
||||||
private static final String configFile = "rip.properties";
|
private static final String CONFIG_FILE = "rip.properties";
|
||||||
private static final String OS = System.getProperty("os.name").toLowerCase();
|
private static final String OS = System.getProperty("os.name").toLowerCase();
|
||||||
private static final Logger logger = Logger.getLogger(Utils.class);
|
private static final Logger LOGGER = Logger.getLogger(Utils.class);
|
||||||
|
private static final int SHORTENED_PATH_LENGTH = 12;
|
||||||
|
|
||||||
private static PropertiesConfiguration config;
|
private static PropertiesConfiguration config;
|
||||||
|
private static HashMap<String, HashMap<String, String>> cookieCache;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
cookieCache = new HashMap<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String configPath = getConfigFilePath();
|
String configPath = getConfigFilePath();
|
||||||
File f = new File(configPath);
|
File file = new File(configPath);
|
||||||
if (!f.exists()) {
|
|
||||||
|
if (!file.exists()) {
|
||||||
// Use default bundled with .jar
|
// Use default bundled with .jar
|
||||||
configPath = configFile;
|
configPath = CONFIG_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
config = new PropertiesConfiguration(configPath);
|
config = new PropertiesConfiguration(configPath);
|
||||||
logger.info("Loaded " + config.getPath());
|
LOGGER.info("Loaded " + config.getPath());
|
||||||
if (f.exists()) {
|
|
||||||
|
if (file.exists()) {
|
||||||
// Config was loaded from file
|
// Config was loaded from file
|
||||||
if ( !config.containsKey("twitter.auth")
|
if (!config.containsKey("twitter.auth") || !config.containsKey("twitter.max_requests")
|
||||||
|| !config.containsKey("twitter.max_requests")
|
|| !config.containsKey("tumblr.auth") || !config.containsKey("error.skip404")
|
||||||
|| !config.containsKey("tumblr.auth")
|
|| !config.containsKey("gw.api") || !config.containsKey("page.timeout")
|
||||||
|| !config.containsKey("error.skip404")
|
|| !config.containsKey("download.max_size")) {
|
||||||
|| !config.containsKey("gw.api")
|
|
||||||
|| !config.containsKey("page.timeout")
|
|
||||||
|| !config.containsKey("download.max_size")
|
|
||||||
) {
|
|
||||||
// Config is missing key fields
|
// Config is missing key fields
|
||||||
// Need to reload the default config
|
// Need to reload the default config
|
||||||
// See https://github.com/4pr0n/ripme/issues/158
|
// See https://github.com/4pr0n/ripme/issues/158
|
||||||
logger.warn("Config does not contain key fields, deleting old config");
|
LOGGER.warn("Config does not contain key fields, deleting old config");
|
||||||
f.delete();
|
file.delete();
|
||||||
config = new PropertiesConfiguration(configFile);
|
config = new PropertiesConfiguration(CONFIG_FILE);
|
||||||
logger.info("Loaded " + config.getPath());
|
LOGGER.info("Loaded " + config.getPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("[!] Failed to load properties file from " + configFile, e);
|
LOGGER.error("[!] Failed to load properties file from " + CONFIG_FILE, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the root rips directory.
|
* Get the root rips directory.
|
||||||
* @return
|
*
|
||||||
* Root directory to save rips to.
|
* @return Root directory to save rips to.
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
public static File getWorkingDirectory() {
|
public static File getWorkingDirectory() {
|
||||||
String currentDir = ".";
|
String currentDir = ".";
|
||||||
try {
|
try {
|
||||||
currentDir = new File(".").getCanonicalPath() + File.separator + RIP_DIRECTORY + File.separator;
|
currentDir = new File(".").getCanonicalPath() + File.separator + RIP_DIRECTORY + File.separator;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Error while finding working dir: ", e);
|
LOGGER.error("Error while finding working dir: ", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
currentDir = getConfigString("rips.directory", currentDir);
|
currentDir = getConfigString("rips.directory", currentDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
File workingDir = new File(currentDir);
|
File workingDir = new File(currentDir);
|
||||||
if (!workingDir.exists()) {
|
if (!workingDir.exists()) {
|
||||||
workingDir.mkdirs();
|
workingDir.mkdirs();
|
||||||
@ -100,36 +115,46 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getConfigStringArray(String key) {
|
public static String[] getConfigStringArray(String key) {
|
||||||
String[] s = config.getStringArray(key);
|
String[] configStringArray = config.getStringArray(key);
|
||||||
if (s.length == 0) {
|
|
||||||
return null;
|
return configStringArray.length == 0 ? null : configStringArray;
|
||||||
} else {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getConfigInteger(String key, int defaultValue) {
|
public static int getConfigInteger(String key, int defaultValue) {
|
||||||
return config.getInt(key, defaultValue);
|
return config.getInt(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean getConfigBoolean(String key, boolean defaultValue) {
|
public static boolean getConfigBoolean(String key, boolean defaultValue) {
|
||||||
return config.getBoolean(key, defaultValue);
|
return config.getBoolean(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getConfigList(String key) {
|
public static List<String> getConfigList(String key) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
for (Object obj : config.getList(key, new ArrayList<String>())) {
|
for (Object obj : config.getList(key, new ArrayList<String>())) {
|
||||||
if (obj instanceof String) {
|
if (obj instanceof String) {
|
||||||
result.add( (String) obj);
|
result.add((String) obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static void setConfigBoolean(String key, boolean value) { config.setProperty(key, value); }
|
|
||||||
public static void setConfigString(String key, String value) { config.setProperty(key, value); }
|
public static void setConfigBoolean(String key, boolean value) {
|
||||||
public static void setConfigInteger(String key, int value) { config.setProperty(key, value); }
|
config.setProperty(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setConfigString(String key, String value) {
|
||||||
|
config.setProperty(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setConfigInteger(String key, int value) {
|
||||||
|
config.setProperty(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
public static void setConfigList(String key, List<Object> list) {
|
public static void setConfigList(String key, List<Object> list) {
|
||||||
config.clearProperty(key);
|
config.clearProperty(key);
|
||||||
config.addProperty(key, list);
|
config.addProperty(key, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setConfigList(String key, Enumeration<Object> enumeration) {
|
public static void setConfigList(String key, Enumeration<Object> enumeration) {
|
||||||
config.clearProperty(key);
|
config.clearProperty(key);
|
||||||
List<Object> list = new ArrayList<>();
|
List<Object> list = new ArrayList<>();
|
||||||
@ -142,9 +167,9 @@ public class Utils {
|
|||||||
public static void saveConfig() {
|
public static void saveConfig() {
|
||||||
try {
|
try {
|
||||||
config.save(getConfigFilePath());
|
config.save(getConfigFilePath());
|
||||||
logger.info("Saved configuration to " + getConfigFilePath());
|
LOGGER.info("Saved configuration to " + getConfigFilePath());
|
||||||
} catch (ConfigurationException e) {
|
} catch (ConfigurationException e) {
|
||||||
logger.error("Error while saving configuration: ", e);
|
LOGGER.error("Error while saving configuration: ", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +201,6 @@ public class Utils {
|
|||||||
return System.getenv("LOCALAPPDATA") + File.separator + "ripme";
|
return System.getenv("LOCALAPPDATA") + File.separator + "ripme";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the directory of where the config file is stored on a UNIX machine.
|
* Gets the directory of where the config file is stored on a UNIX machine.
|
||||||
*/
|
*/
|
||||||
@ -197,13 +221,14 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
private static boolean portableMode() {
|
private static boolean portableMode() {
|
||||||
try {
|
try {
|
||||||
File f = new File(new File(".").getCanonicalPath() + File.separator + configFile);
|
File file = new File(new File(".").getCanonicalPath() + File.separator + CONFIG_FILE);
|
||||||
if(f.exists() && !f.isDirectory()) {
|
if (file.exists() && !file.isDirectory()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,6 +254,7 @@ public class Utils {
|
|||||||
return ".";
|
return ".";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete the url history file
|
* Delete the url history file
|
||||||
*/
|
*/
|
||||||
@ -248,26 +274,23 @@ public class Utils {
|
|||||||
* Gets the path to the configuration file.
|
* Gets the path to the configuration file.
|
||||||
*/
|
*/
|
||||||
private static String getConfigFilePath() {
|
private static String getConfigFilePath() {
|
||||||
return getConfigDir() + File.separator + configFile;
|
return getConfigDir() + File.separator + CONFIG_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the current working directory (CWD) from a File.
|
* Removes the current working directory (CWD) from a File.
|
||||||
* @param saveAs
|
*
|
||||||
* The File path
|
* @param saveAs The File path
|
||||||
* @return
|
* @return saveAs in relation to the CWD
|
||||||
* saveAs in relation to the CWD
|
|
||||||
*/
|
*/
|
||||||
public static String removeCWD(File saveAs) {
|
public static String removeCWD(File saveAs) {
|
||||||
String prettySaveAs = saveAs.toString();
|
String prettySaveAs = saveAs.toString();
|
||||||
try {
|
try {
|
||||||
prettySaveAs = saveAs.getCanonicalPath();
|
prettySaveAs = saveAs.getCanonicalPath();
|
||||||
String cwd = new File(".").getCanonicalPath() + File.separator;
|
String cwd = new File(".").getCanonicalPath() + File.separator;
|
||||||
prettySaveAs = prettySaveAs.replace(
|
prettySaveAs = prettySaveAs.replace(cwd, "." + File.separator);
|
||||||
cwd,
|
|
||||||
"." + File.separator);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Exception: ", e);
|
LOGGER.error("Exception: ", e);
|
||||||
}
|
}
|
||||||
return prettySaveAs;
|
return prettySaveAs;
|
||||||
}
|
}
|
||||||
@ -278,7 +301,6 @@ public class Utils {
|
|||||||
*
|
*
|
||||||
* @param url The URL to filter/strip
|
* @param url The URL to filter/strip
|
||||||
* @param parameter The parameter to strip
|
* @param parameter The parameter to strip
|
||||||
*
|
|
||||||
* @return The stripped URL
|
* @return The stripped URL
|
||||||
*/
|
*/
|
||||||
public static String stripURLParameter(String url, String parameter) {
|
public static String stripURLParameter(String url, String parameter) {
|
||||||
@ -290,13 +312,13 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (paramIndex > 0) {
|
if (paramIndex > 0) {
|
||||||
int nextParam = url.indexOf("&", paramIndex+1);
|
int nextParam = url.indexOf('&', paramIndex + 1);
|
||||||
if (nextParam != -1) {
|
if (nextParam != -1) {
|
||||||
String c = "&";
|
String c = "&";
|
||||||
if (wasFirstParam) {
|
if (wasFirstParam) {
|
||||||
c = "?";
|
c = "?";
|
||||||
}
|
}
|
||||||
url = url.substring(0, paramIndex) + c + url.substring(nextParam+1, url.length());
|
url = url.substring(0, paramIndex) + c + url.substring(nextParam + 1, url.length());
|
||||||
} else {
|
} else {
|
||||||
url = url.substring(0, paramIndex);
|
url = url.substring(0, paramIndex);
|
||||||
}
|
}
|
||||||
@ -307,10 +329,9 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the current working directory from a given filename
|
* Removes the current working directory from a given filename
|
||||||
* @param file
|
*
|
||||||
* Path to the file
|
* @param file Path to the file
|
||||||
* @return
|
* @return 'file' without the leading current working directory
|
||||||
* 'file' without the leading current working directory
|
|
||||||
*/
|
*/
|
||||||
public static String removeCWD(String file) {
|
public static String removeCWD(String file) {
|
||||||
return removeCWD(new File(file));
|
return removeCWD(new File(file));
|
||||||
@ -320,12 +341,11 @@ public class Utils {
|
|||||||
* Get a list of all Classes within a package.
|
* Get a list of all Classes within a package.
|
||||||
* Works with file system projects and jar files!
|
* Works with file system projects and jar files!
|
||||||
* Borrowed from StackOverflow, but I don't have a link :[
|
* Borrowed from StackOverflow, but I don't have a link :[
|
||||||
* @param pkgname
|
*
|
||||||
* The name of the package
|
* @param pkgname The name of the package
|
||||||
* @return
|
* @return List of classes within the package
|
||||||
* List of classes within the package
|
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Class<?>> getClassesForPackage(String pkgname) {
|
public static List<Class<?>> getClassesForPackage(String pkgname) {
|
||||||
ArrayList<Class<?>> classes = new ArrayList<>();
|
ArrayList<Class<?>> classes = new ArrayList<>();
|
||||||
String relPath = pkgname.replace('.', '/');
|
String relPath = pkgname.replace('.', '/');
|
||||||
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
|
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
|
||||||
@ -334,7 +354,8 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String fullPath = resource.getFile();
|
String fullPath = resource.getFile();
|
||||||
File directory = null;
|
File directory;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
directory = new File(resource.toURI());
|
directory = new File(resource.toURI());
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
@ -356,8 +377,7 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Load from JAR
|
// Load from JAR
|
||||||
try {
|
try {
|
||||||
String jarPath = fullPath
|
String jarPath = fullPath
|
||||||
@ -376,7 +396,7 @@ public class Utils {
|
|||||||
try {
|
try {
|
||||||
classes.add(Class.forName(className));
|
classes.add(Class.forName(className));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
logger.error("ClassNotFoundException loading " + className);
|
LOGGER.error("ClassNotFoundException loading " + className);
|
||||||
jarFile.close(); // Resource leak fix?
|
jarFile.close(); // Resource leak fix?
|
||||||
throw new RuntimeException("ClassNotFoundException loading " + className);
|
throw new RuntimeException("ClassNotFoundException loading " + className);
|
||||||
}
|
}
|
||||||
@ -384,20 +404,18 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
jarFile.close(); // Eclipse said not closing it would have a resource leak
|
jarFile.close(); // Eclipse said not closing it would have a resource leak
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Error while loading jar file:", e);
|
LOGGER.error("Error while loading jar file:", e);
|
||||||
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
|
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int SHORTENED_PATH_LENGTH = 12;
|
|
||||||
/**
|
/**
|
||||||
* Shortens the path to a file
|
* Shortens the path to a file
|
||||||
* @param path
|
*
|
||||||
* String of the path to the file
|
* @param path String of the path to the file
|
||||||
* @return
|
* @return The simplified path to the file.
|
||||||
* The simplified path to the file.
|
|
||||||
*/
|
*/
|
||||||
public static String shortenPath(String path) {
|
public static String shortenPath(String path) {
|
||||||
return shortenPath(new File(path));
|
return shortenPath(new File(path));
|
||||||
@ -405,10 +423,9 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortens the path to a file
|
* Shortens the path to a file
|
||||||
* @param file
|
*
|
||||||
* File object that you want the shortened path of.
|
* @param file File object that you want the shortened path of.
|
||||||
* @return
|
* @return The simplified path to the file.
|
||||||
* The simplified path to the file.
|
|
||||||
*/
|
*/
|
||||||
public static String shortenPath(File file) {
|
public static String shortenPath(File file) {
|
||||||
String path = removeCWD(file);
|
String path = removeCWD(file);
|
||||||
@ -422,10 +439,9 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitizes a string so that a filesystem can handle it
|
* Sanitizes a string so that a filesystem can handle it
|
||||||
* @param text
|
*
|
||||||
* The text to be sanitized.
|
* @param text The text to be sanitized.
|
||||||
* @return
|
* @return The sanitized text.
|
||||||
* The sanitized text.
|
|
||||||
*/
|
*/
|
||||||
public static String filesystemSanitized(String text) {
|
public static String filesystemSanitized(String text) {
|
||||||
text = text.replaceAll("[^a-zA-Z0-9.-]", "_");
|
text = text.replaceAll("[^a-zA-Z0-9.-]", "_");
|
||||||
@ -451,7 +467,7 @@ public class Utils {
|
|||||||
public static String getOriginalDirectory(String path) {
|
public static String getOriginalDirectory(String path) {
|
||||||
|
|
||||||
int index;
|
int index;
|
||||||
if(isUnix() || isMacOS()) {
|
if (isUnix() || isMacOS()) {
|
||||||
index = path.lastIndexOf('/');
|
index = path.lastIndexOf('/');
|
||||||
} else {
|
} else {
|
||||||
// current OS is windows - nothing to do here
|
// current OS is windows - nothing to do here
|
||||||
@ -459,17 +475,17 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String original = path; // needs to be checked if lowercase exists
|
String original = path; // needs to be checked if lowercase exists
|
||||||
String lastPart = original.substring(index+1).toLowerCase(); // setting lowercase to check if it exists
|
String lastPart = original.substring(index + 1).toLowerCase(); // setting lowercase to check if it exists
|
||||||
|
|
||||||
// Get a List of all Directories and check its lowercase
|
// Get a List of all Directories and check its lowercase
|
||||||
// if file exists return it
|
// if file exists return it
|
||||||
File f = new File(path.substring(0, index));
|
File file = new File(path.substring(0, index));
|
||||||
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
|
ArrayList<String> names = new ArrayList<>(Arrays.asList(file.list()));
|
||||||
|
|
||||||
for (String s : names) {
|
for (String name : names) {
|
||||||
if(s.toLowerCase().equals(lastPart)) {
|
if (name.toLowerCase().equals(lastPart)) {
|
||||||
// Building Path of existing file
|
// Building Path of existing file
|
||||||
return path.substring(0, index) + File.separator + s;
|
return path.substring(0, index) + File.separator + name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,14 +494,13 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an integer into a human readable string
|
* Converts an integer into a human readable string
|
||||||
* @param bytes
|
*
|
||||||
* Non-human readable integer.
|
* @param bytes Non-human readable integer.
|
||||||
* @return
|
* @return Human readable interpretation of a byte.
|
||||||
* Human readable interpretation of a byte.
|
|
||||||
*/
|
*/
|
||||||
public static String bytesToHumanReadable(int bytes) {
|
public static String bytesToHumanReadable(int bytes) {
|
||||||
float fbytes = (float) bytes;
|
float fbytes = (float) bytes;
|
||||||
String[] mags = new String[] {"", "K", "M", "G", "T"};
|
String[] mags = new String[]{"", "K", "M", "G", "T"};
|
||||||
int magIndex = 0;
|
int magIndex = 0;
|
||||||
while (fbytes >= 1024) {
|
while (fbytes >= 1024) {
|
||||||
fbytes /= 1024;
|
fbytes /= 1024;
|
||||||
@ -496,6 +511,7 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets and returns a list of all the album rippers present in the "com.rarchives.ripme.ripper.rippers" package.
|
* Gets and returns a list of all the album rippers present in the "com.rarchives.ripme.ripper.rippers" package.
|
||||||
|
*
|
||||||
* @return List<String> of all album rippers present.
|
* @return List<String> of all album rippers present.
|
||||||
*/
|
*/
|
||||||
public static List<String> getListOfAlbumRippers() throws Exception {
|
public static List<String> getListOfAlbumRippers() throws Exception {
|
||||||
@ -508,6 +524,7 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets and returns a list of all video rippers present in the "com.rarchives.rime.rippers.video" package
|
* Gets and returns a list of all video rippers present in the "com.rarchives.rime.rippers.video" package
|
||||||
|
*
|
||||||
* @return List<String> of all the video rippers.
|
* @return List<String> of all the video rippers.
|
||||||
*/
|
*/
|
||||||
public static List<String> getListOfVideoRippers() throws Exception {
|
public static List<String> getListOfVideoRippers() throws Exception {
|
||||||
@ -520,8 +537,8 @@ public class Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays a sound from a file.
|
* Plays a sound from a file.
|
||||||
* @param filename
|
*
|
||||||
* Path to the sound file
|
* @param filename Path to the sound file
|
||||||
*/
|
*/
|
||||||
public static void playSound(String filename) {
|
public static void playSound(String filename) {
|
||||||
URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
|
URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
|
||||||
@ -535,7 +552,7 @@ public class Utils {
|
|||||||
clip.open(AudioSystem.getAudioInputStream(resource));
|
clip.open(AudioSystem.getAudioInputStream(resource));
|
||||||
clip.start();
|
clip.start();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Failed to play sound " + filename, e);
|
LOGGER.error("Failed to play sound " + filename, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -544,27 +561,25 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
public static void configureLogger() {
|
public static void configureLogger() {
|
||||||
LogManager.shutdown();
|
LogManager.shutdown();
|
||||||
String logFile;
|
String logFile = getConfigBoolean("log.save", false) ? "log4j.file.properties" : "log4j.properties";
|
||||||
if (getConfigBoolean("log.save", false)) {
|
|
||||||
logFile = "log4j.file.properties";
|
try (InputStream stream = Utils.class.getClassLoader().getResourceAsStream(logFile)) {
|
||||||
}
|
|
||||||
else {
|
|
||||||
logFile = "log4j.properties";
|
|
||||||
}
|
|
||||||
InputStream stream = Utils.class.getClassLoader().getResourceAsStream(logFile);
|
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
PropertyConfigurator.configure("src/main/resources/" + logFile);
|
PropertyConfigurator.configure("src/main/resources/" + logFile);
|
||||||
} else {
|
} else {
|
||||||
PropertyConfigurator.configure(stream);
|
PropertyConfigurator.configure(stream);
|
||||||
}
|
}
|
||||||
logger.info("Loaded " + logFile);
|
|
||||||
try {
|
LOGGER.info("Loaded " + logFile);
|
||||||
stream.close();
|
} catch (IOException e) {
|
||||||
} catch (IOException e) { }
|
LOGGER.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets list of strings between two strings.
|
* Gets list of strings between two strings.
|
||||||
|
*
|
||||||
* @param fullText Text to retrieve from.
|
* @param fullText Text to retrieve from.
|
||||||
* @param start String that precedes the desired text
|
* @param start String that precedes the desired text
|
||||||
* @param finish String that follows the desired text
|
* @param finish String that follows the desired text
|
||||||
@ -572,29 +587,29 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
public static List<String> between(String fullText, String start, String finish) {
|
public static List<String> between(String fullText, String start, String finish) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
int i, j;
|
int i = fullText.indexOf(start);
|
||||||
i = fullText.indexOf(start);
|
|
||||||
while (i >= 0) {
|
while (i >= 0) {
|
||||||
i += start.length();
|
i += start.length();
|
||||||
j = fullText.indexOf(finish, i);
|
int j = fullText.indexOf(finish, i);
|
||||||
if (j < 0) {
|
if (j < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
result.add(fullText.substring(i, j));
|
result.add(fullText.substring(i, j));
|
||||||
i = fullText.indexOf(start, j + finish.length());
|
i = fullText.indexOf(start, j + finish.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an URL query
|
* Parses an URL query
|
||||||
*
|
*
|
||||||
* @param query
|
* @param query The query part of an URL
|
||||||
* The query part of an URL
|
|
||||||
* @return The map of all query parameters
|
* @return The map of all query parameters
|
||||||
*/
|
*/
|
||||||
public static Map<String,String> parseUrlQuery(String query) {
|
public static Map<String, String> parseUrlQuery(String query) {
|
||||||
Map<String,String> res = new HashMap<>();
|
Map<String, String> res = new HashMap<>();
|
||||||
|
|
||||||
if (query.equals("")) {
|
if (query.equals("")) {
|
||||||
return res;
|
return res;
|
||||||
@ -622,10 +637,8 @@ public class Utils {
|
|||||||
/**
|
/**
|
||||||
* Parses an URL query and returns the requested parameter's value
|
* Parses an URL query and returns the requested parameter's value
|
||||||
*
|
*
|
||||||
* @param query
|
* @param query The query part of an URL
|
||||||
* The query part of an URL
|
* @param key The key whose value is requested
|
||||||
* @param key
|
|
||||||
* The key whose value is requested
|
|
||||||
* @return The associated value or null if key wasn't found
|
* @return The associated value or null if key wasn't found
|
||||||
*/
|
*/
|
||||||
public static String parseUrlQuery(String query, String key) {
|
public static String parseUrlQuery(String query, String key) {
|
||||||
@ -655,18 +668,13 @@ public class Utils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HashMap<String, HashMap<String, String>> cookieCache;
|
|
||||||
static {
|
|
||||||
cookieCache = new HashMap<String, HashMap<String, String>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the cookies from a certain host
|
* Gets all the cookies from a certain host
|
||||||
*/
|
*/
|
||||||
public static Map<String, String> getCookies(String host) {
|
public static Map<String, String> getCookies(String host) {
|
||||||
HashMap<String, String> domainCookies = cookieCache.get(host);
|
HashMap<String, String> domainCookies = cookieCache.get(host);
|
||||||
if (domainCookies == null) {
|
if (domainCookies == null) {
|
||||||
domainCookies = new HashMap<String, String>();
|
domainCookies = new HashMap<>();
|
||||||
String cookiesConfig = getConfigString("cookies." + host, "");
|
String cookiesConfig = getConfigString("cookies." + host, "");
|
||||||
for (String pair : cookiesConfig.split(" ")) {
|
for (String pair : cookiesConfig.split(" ")) {
|
||||||
pair = pair.trim();
|
pair = pair.trim();
|
||||||
@ -690,20 +698,21 @@ public class Utils {
|
|||||||
if (langSelect == null) {
|
if (langSelect == null) {
|
||||||
if (!getConfigString("lang", "").equals("")) {
|
if (!getConfigString("lang", "").equals("")) {
|
||||||
String[] langCode = getConfigString("lang", "").split("_");
|
String[] langCode = getConfigString("lang", "").split("_");
|
||||||
logger.info("Setting locale to " + getConfigString("lang", ""));
|
LOGGER.info("Setting locale to " + getConfigString("lang", ""));
|
||||||
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
|
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String[] langCode = langSelect.split("_");
|
String[] langCode = langSelect.split("_");
|
||||||
logger.info("Setting locale to " + langSelect);
|
LOGGER.info("Setting locale to " + langSelect);
|
||||||
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
|
return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
logger.info("Setting locale to default");
|
LOGGER.info("Setting locale to default");
|
||||||
return ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), new UTF8Control());
|
return ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), new UTF8Control());
|
||||||
} catch (MissingResourceException e) {
|
} catch (MissingResourceException e) {
|
||||||
logger.info("Setting locale to root");
|
LOGGER.info("Setting locale to root");
|
||||||
return ResourceBundle.getBundle("LabelsBundle", Locale.ROOT);
|
return ResourceBundle.getBundle("LabelsBundle", Locale.ROOT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user