From 96a8801c190f1b396373d642dad1278eab5c2101 Mon Sep 17 00:00:00 2001 From: cyian-1756 Date: Tue, 26 Jun 2018 00:54:18 -0400 Subject: [PATCH] Rippers can now choose to use a byte progess bar --- .../ripme/ripper/AbstractRipper.java | 3 ++ .../ripme/ripper/DownloadFileThread.java | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 87d8bd46..cbc86548 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -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;} } diff --git a/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java b/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java index 42dedffe..768e1c12 100644 --- a/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java +++ b/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java @@ -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);