|
@@ -2,6 +2,7 @@ package com.RNFetchBlob;
|
2
|
2
|
|
3
|
3
|
import android.app.DownloadManager;
|
4
|
4
|
import android.content.BroadcastReceiver;
|
|
5
|
+import android.content.ContentResolver;
|
5
|
6
|
import android.content.Context;
|
6
|
7
|
import android.content.Intent;
|
7
|
8
|
import android.content.IntentFilter;
|
|
@@ -116,9 +117,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
116
|
117
|
String key = it.nextKey();
|
117
|
118
|
req.addRequestHeader(key, headers.getString(key));
|
118
|
119
|
}
|
119
|
|
- DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
120
|
+ Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
|
|
121
|
+ DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
|
120
|
122
|
downloadManagerId = dm.enqueue(req);
|
121
|
|
- ctx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
123
|
+ appCtx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
122
|
124
|
return;
|
123
|
125
|
}
|
124
|
126
|
|
|
@@ -146,7 +148,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
146
|
148
|
|
147
|
149
|
OkHttpClient.Builder client;
|
148
|
150
|
|
149
|
|
-// try {
|
|
151
|
+ try {
|
150
|
152
|
// use trusty SSL socket
|
151
|
153
|
if (this.options.trusty) {
|
152
|
154
|
client = RNFetchBlobUtils.getUnsafeOkHttpClient();
|
|
@@ -173,7 +175,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
173
|
175
|
// set request body
|
174
|
176
|
switch (requestType) {
|
175
|
177
|
case SingleFile:
|
176
|
|
- InputStream dataStream= buildOctetBody(rawRequestBody);
|
|
178
|
+ InputStream dataStream = buildOctetBody(rawRequestBody);
|
177
|
179
|
builder.method(method, new RNFetchBlobBody(
|
178
|
180
|
taskId,
|
179
|
181
|
RequestType.SingleFile,
|
|
@@ -200,7 +202,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
200
|
202
|
|
201
|
203
|
final Request req = builder.build();
|
202
|
204
|
|
203
|
|
-// create response handler
|
|
205
|
+// create response handler
|
204
|
206
|
client.addInterceptor(new Interceptor() {
|
205
|
207
|
@Override
|
206
|
208
|
public Response intercept(Chain chain) throws IOException {
|
|
@@ -262,10 +264,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
262
|
264
|
});
|
263
|
265
|
|
264
|
266
|
|
265
|
|
-// } catch (Exception error) {
|
266
|
|
-// error.printStackTrace();
|
267
|
|
-// callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
|
268
|
|
-// }
|
|
267
|
+ } catch (Exception error) {
|
|
268
|
+ error.printStackTrace();
|
|
269
|
+ callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
|
|
270
|
+ }
|
269
|
271
|
}
|
270
|
272
|
|
271
|
273
|
/**
|
|
@@ -283,11 +285,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
283
|
285
|
}
|
284
|
286
|
break;
|
285
|
287
|
case FileStorage:
|
286
|
|
- // write chunk
|
287
|
|
- try {
|
|
288
|
+ try{
|
288
|
289
|
resp.body().bytes();
|
289
|
|
- } catch (IOException e) {
|
290
|
|
- e.printStackTrace();
|
|
290
|
+ } catch (Exception ignored) {
|
|
291
|
+
|
291
|
292
|
}
|
292
|
293
|
callback.invoke(null, this.destPath);
|
293
|
294
|
break;
|
|
@@ -364,17 +365,18 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
364
|
365
|
public void onReceive(Context context, Intent intent) {
|
365
|
366
|
String action = intent.getAction();
|
366
|
367
|
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
|
|
368
|
+ Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
|
367
|
369
|
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
|
368
|
370
|
if (id == this.downloadManagerId) {
|
369
|
371
|
DownloadManager.Query query = new DownloadManager.Query();
|
370
|
372
|
query.setFilterById(downloadManagerId);
|
371
|
|
- DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
373
|
+ DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
|
372
|
374
|
dm.query(query);
|
373
|
375
|
Cursor c = dm.query(query);
|
374
|
376
|
if (c.moveToFirst()) {
|
375
|
377
|
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
376
|
378
|
Uri uri = Uri.parse(contentUri);
|
377
|
|
- Cursor cursor = ctx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
|
|
379
|
+ Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
|
378
|
380
|
if (cursor != null) {
|
379
|
381
|
cursor.moveToFirst();
|
380
|
382
|
String filePath = cursor.getString(0);
|