Browse Source

Merge pull request #45 from ataillefer/0.10.9

Bug fix: Support Android content URIs for upload
Travis Nuttall 6 years ago
parent
commit
20d1be476b
No account linked to committer's email address

+ 40
- 3
android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java View File

18
 import java.io.InputStream;
18
 import java.io.InputStream;
19
 import java.util.ArrayList;
19
 import java.util.ArrayList;
20
 
20
 
21
+import android.net.Uri;
21
 import okhttp3.MediaType;
22
 import okhttp3.MediaType;
22
 import okhttp3.RequestBody;
23
 import okhttp3.RequestBody;
23
 import okio.BufferedSink;
24
 import okio.BufferedSink;
68
         try {
69
         try {
69
             switch (requestType) {
70
             switch (requestType) {
70
                 case SingleFile:
71
                 case SingleFile:
71
-                    requestStream = getReuqestStream();
72
+                    requestStream = getRequestStream();
72
                     contentLength = requestStream.available();
73
                     contentLength = requestStream.available();
73
                     break;
74
                     break;
74
                 case AsIs:
75
                 case AsIs:
135
         return true;
136
         return true;
136
     }
137
     }
137
 
138
 
138
-    private InputStream getReuqestStream() throws Exception {
139
+    private InputStream getRequestStream() throws Exception {
139
 
140
 
140
         // upload from storage
141
         // upload from storage
141
         if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
142
         if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
159
                     throw new Exception("error when getting request stream: " +e.getLocalizedMessage());
160
                     throw new Exception("error when getting request stream: " +e.getLocalizedMessage());
160
                 }
161
                 }
161
             }
162
             }
163
+        } else if (rawBody.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
164
+            String contentURI = rawBody.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
165
+            try {
166
+                return RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(contentURI));
167
+            } catch (Exception e) {
168
+                throw new Exception("error when getting request stream for content URI: " + contentURI, e);
169
+            }
162
         }
170
         }
163
         // base 64 encoded
171
         // base 64 encoded
164
         else {
172
         else {
224
                             RNFetchBlobUtils.emitWarningEvent("Failed to create form data from path :" + orgPath + ", file not exists.");
232
                             RNFetchBlobUtils.emitWarningEvent("Failed to create form data from path :" + orgPath + ", file not exists.");
225
                         }
233
                         }
226
                     }
234
                     }
235
+                } else if (data.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
236
+                    String contentURI = data.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
237
+                    InputStream is = null;
238
+                    try {
239
+                        is = ctx.getContentResolver().openInputStream(Uri.parse(contentURI));
240
+                        pipeStreamToFileStream(is, os);
241
+                    } catch(Exception e) {
242
+                        RNFetchBlobUtils.emitWarningEvent(
243
+                                "Failed to create form data from content URI:" + contentURI + ", " + e.getLocalizedMessage());
244
+                    } finally {
245
+                        if (is != null) {
246
+                            is.close();
247
+                        }
248
+                    }
227
                 }
249
                 }
228
                 // base64 embedded file content
250
                 // base64 embedded file content
229
                 else {
251
                 else {
289
      * Compute approximate content length for form data
311
      * Compute approximate content length for form data
290
      * @return ArrayList<FormField>
312
      * @return ArrayList<FormField>
291
      */
313
      */
292
-    private ArrayList<FormField> countFormDataLength() {
314
+    private ArrayList<FormField> countFormDataLength() throws IOException {
293
         long total = 0;
315
         long total = 0;
294
         ArrayList<FormField> list = new ArrayList<>();
316
         ArrayList<FormField> list = new ArrayList<>();
295
         ReactApplicationContext ctx = RNFetchBlob.RCTContext;
317
         ReactApplicationContext ctx = RNFetchBlob.RCTContext;
320
                         File file = new File(RNFetchBlobFS.normalizePath(orgPath));
342
                         File file = new File(RNFetchBlobFS.normalizePath(orgPath));
321
                         total += file.length();
343
                         total += file.length();
322
                     }
344
                     }
345
+                } else if (data.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
346
+                    String contentURI = data.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
347
+                    InputStream is = null;
348
+                    try {
349
+                        is = ctx.getContentResolver().openInputStream(Uri.parse(contentURI));
350
+                        long length = is.available();
351
+                        total += length;
352
+                    } catch(Exception e) {
353
+                        RNFetchBlobUtils.emitWarningEvent(
354
+                                "Failed to estimate form data length from content URI:" + contentURI + ", " + e.getLocalizedMessage());
355
+                    } finally {
356
+                        if (is != null) {
357
+                            is.close();
358
+                        }
359
+                    }
323
                 }
360
                 }
324
                 // base64 embedded file content
361
                 // base64 embedded file content
325
                 else {
362
                 else {

+ 1
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java View File

7
     public static final String EVENT_HTTP_STATE = "RNFetchBlobState";
7
     public static final String EVENT_HTTP_STATE = "RNFetchBlobState";
8
     public static final String EVENT_MESSAGE = "RNFetchBlobMessage";
8
     public static final String EVENT_MESSAGE = "RNFetchBlobMessage";
9
     public static final String FILE_PREFIX = "RNFetchBlob-file://";
9
     public static final String FILE_PREFIX = "RNFetchBlob-file://";
10
+    public static final String CONTENT_PREFIX = "RNFetchBlob-content://";
10
     public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
11
     public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
11
     public static final String FILE_PREFIX_CONTENT = "content://";
12
     public static final String FILE_PREFIX_CONTENT = "content://";
12
     public static final String DATA_ENCODE_URI = "uri";
13
     public static final String DATA_ENCODE_URI = "uri";

+ 2
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

266
                     requestType = RequestType.SingleFile;
266
                     requestType = RequestType.SingleFile;
267
                 }
267
                 }
268
                 if(rawRequestBody != null) {
268
                 if(rawRequestBody != null) {
269
-                    if(rawRequestBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
269
+                    if (rawRequestBody.startsWith(RNFetchBlobConst.FILE_PREFIX)
270
+                            || rawRequestBody.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
270
                         requestType = RequestType.SingleFile;
271
                         requestType = RequestType.SingleFile;
271
                     }
272
                     }
272
                     else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {
273
                     else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {

+ 2
- 1
index.js View File

79
 }
79
 }
80
 
80
 
81
 function wrap(path:string):string {
81
 function wrap(path:string):string {
82
-  return 'RNFetchBlob-file://' + path
82
+  const prefix = path.startsWith('content://') ? 'RNFetchBlob-content://' : 'RNFetchBlob-file://'
83
+  return prefix + path
83
 }
84
 }
84
 
85
 
85
 /**
86
 /**