ソースを参照

Merge pull request #45 from ataillefer/0.10.9

Bug fix: Support Android content URIs for upload
Travis Nuttall 6 年 前
コミット
20d1be476b
No account linked to committer's email address

+ 40
- 3
android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java ファイルの表示

@@ -18,6 +18,7 @@ import java.io.IOException;
18 18
 import java.io.InputStream;
19 19
 import java.util.ArrayList;
20 20
 
21
+import android.net.Uri;
21 22
 import okhttp3.MediaType;
22 23
 import okhttp3.RequestBody;
23 24
 import okio.BufferedSink;
@@ -68,7 +69,7 @@ class RNFetchBlobBody extends RequestBody{
68 69
         try {
69 70
             switch (requestType) {
70 71
                 case SingleFile:
71
-                    requestStream = getReuqestStream();
72
+                    requestStream = getRequestStream();
72 73
                     contentLength = requestStream.available();
73 74
                     break;
74 75
                 case AsIs:
@@ -135,7 +136,7 @@ class RNFetchBlobBody extends RequestBody{
135 136
         return true;
136 137
     }
137 138
 
138
-    private InputStream getReuqestStream() throws Exception {
139
+    private InputStream getRequestStream() throws Exception {
139 140
 
140 141
         // upload from storage
141 142
         if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
@@ -159,6 +160,13 @@ class RNFetchBlobBody extends RequestBody{
159 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 171
         // base 64 encoded
164 172
         else {
@@ -224,6 +232,20 @@ class RNFetchBlobBody extends RequestBody{
224 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 250
                 // base64 embedded file content
229 251
                 else {
@@ -289,7 +311,7 @@ class RNFetchBlobBody extends RequestBody{
289 311
      * Compute approximate content length for form data
290 312
      * @return ArrayList<FormField>
291 313
      */
292
-    private ArrayList<FormField> countFormDataLength() {
314
+    private ArrayList<FormField> countFormDataLength() throws IOException {
293 315
         long total = 0;
294 316
         ArrayList<FormField> list = new ArrayList<>();
295 317
         ReactApplicationContext ctx = RNFetchBlob.RCTContext;
@@ -320,6 +342,21 @@ class RNFetchBlobBody extends RequestBody{
320 342
                         File file = new File(RNFetchBlobFS.normalizePath(orgPath));
321 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 361
                 // base64 embedded file content
325 362
                 else {

+ 1
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java ファイルの表示

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

+ 2
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java ファイルの表示

@@ -266,7 +266,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
266 266
                     requestType = RequestType.SingleFile;
267 267
                 }
268 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 271
                         requestType = RequestType.SingleFile;
271 272
                     }
272 273
                     else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {

+ 2
- 1
index.js ファイルの表示

@@ -79,7 +79,8 @@ if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
79 79
 }
80 80
 
81 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
 /**