Просмотр исходного кода

Merge branch '0.9.5' into 0.10.0

Ben Hsieh 8 лет назад
Родитель
Сommit
301c8cf7d4

+ 1
- 1
package.json Просмотреть файл

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "fetchblob-dev",
3
-  "version": "0.9.4",
3
+  "version": "0.9.5-beta.2",
4 4
   "private": true,
5 5
   "scripts": {
6 6
     "start": "node node_modules/react-native/local-cli/cli.js start",

+ 28
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Просмотреть файл

@@ -63,6 +63,12 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
63 63
         FileStorage
64 64
     }
65 65
 
66
+    enum ResponseFormat {
67
+        Auto,
68
+        UTF8,
69
+        BASE64
70
+    }
71
+
66 72
     public static HashMap<String, Call> taskTable = new HashMap<>();
67 73
     static HashMap<String, Boolean> progressReport = new HashMap<>();
68 74
     static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
@@ -83,8 +89,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
83 89
     RNFetchBlobBody requestBody;
84 90
     RequestType requestType;
85 91
     ResponseType responseType;
92
+    ResponseFormat responseFormat = ResponseFormat.Auto;
86 93
     WritableMap respInfo;
87 94
     boolean timeout = false;
95
+
88 96
     ArrayList<String> redirects = new ArrayList<>();
89 97
 
90 98
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
@@ -200,8 +208,16 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
200 208
                 while (it.hasNextKey()) {
201 209
                     String key = it.nextKey();
202 210
                     String value = headers.getString(key);
203
-                    builder.header(key, value);
204
-                    mheaders.put(key,value);
211
+                    if(key.equalsIgnoreCase("RNFB-Response")) {
212
+                        if(value.equalsIgnoreCase("base64"))
213
+                            responseFormat = ResponseFormat.BASE64;
214
+                        else if (value.equalsIgnoreCase("utf8"))
215
+                            responseFormat = ResponseFormat.UTF8;
216
+                    }
217
+                    else {
218
+                        builder.header(key, value);
219
+                        mheaders.put(key, value);
220
+                    }
205 221
                 }
206 222
             }
207 223
 
@@ -439,6 +455,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
439 455
                         // string correctly, we should do URL encoding before BASE64.
440 456
                         byte[] b = resp.body().bytes();
441 457
                         CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
458
+                        if(responseFormat == ResponseFormat.BASE64) {
459
+                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
460
+                            return;
461
+                        }
442 462
                         try {
443 463
                             encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
444 464
                             // if the data contains invalid characters the following lines will be
@@ -449,7 +469,12 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
449 469
                         // This usually mean the data is contains invalid unicode characters, it's
450 470
                         // binary data
451 471
                         catch(CharacterCodingException ignored) {
452
-                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
472
+                            if(responseFormat == ResponseFormat.UTF8) {
473
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
474
+                            }
475
+                            else {
476
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
477
+                            }
453 478
                         }
454 479
                     }
455 480
                 } catch (IOException e) {

+ 33
- 5
src/ios/RNFetchBlobNetwork.m Просмотреть файл

@@ -27,6 +27,12 @@ NSMapTable * taskTable;
27 27
 NSMutableDictionary * progressTable;
28 28
 NSMutableDictionary * uploadProgressTable;
29 29
 
30
+typedef NS_ENUM(NSUInteger, ResponseFormat) {
31
+    UTF8,
32
+    BASE64,
33
+    AUTO
34
+};
35
+
30 36
 
31 37
 @interface RNFetchBlobNetwork ()
32 38
 {
@@ -37,6 +43,7 @@ NSMutableDictionary * uploadProgressTable;
37 43
     NSMutableDictionary * respInfo;
38 44
     NSInteger respStatus;
39 45
     NSMutableArray * redirects;
46
+    ResponseFormat responseFormat;
40 47
 }
41 48
 
42 49
 @end
@@ -128,6 +135,15 @@ NSOperationQueue *taskQueue;
128 135
     self.options = options;
129 136
     redirects = [[NSMutableArray alloc] init];
130 137
     [redirects addObject:req.URL.absoluteString];
138
+    
139
+    // set response format
140
+    NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
141
+    if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
142
+        responseFormat = BASE64;
143
+    else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
144
+        responseFormat = UTF8;
145
+    else
146
+        responseFormat = AUTO;
131 147
 
132 148
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
133 149
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
@@ -365,18 +381,30 @@ NSOperationQueue *taskQueue;
365 381
             // if it turns out not to be `nil` that means the response data contains valid UTF8 string,
366 382
             // in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
367 383
             NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
368
-
369
-            if(utf8 != nil)
384
+            
385
+            if(responseFormat == BASE64)
386
+            {
387
+                rnfbRespType = RESP_TYPE_BASE64;
388
+                respStr = [respData base64EncodedStringWithOptions:0];
389
+            }
390
+            else if (responseFormat == UTF8)
370 391
             {
371 392
                 rnfbRespType = RESP_TYPE_UTF8;
372 393
                 respStr = utf8;
373 394
             }
374 395
             else
375 396
             {
376
-                rnfbRespType = RESP_TYPE_BASE64;
377
-                respStr = [respData base64EncodedStringWithOptions:0];
397
+                if(utf8 != nil)
398
+                {
399
+                    rnfbRespType = RESP_TYPE_UTF8;
400
+                    respStr = utf8;
401
+                }
402
+                else
403
+                {
404
+                    rnfbRespType = RESP_TYPE_BASE64;
405
+                    respStr = [respData base64EncodedStringWithOptions:0];
406
+                }
378 407
             }
379
-
380 408
         }
381 409
     }
382 410
 

+ 1
- 1
src/package.json Просмотреть файл

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "react-native-fetch-blob",
3
-  "version": "0.9.4",
3
+  "version": "0.9.5-beta.2",
4 4
   "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
5 5
   "main": "index.js",
6 6
   "scripts": {

+ 2
- 2
src/react-native-fetch-blob.podspec Просмотреть файл

@@ -1,12 +1,12 @@
1 1
 Pod::Spec.new do |s|
2 2
   s.name             = "react-native-fetch-blob"
3
-  s.version          = "0.9.4"
3
+  s.version          = "0.9.5-beta.2"
4 4
   s.summary          = "A project committed to make file acess and data transfer easier, effiecient for React Native developers."
5 5
   s.requires_arc = true
6 6
   s.license      = 'MIT'
7 7
   s.homepage     = 'n/a'
8 8
   s.authors      = { "wkh237" => "xeiyan@gmail.com" }
9
-  s.source       = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.4-beta.3'}
9
+  s.source       = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.5-beta.2'}
10 10
   s.source_files = 'ios/**/*.{h,m}'
11 11
   s.platform     = :ios, "7.0"
12 12
   s.dependency 'React/Core'

+ 54
- 0
test/test-0.9.5.js Просмотреть файл

@@ -0,0 +1,54 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+import {
5
+  StyleSheet,
6
+  Text,
7
+  View,
8
+  ScrollView,
9
+  Platform,
10
+  Dimensions,
11
+  Image,
12
+  TouchableOpacity,
13
+} from 'react-native';
14
+
15
+window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
16
+window.Blob = RNFetchBlob.polyfill.Blob
17
+
18
+const fs = RNFetchBlob.fs
19
+const { Assert, Comparer, Info, prop } = RNTest
20
+const describe = RNTest.config({
21
+  group : '0.9.5',
22
+  run : true,
23
+  expand : false,
24
+  timeout : 20000,
25
+})
26
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
27
+const dirs = RNFetchBlob.fs.dirs
28
+
29
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
30
+
31
+describe('issue #122 force response data format', (report, done) => {
32
+
33
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`, {
34
+    'RNFB-Response' : 'base64'
35
+  })
36
+  .then((res) => {
37
+    let r = RNFetchBlob.base64.decode(res.data)
38
+    report(
39
+      <Assert key="test data verify" expect="fetchblob-dev" actual={JSON.parse(r).name}/>,
40
+      <Assert key="should successfully decode the data" expect={true} actual={true}/>)
41
+    return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`)
42
+  })
43
+  .then((res) => {
44
+    report(
45
+      <Assert key="response should in format of plain-text" expect="fetchblob-dev" actual={JSON.parse(res.data).name}/>)
46
+    done()
47
+  })
48
+  .catch(() => {
49
+    report(
50
+      <Assert key="Should successfully decode the data" expect={true} actual={false}/>)
51
+    done()
52
+  })
53
+
54
+})