Rippers can now choose to use a byte progess bar

This commit is contained in:
cyian-1756 2018-06-26 00:54:18 -04:00
parent f8652ceaf7
commit 96a8801c19
2 changed files with 45 additions and 1 deletions

View File

@ -613,4 +613,7 @@ public abstract class AbstractRipper
protected boolean isThisATest() {
return thisIsATest;
}
// If true ripme uses a byte progress bar
protected boolean useByteProgessBar() { return false;}
}

View File

@ -59,6 +59,16 @@ class DownloadFileThread extends Thread {
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.
* Notifies observers upon completion/error/warn.
@ -81,6 +91,21 @@ class DownloadFileThread extends Thread {
}
}
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;
boolean redirected = false;
int tries = 0; // Number of attempts to download
@ -156,7 +181,23 @@ class DownloadFileThread extends Thread {
}
fos = new FileOutputStream(saveAs);
IOUtils.copy(bis, fos);
byte[] data = new byte[1024 * 256]; int bytesRead;
while ( (bytesRead = bis.read(data)) != -1) {
try {
observer.stopCheck();
} catch (IOException e) {
observer.downloadErrored(url, "Download interrupted");
return;
}
fos.write(data, 0, bytesRead);
if (observer.useByteProgessBar()) {
bytesDownloaded += bytesRead;
observer.setBytesCompleted(bytesDownloaded);
observer.sendUpdate(STATUS.COMPLETED_BYTES, bytesDownloaded);
}
}
bis.close();
fos.close();
break; // Download successful: break out of infinite loop
} catch (HttpStatusException hse) {
logger.debug("HTTP status exception", hse);