Browse Source

Add fetch polyfill Blob reader method #70

Fix Blob unicode data convertion #73
Ben Hsieh 8 years ago
parent
commit
1bbe2923c1

+ 12
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

30
 import java.io.IOException;
30
 import java.io.IOException;
31
 import java.io.InputStream;
31
 import java.io.InputStream;
32
 import java.io.OutputStream;
32
 import java.io.OutputStream;
33
+import java.io.UnsupportedEncodingException;
34
+import java.net.URLDecoder;
33
 import java.nio.charset.Charset;
35
 import java.nio.charset.Charset;
34
 import java.util.HashMap;
36
 import java.util.HashMap;
35
 import java.util.Map;
37
 import java.util.Map;
792
         if(encoding.equalsIgnoreCase("ascii")) {
794
         if(encoding.equalsIgnoreCase("ascii")) {
793
             return data.getBytes(Charset.forName("US-ASCII"));
795
             return data.getBytes(Charset.forName("US-ASCII"));
794
         }
796
         }
795
-        else if(encoding.equalsIgnoreCase("base64")) {
796
-            return Base64.decode(data, Base64.NO_WRAP);
797
+        else if(encoding.toLowerCase().contains("base64")) {
798
+            byte [] b = Base64.decode(data, Base64.NO_WRAP);
799
+            if(encoding.toLowerCase().contains("urlencode")) {
800
+                try {
801
+                    b = URLDecoder.decode(new String(b), "UTF-8").getBytes();
802
+                } catch (UnsupportedEncodingException e) {
803
+                    e.printStackTrace();
804
+                }
805
+            }
806
+            return b;
797
         }
807
         }
798
         else if(encoding.equalsIgnoreCase("utf8")) {
808
         else if(encoding.equalsIgnoreCase("utf8")) {
799
             return data.getBytes(Charset.forName("UTF-8"));
809
             return data.getBytes(Charset.forName("UTF-8"));

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

312
             });
312
             });
313
 
313
 
314
 
314
 
315
-            if(options.timeout > 0) {
315
+            if(options.timeout >= 0) {
316
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.MILLISECONDS);
316
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.MILLISECONDS);
317
                 clientBuilder.readTimeout(options.timeout, TimeUnit.MILLISECONDS);
317
                 clientBuilder.readTimeout(options.timeout, TimeUnit.MILLISECONDS);
318
             }
318
             }

+ 6
- 1
src/ios/RNFetchBlobFS.m View File

224
         }
224
         }
225
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
225
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
226
         NSData * content = nil;
226
         NSData * content = nil;
227
-        if([encoding isEqualToString:@"base64"]) {
227
+        if([encoding containsString:@"base64"]) {
228
             content = [[NSData alloc] initWithBase64EncodedString:data options:0];
228
             content = [[NSData alloc] initWithBase64EncodedString:data options:0];
229
+            if([encoding containsString:@"urlencode"])
230
+            {
231
+                NSString * decode = [[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];
232
+                content = [decode dataUsingEncoding:NSUTF8StringEncoding];
233
+            }
229
         }
234
         }
230
         else if([encoding isEqualToString:@"uri"]) {
235
         else if([encoding isEqualToString:@"uri"]) {
231
             NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];
236
             NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];

+ 0
- 1
src/ios/RNFetchBlobNetwork.m View File

138
     
138
     
139
     NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
139
     NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
140
     float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
140
     float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
141
-    NSLog(@"timeout = %f",timeout);
142
     if(timeout > 0)
141
     if(timeout > 0)
143
     {
142
     {
144
         defaultConfigObject.timeoutIntervalForRequest = timeout/1000;
143
         defaultConfigObject.timeoutIntervalForRequest = timeout/1000;

+ 2
- 2
src/polyfill/Blob.js View File

11
 const log = new Log('Blob')
11
 const log = new Log('Blob')
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
13
 
13
 
14
-log.disable()
14
+log.level(3)
15
 
15
 
16
 /**
16
 /**
17
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
17
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
123
       // when content type contains application/octet* or *;base64, RNFetchBlob
123
       // when content type contains application/octet* or *;base64, RNFetchBlob
124
       // fs will treat it as BASE64 encoded string binary data
124
       // fs will treat it as BASE64 encoded string binary data
125
       if(/(application\/octet|\;base64)/i.test(mime))
125
       if(/(application\/octet|\;base64)/i.test(mime))
126
-        encoding = 'base64'
126
+        encoding = 'base64+urlencode'
127
       else
127
       else
128
         data = data.toString()
128
         data = data.toString()
129
       // create cache file
129
       // create cache file

+ 17
- 1
src/polyfill/Fetch.js View File

2
 import Log from '../utils/log.js'
2
 import Log from '../utils/log.js'
3
 import fs from '../fs'
3
 import fs from '../fs'
4
 import unicode from '../utils/unicode'
4
 import unicode from '../utils/unicode'
5
+import Blob from './Blob'
5
 
6
 
6
 const log = new Log('FetchPolyfill')
7
 const log = new Log('FetchPolyfill')
7
 
8
 
18
 class RNFetchBlobFetchPolyfill {
19
 class RNFetchBlobFetchPolyfill {
19
 
20
 
20
   constructor(config:RNFetchBlobConfig) {
21
   constructor(config:RNFetchBlobConfig) {
21
-    this.build = () => (url, options) => {
22
+    this.build = () => (url, options = {}) => {
22
       options.headers = options.headers || {}
23
       options.headers = options.headers || {}
23
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
24
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
24
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
25
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
25
       return RNFetchBlob.config(config)
26
       return RNFetchBlob.config(config)
26
         .fetch(options.method, url, options.headers, options.body)
27
         .fetch(options.method, url, options.headers, options.body)
27
         .then((resp) => {
28
         .then((resp) => {
29
+          log.verbose('response', resp)
28
           let info = resp.info()
30
           let info = resp.info()
29
           return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
31
           return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
30
         })
32
         })
91
   }
93
   }
92
 }
94
 }
93
 
95
 
96
+function readBlob(resp, info):Promise<object> {
97
+  log.verbose('readBlob', resp, info)
98
+  let cType = info.headers['Content-Type']
99
+  switch (info.rnfbEncode) {
100
+    case 'base64':
101
+      return Blob.build(resp.data, { type : `${cType};BASE64` })
102
+    case 'path':
103
+      return Blob.build(RNFetchBlob.wrap(resp.data), { type : `${cType}`})
104
+    default:
105
+      return Blob.build(resp.data, { type : `${cType}`})
106
+  }
107
+}
108
+
94
 function readJSON(resp, info):Promise<object> {
109
 function readJSON(resp, info):Promise<object> {
110
+  log.verbose('readJSON', resp, info)
95
   switch (info.rnfbEncode) {
111
   switch (info.rnfbEncode) {
96
     case 'base64':
112
     case 'base64':
97
       return Promise.resolve(resp.json())
113
       return Promise.resolve(resp.json())