Skip to content

Commit 0c758c8

Browse files
committed
feat: add trust manager in android
1 parent 39c1096 commit 0c758c8

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploader.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
import java.io.FileInputStream;
2020
import java.net.HttpURLConnection;
2121
import java.net.URL;
22+
import java.security.cert.X509Certificate;
23+
24+
import javax.net.ssl.HttpsURLConnection;
25+
import javax.net.ssl.SSLContext;
26+
import javax.net.ssl.TrustManager;
27+
import javax.net.ssl.X509TrustManager;
2228

2329
public class FileUploader extends AsyncTask<String, Void, String> {
2430
private Promise promise;
@@ -54,9 +60,30 @@ private void sendProgressEvent(float progress) {
5460
@Override
5561
protected String doInBackground(String... params) {
5662
try {
63+
64+
TrustManager[] trustAllCerts = new TrustManager[] {
65+
new X509TrustManager() {
66+
public java.security.cert.X509Certificate[] getAcceptedIssuers()
67+
{
68+
return null;
69+
}
70+
public void checkClientTrusted(X509Certificate[] certs, String authType)
71+
{
72+
//
73+
}
74+
public void checkServerTrusted(X509Certificate[] certs, String authType)
75+
{
76+
//
77+
}
78+
}
79+
};
80+
SSLContext sc = SSLContext.getInstance("TLS");
81+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
82+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
83+
5784
String sourceFileUri = fileUrl;
5885

59-
HttpURLConnection conn = null;
86+
HttpsURLConnection conn = null;
6087
DataOutputStream dos = null;
6188
String lineEnd = "\r\n";
6289
String twoHyphens = "--";
@@ -74,19 +101,16 @@ protected String doInBackground(String... params) {
74101
FileInputStream fileInputStream = new FileInputStream(
75102
sourceFile);
76103
URL url = new URL(upLoadServerUri);
77-
conn = (HttpURLConnection) url.openConnection();
78-
104+
conn = (HttpsURLConnection) url.openConnection();
79105
ReadableMapKeySetIterator headerIterator = this.options.headers.keySetIterator();
80106
while (headerIterator.hasNextKey()) {
81107
String key = headerIterator.nextKey();
82108
String value = this.options.headers.getString(key);
83109
Log.d(TAG, key+" value: "+value);
84110
conn.setRequestProperty(key, value);
85111
}
86-
87112
conn.setUseCaches(false); // Don't use a Cached Copy
88113
conn.setRequestMethod(this.options.method);
89-
conn.setRequestProperty("file", sourceFileUri);
90114
conn.connect();
91115
dos = new DataOutputStream(conn.getOutputStream());
92116

@@ -135,7 +159,7 @@ protected String doInBackground(String... params) {
135159
}
136160
else
137161
{
138-
Log.d(TAG, serverResponseMessage+" doInBackground: error"+conn.getResponseCode());
162+
Log.d(TAG, serverResponseMessage+" doInBackground: error "+conn.getResponseCode());
139163
this.promise.reject("");
140164
}
141165

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-compressor",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "This library compress image, video and audio",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)