Merge pull request #541 from cyian-1756/AbstractRipperFixes

Abstract ripper fixes
This commit is contained in:
cyian-1756 2018-04-26 12:40:12 -04:00 committed by GitHub
commit 2f22e9d08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 16 deletions

View File

@ -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<String, String> cookies, String fileName) {
protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map<String, String> 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<String, String> 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.

View File

@ -0,0 +1,30 @@
package com.rarchives.ripme.tst;
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);
}
}