1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-13 17:34:13 +02:00

split out method to retrieve the download path

This commit is contained in:
soloturn
2022-04-15 16:09:26 +02:00
parent 3eca9fa620
commit 015d35706c
3 changed files with 29 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
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.ArrayList;
@@ -317,31 +318,18 @@ public abstract class AbstractRipper
return false;
}
LOGGER.debug("url: " + url + ", subdirectory" + subdirectory + ", referrer: " + referrer + ", cookies: " + cookies + ", prefix: " + prefix + ", fileName: " + fileName);
String saveAs = getFileName(url, prefix, fileName, extension);
File saveFileAs;
Path saveAs;
try {
if (!subdirectory.equals("")) {
subdirectory = Utils.filesystemSafe(subdirectory);
subdirectory = File.separator + subdirectory;
saveAs = getFilePath(url, subdirectory, prefix, fileName, extension);
LOGGER.debug("Downloading " + url + " to " + saveAs);
if (!Files.exists(saveAs.getParent())) {
LOGGER.info("[+] Creating directory: " + saveAs.getParent());
Files.createDirectories(saveAs.getParent());
}
String topFolderName = workingDir.getCanonicalPath();
if (App.stringToAppendToFoldername != null) {
topFolderName = topFolderName + App.stringToAppendToFoldername;
}
saveFileAs = new File(
topFolderName
+ subdirectory
+ File.separator
+ saveAs);
} catch (IOException e) {
LOGGER.error("[!] Error creating save file path for URL '" + url + "':", e);
return false;
}
LOGGER.debug("Downloading " + url + " to " + saveFileAs);
if (!saveFileAs.getParentFile().exists()) {
LOGGER.info("[+] Creating directory: " + saveFileAs.getParent());
saveFileAs.getParentFile().mkdirs();
}
if (Utils.getConfigBoolean("remember.url_history", true) && !isThisATest()) {
LOGGER.info("Writing " + url.toExternalForm() + " to file");
try {
@@ -350,7 +338,7 @@ public abstract class AbstractRipper
LOGGER.debug("Unable to write URL history file");
}
}
return addURLToDownload(url, saveFileAs.toPath(), referrer, cookies, getFileExtFromMIME);
return addURLToDownload(url, saveAs, referrer, cookies, getFileExtFromMIME);
}
protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map<String,String> cookies, String fileName, String extension) {
@@ -393,6 +381,21 @@ public abstract class AbstractRipper
return addURLToDownload(url, prefix, "");
}
public Path getFilePath(URL url, String subdir, String prefix, String fileName, String extension) throws IOException {
// construct the path: workingdir + subdir + prefix + filename + extension
// save into working dir
Path filepath = Paths.get(workingDir.getCanonicalPath());
if (null != App.stringToAppendToFoldername)
filepath = filepath.resolveSibling(filepath.getFileName() + App.stringToAppendToFoldername);
if (null != subdir && !subdir.trim().isEmpty())
filepath = filepath.resolve(Utils.filesystemSafe(subdir));
filepath = filepath.resolve(getFileName(url, prefix, fileName, extension));
return filepath;
}
public static String getFileName(URL url, String prefix, String fileName, String extension) {
// retrieve filename from URL if not passed
if (fileName == null || fileName.trim().isEmpty()) {

View File

@@ -131,7 +131,7 @@ public class DuckmoviesRipper extends AbstractSingleFileRipper {
@Override
public void downloadURL(URL url, int index) {
addURLToDownload(url, "", "", null, null, AbstractRipper.getFileName(url, null, null).replaceAll("%20", "_"));
addURLToDownload(url, "", "", null, null, null);
}
@Override

View File

@@ -13,19 +13,19 @@ public class AbstractRipperTest {
@Test
public void testGetFileName() throws IOException {
String fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), "test", "test");
String fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"),null, "test", "test");
assertEquals("test.test", fileName);
fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), "test", null);
fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), null,"test", null);
assertEquals("test", fileName);
fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), null, null);
fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), null,null, null);
assertEquals("Object", fileName);
fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file.png"), null, null);
fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file.png"), null,null, null);
assertEquals("file.png", fileName);
fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file."), null, null);
fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file."), null,null, null);
assertEquals("file.", fileName);
}