From 19ce96eae5236ce4766bf1bd2a90eead7853187a Mon Sep 17 00:00:00 2001 From: cyian-1756 Date: Mon, 23 Apr 2018 15:58:01 -0400 Subject: [PATCH 1/2] Fixed issue with downloading images without an extension --- .../ripme/ripper/AbstractRipper.java | 51 +++++++++++++------ .../ripme/tst/AbstractRipperTest.java | 4 ++ 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 6068ed18..4438ae2a 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -212,7 +212,7 @@ public abstract class AbstractRipper * True if downloaded successfully * False if failed to download */ - protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies, String fileName) { + protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies, String fileName, String extension) { // Don't re-add the url if it was downloaded in a previous rip if (Utils.getConfigBoolean("remember.url_history", true) && !isThisATest()) { if (hasDownloadedURL(url.toExternalForm())) { @@ -228,21 +228,7 @@ public abstract class AbstractRipper return false; } logger.debug("url: " + url + ", prefix: " + prefix + ", subdirectory" + subdirectory + ", referrer: " + referrer + ", cookies: " + cookies + ", fileName: " + fileName); - String saveAs; - if (fileName != null) { - saveAs = fileName; - // Get the extension of the file - String extension = url.toExternalForm().substring(url.toExternalForm().lastIndexOf(".") + 1); - saveAs = saveAs + "." + extension; - } else { - saveAs = url.toExternalForm(); - saveAs = saveAs.substring(saveAs.lastIndexOf('/')+1); - } - - if (saveAs.indexOf('?') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('?')); } - if (saveAs.indexOf('#') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('#')); } - if (saveAs.indexOf('&') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('&')); } - if (saveAs.indexOf(':') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf(':')); } + String saveAs = getFileName(url, fileName, extension); File saveFileAs; try { if (!subdirectory.equals("")) { @@ -274,6 +260,10 @@ public abstract class AbstractRipper return addURLToDownload(url, saveFileAs, referrer, cookies); } + protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies, String fileName) { + return addURLToDownload(url, prefix, subdirectory, referrer, cookies, fileName, null); + } + /** * Queues file to be downloaded and saved. With options. * @param url @@ -306,6 +296,35 @@ public abstract class AbstractRipper return addURLToDownload(url, prefix, ""); } + public static String getFileName(URL url, String fileName, String extension) { + String saveAs; + if (fileName != null) { + saveAs = fileName; + } else { + saveAs = url.toExternalForm(); + saveAs = saveAs.substring(saveAs.lastIndexOf('/')+1); + } + if (extension == null) { + // Get the extension of the file + String[] lastBitOfURL = url.toExternalForm().split("/"); + + String[] lastBit = lastBitOfURL[lastBitOfURL.length - 1].split("."); + if (lastBit.length != 0) { + extension = lastBit[lastBit.length - 1]; + saveAs = saveAs + "." + extension; + } + } + + if (saveAs.indexOf('?') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('?')); } + if (saveAs.indexOf('#') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('#')); } + if (saveAs.indexOf('&') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('&')); } + if (saveAs.indexOf(':') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf(':')); } + if (extension != null) { + saveAs = saveAs + "." + extension; + } + return saveAs; + } + /** * Waits for downloading threads to complete. diff --git a/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java new file mode 100644 index 00000000..3a71f472 --- /dev/null +++ b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java @@ -0,0 +1,4 @@ +package com.rarchives.ripme.tst; + +public class AbstractRipperTest { +} From 56d20b15000e33f5ff9683938c5f786aa07afa95 Mon Sep 17 00:00:00 2001 From: cyian-1756 Date: Mon, 23 Apr 2018 15:58:18 -0400 Subject: [PATCH 2/2] Added some unit tests for getting file name --- .../ripme/tst/AbstractRipperTest.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java index 3a71f472..f1d8eff5 100644 --- a/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java +++ b/src/test/java/com/rarchives/ripme/tst/AbstractRipperTest.java @@ -1,4 +1,30 @@ package com.rarchives.ripme.tst; -public class AbstractRipperTest { +import com.rarchives.ripme.ripper.AbstractRipper; +import junit.framework.TestCase; + +import java.io.IOException; +import java.net.URL; + + + +public class AbstractRipperTest extends TestCase { + + public void testGetFileName() throws IOException { + String fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), "test", "test"); + assertEquals("test.test", fileName); + + fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), "test", null); + assertEquals("test", fileName); + + fileName = AbstractRipper.getFileName(new URL("http://www.tsumino.com/Image/Object?name=U1EieteEGwm6N1dGszqCpA%3D%3D"), null, null); + assertEquals("Object", fileName); + + fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file.png"), null, null); + assertEquals("file.png", fileName); + + fileName = AbstractRipper.getFileName(new URL("http://www.test.com/file."), null, null); + assertEquals("file.", fileName); + } + }