Added support for resuming downloads
This commit is contained in:
parent
b1718965c5
commit
04ca5a2054
@ -616,4 +616,6 @@ public abstract class AbstractRipper
|
|||||||
|
|
||||||
// If true ripme uses a byte progress bar
|
// If true ripme uses a byte progress bar
|
||||||
protected boolean useByteProgessBar() { return false;}
|
protected boolean useByteProgessBar() { return false;}
|
||||||
|
// If true ripme will try to resume a broken download for this ripper
|
||||||
|
protected boolean tryResumeDownload() { return false;}
|
||||||
}
|
}
|
||||||
|
@ -59,28 +59,25 @@ class DownloadFileThread extends Thread {
|
|||||||
this.cookies = cookies;
|
this.cookies = cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getTotalBytes(URL url) throws IOException {
|
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setRequestMethod("HEAD");
|
|
||||||
conn.setRequestProperty("accept", "*/*");
|
|
||||||
conn.setRequestProperty("Referer", this.url.toExternalForm()); // Sic
|
|
||||||
conn.setRequestProperty("User-agent", AbstractRipper.USER_AGENT);
|
|
||||||
return conn.getContentLength();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to download the file. Retries as needed.
|
* Attempts to download the file. Retries as needed.
|
||||||
* Notifies observers upon completion/error/warn.
|
* Notifies observers upon completion/error/warn.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
long fileSize = 0;
|
||||||
|
int bytesTotal = 0;
|
||||||
|
int bytesDownloaded = 0;
|
||||||
|
if (saveAs.exists() && observer.tryResumeDownload()) {
|
||||||
|
fileSize = saveAs.length();
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
observer.stopCheck();
|
observer.stopCheck();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
observer.downloadErrored(url, "Download interrupted");
|
observer.downloadErrored(url, "Download interrupted");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (saveAs.exists()) {
|
if (saveAs.exists() && !observer.tryResumeDownload()) {
|
||||||
if (Utils.getConfigBoolean("file.overwrite", false)) {
|
if (Utils.getConfigBoolean("file.overwrite", false)) {
|
||||||
logger.info("[!] Deleting existing file" + prettySaveAs);
|
logger.info("[!] Deleting existing file" + prettySaveAs);
|
||||||
saveAs.delete();
|
saveAs.delete();
|
||||||
@ -90,22 +87,6 @@ class DownloadFileThread extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int bytesTotal, bytesDownloaded = 0;
|
|
||||||
if (observer.useByteProgessBar()) {
|
|
||||||
try {
|
|
||||||
bytesTotal = getTotalBytes(this.url);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("Failed to get file size at " + this.url, e);
|
|
||||||
observer.downloadErrored(this.url, "Failed to get file size of " + this.url);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
observer.setBytesTotal(bytesTotal);
|
|
||||||
observer.sendUpdate(STATUS.TOTAL_BYTES, bytesTotal);
|
|
||||||
logger.debug("Size of file at " + this.url + " = " + bytesTotal + "b");
|
|
||||||
}
|
|
||||||
|
|
||||||
URL urlToDownload = this.url;
|
URL urlToDownload = this.url;
|
||||||
boolean redirected = false;
|
boolean redirected = false;
|
||||||
int tries = 0; // Number of attempts to download
|
int tries = 0; // Number of attempts to download
|
||||||
@ -139,11 +120,20 @@ class DownloadFileThread extends Thread {
|
|||||||
cookie += key + "=" + cookies.get(key);
|
cookie += key + "=" + cookies.get(key);
|
||||||
}
|
}
|
||||||
huc.setRequestProperty("Cookie", cookie);
|
huc.setRequestProperty("Cookie", cookie);
|
||||||
|
if (observer.tryResumeDownload()) {
|
||||||
|
if (fileSize != 0) {
|
||||||
|
huc.setRequestProperty("Range", "bytes=" + fileSize + "-");
|
||||||
|
}
|
||||||
|
}
|
||||||
logger.debug("Request properties: " + huc.getRequestProperties());
|
logger.debug("Request properties: " + huc.getRequestProperties());
|
||||||
huc.connect();
|
huc.connect();
|
||||||
|
|
||||||
int statusCode = huc.getResponseCode();
|
int statusCode = huc.getResponseCode();
|
||||||
logger.debug("Status code: " + statusCode);
|
logger.debug("Status code: " + statusCode);
|
||||||
|
if (statusCode != 206 && observer.tryResumeDownload()) {
|
||||||
|
// TODO find a better way to handle servers that don't support resuming downloads then just erroring out
|
||||||
|
throw new IOException("Server doesn't support resuming downloads");
|
||||||
|
}
|
||||||
if (statusCode / 100 == 3) { // 3xx Redirect
|
if (statusCode / 100 == 3) { // 3xx Redirect
|
||||||
if (!redirected) {
|
if (!redirected) {
|
||||||
// Don't increment retries on the first redirect
|
// Don't increment retries on the first redirect
|
||||||
@ -171,6 +161,15 @@ class DownloadFileThread extends Thread {
|
|||||||
observer.downloadErrored(url, "Imgur image is 404: " + url.toExternalForm());
|
observer.downloadErrored(url, "Imgur image is 404: " + url.toExternalForm());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the ripper is using the bytes progress bar set bytesTotal to huc.getContentLength()
|
||||||
|
if (observer.useByteProgessBar()) {
|
||||||
|
bytesTotal = huc.getContentLength();
|
||||||
|
observer.setBytesTotal(bytesTotal);
|
||||||
|
observer.sendUpdate(STATUS.TOTAL_BYTES, bytesTotal);
|
||||||
|
logger.debug("Size of file at " + this.url + " = " + bytesTotal + "b");
|
||||||
|
}
|
||||||
|
|
||||||
// Save file
|
// Save file
|
||||||
bis = new BufferedInputStream(huc.getInputStream());
|
bis = new BufferedInputStream(huc.getInputStream());
|
||||||
|
|
||||||
@ -179,8 +178,12 @@ class DownloadFileThread extends Thread {
|
|||||||
String fileExt = URLConnection.guessContentTypeFromStream(bis).replaceAll("image/", "");
|
String fileExt = URLConnection.guessContentTypeFromStream(bis).replaceAll("image/", "");
|
||||||
saveAs = new File(saveAs.toString() + "." + fileExt);
|
saveAs = new File(saveAs.toString() + "." + fileExt);
|
||||||
}
|
}
|
||||||
|
// If we're resuming a download we append data to the existing file
|
||||||
fos = new FileOutputStream(saveAs);
|
if (statusCode == 206) {
|
||||||
|
fos = new FileOutputStream(saveAs, true);
|
||||||
|
} else {
|
||||||
|
fos = new FileOutputStream(saveAs);
|
||||||
|
}
|
||||||
byte[] data = new byte[1024 * 256]; int bytesRead;
|
byte[] data = new byte[1024 * 256]; int bytesRead;
|
||||||
while ( (bytesRead = bis.read(data)) != -1) {
|
while ( (bytesRead = bis.read(data)) != -1) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user