mirror of
https://github.com/TeamNewPipe/NewPipe.git
synced 2024-11-22 11:02:35 +01:00
download files via Tor when Tor is enabled
DownloadManager does not let you set its proxy or change how it connects to the internet. So we have to make a custom one, unfortunately. This is a very basic downloader with none of the special sauce that makes the built-in DownloadManager handy.
This commit is contained in:
parent
b3a1a5dcc2
commit
0bb0226bc2
@ -39,4 +39,8 @@ public class App extends Application {
|
|||||||
OrbotHelper.requestStartTor(context);
|
OrbotHelper.requestStartTor(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isUsingTor() {
|
||||||
|
return useTor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,15 +87,21 @@ public class DownloadDialog extends DialogFragment {
|
|||||||
//TODO notify user "download directory should be changed" ?
|
//TODO notify user "download directory should be changed" ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
String saveFilePath = dir + "/" + title + suffix;
|
||||||
DownloadManager.Request request = new DownloadManager.Request(
|
if (App.isUsingTor()) {
|
||||||
Uri.parse(url));
|
// if using Tor, do not use DownloadManager because the proxy cannot be set
|
||||||
request.setDestinationUri(Uri.fromFile(new File(dir + "/" + title + suffix)));
|
Downloader.downloadFile(url, saveFilePath);
|
||||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
} else {
|
||||||
try {
|
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
dm.enqueue(request);
|
DownloadManager.Request request = new DownloadManager.Request(
|
||||||
} catch (Exception e) {
|
Uri.parse(url));
|
||||||
e.printStackTrace();
|
request.setDestinationUri(Uri.fromFile(new File(saveFilePath)));
|
||||||
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||||
|
try {
|
||||||
|
dm.enqueue(request);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
package org.schabi.newpipe;
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
@ -32,6 +40,7 @@ import info.guardianproject.netcipher.NetCipher;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class Downloader {
|
public class Downloader {
|
||||||
|
public static final String TAG = "Downloader";
|
||||||
|
|
||||||
private static final String USER_AGENT = "Mozilla/5.0";
|
private static final String USER_AGENT = "Mozilla/5.0";
|
||||||
|
|
||||||
@ -97,4 +106,51 @@ public class Downloader {
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads a file from a URL in the background using an {@link AsyncTask}.
|
||||||
|
*
|
||||||
|
* @param fileURL HTTP URL of the file to be downloaded
|
||||||
|
* @param saveFilePath path of the directory to save the file
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void downloadFile(final String fileURL, final String saveFilePath) {
|
||||||
|
new AsyncTask<Void, Void, Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... voids) {
|
||||||
|
HttpsURLConnection con = null;
|
||||||
|
try {
|
||||||
|
con = NetCipher.getHttpsURLConnection(fileURL);
|
||||||
|
int responseCode = con.getResponseCode();
|
||||||
|
|
||||||
|
// always check HTTP response code first
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
InputStream inputStream = new BufferedInputStream(con.getInputStream());
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
|
||||||
|
|
||||||
|
int bytesRead = -1;
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Log.i(TAG, "No file to download. Server replied HTTP code: " + responseCode);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (con != null) {
|
||||||
|
con.disconnect();
|
||||||
|
con = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user