From 015d35706c0fd2e943c2f703e3b5dcecc4b01649 Mon Sep 17 00:00:00 2001 From: soloturn Date: Fri, 15 Apr 2022 16:09:26 +0200 Subject: [PATCH] split out method to retrieve the download path --- .../ripme/ripper/AbstractRipper.java | 43 ++++++++++--------- .../ripper/rippers/DuckmoviesRipper.java | 2 +- .../ripme/tst/AbstractRipperTest.java | 10 ++--- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index ea713789..4321d337 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -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 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()) { diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/DuckmoviesRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/DuckmoviesRipper.java index 48c1856c..696ea015 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/DuckmoviesRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/DuckmoviesRipper.java @@ -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 diff --git a/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java index a388151c..c750b22b 100644 --- a/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java +++ b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java @@ -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); }