1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-09-03 19:02:47 +02:00

downloadvideothread java.nio

This commit is contained in:
soloturn
2022-01-03 19:56:08 +01:00
parent 01e6b5b0d0
commit a3df3d7157

View File

@@ -1,13 +1,13 @@
package com.rarchives.ripme.ripper; package com.rarchives.ripme.ripper;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@@ -25,16 +25,16 @@ class DownloadVideoThread extends Thread {
private static final Logger logger = LogManager.getLogger(DownloadVideoThread.class); private static final Logger logger = LogManager.getLogger(DownloadVideoThread.class);
private URL url; private URL url;
private File saveAs; private Path saveAs;
private String prettySaveAs; private String prettySaveAs;
private AbstractRipper observer; private AbstractRipper observer;
private int retries; private int retries;
public DownloadVideoThread(URL url, File saveAs, AbstractRipper observer) { public DownloadVideoThread(URL url, Path saveAs, AbstractRipper observer) {
super(); super();
this.url = url; this.url = url;
this.saveAs = saveAs; this.saveAs = saveAs;
this.prettySaveAs = Utils.removeCWD(saveAs); this.prettySaveAs = Utils.removeCWD(saveAs.toFile());
this.observer = observer; this.observer = observer;
this.retries = Utils.getConfigInteger("download.retries", 1); this.retries = Utils.getConfigInteger("download.retries", 1);
} }
@@ -50,13 +50,17 @@ class DownloadVideoThread extends Thread {
observer.downloadErrored(url, "Download interrupted"); observer.downloadErrored(url, "Download interrupted");
return; return;
} }
if (saveAs.exists()) { if (Files.exists(saveAs)) {
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(); try {
Files.delete(saveAs);
} catch (IOException e) {
e.printStackTrace();
}
} else { } else {
logger.info("[!] Skipping " + url + " -- file already exists: " + prettySaveAs); logger.info("[!] Skipping " + url + " -- file already exists: " + prettySaveAs);
observer.downloadExists(url, saveAs); observer.downloadExists(url, saveAs.toFile());
return; return;
} }
} }
@@ -100,7 +104,7 @@ class DownloadVideoThread extends Thread {
huc.connect(); huc.connect();
// Check status code // Check status code
bis = new BufferedInputStream(huc.getInputStream()); bis = new BufferedInputStream(huc.getInputStream());
fos = new FileOutputStream(saveAs); fos = Files.newOutputStream(saveAs);
while ( (bytesRead = bis.read(data)) != -1) { while ( (bytesRead = bis.read(data)) != -1) {
try { try {
observer.stopCheck(); observer.stopCheck();
@@ -122,10 +126,10 @@ class DownloadVideoThread extends Thread {
// Close any open streams // Close any open streams
try { try {
if (bis != null) { bis.close(); } if (bis != null) { bis.close(); }
} catch (IOException e) { } } catch (IOException ignored) { }
try { try {
if (fos != null) { fos.close(); } if (fos != null) { fos.close(); }
} catch (IOException e) { } } catch (IOException ignored) { }
} }
if (tries > this.retries) { if (tries > this.retries) {
logger.error("[!] Exceeded maximum retries (" + this.retries + ") for URL " + url); logger.error("[!] Exceeded maximum retries (" + this.retries + ") for URL " + url);
@@ -133,7 +137,7 @@ class DownloadVideoThread extends Thread {
return; return;
} }
} while (true); } while (true);
observer.downloadCompleted(url, saveAs); observer.downloadCompleted(url, saveAs.toFile());
logger.info("[+] Saved " + url + " as " + this.prettySaveAs); logger.info("[+] Saved " + url + " as " + this.prettySaveAs);
} }