ソースを参照

Add fetch polyfill Blob reader method #70

Fix Blob unicode data convertion #73
Ben Hsieh 8 年 前
コミット
1bbe2923c1

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

@@ -30,6 +30,8 @@ import java.io.FileOutputStream;
30 30
 import java.io.IOException;
31 31
 import java.io.InputStream;
32 32
 import java.io.OutputStream;
33
+import java.io.UnsupportedEncodingException;
34
+import java.net.URLDecoder;
33 35
 import java.nio.charset.Charset;
34 36
 import java.util.HashMap;
35 37
 import java.util.Map;
@@ -792,8 +794,16 @@ public class RNFetchBlobFS {
792 794
         if(encoding.equalsIgnoreCase("ascii")) {
793 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 808
         else if(encoding.equalsIgnoreCase("utf8")) {
799 809
             return data.getBytes(Charset.forName("UTF-8"));

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

@@ -312,7 +312,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
312 312
             });
313 313
 
314 314
 
315
-            if(options.timeout > 0) {
315
+            if(options.timeout >= 0) {
316 316
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.MILLISECONDS);
317 317
                 clientBuilder.readTimeout(options.timeout, TimeUnit.MILLISECONDS);
318 318
             }

+ 6
- 1
src/ios/RNFetchBlobFS.m ファイルの表示

@@ -224,8 +224,13 @@ NSMutableDictionary *fileStreams = nil;
224 224
         }
225 225
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
226 226
         NSData * content = nil;
227
-        if([encoding isEqualToString:@"base64"]) {
227
+        if([encoding containsString:@"base64"]) {
228 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 235
         else if([encoding isEqualToString:@"uri"]) {
231 236
             NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];

+ 0
- 1
src/ios/RNFetchBlobNetwork.m ファイルの表示

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

+ 2
- 2
src/polyfill/Blob.js ファイルの表示

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

+ 17
- 1
src/polyfill/Fetch.js ファイルの表示

@@ -2,6 +2,7 @@ import RNFetchBlob from '../index.js'
2 2
 import Log from '../utils/log.js'
3 3
 import fs from '../fs'
4 4
 import unicode from '../utils/unicode'
5
+import Blob from './Blob'
5 6
 
6 7
 const log = new Log('FetchPolyfill')
7 8
 
@@ -18,13 +19,14 @@ export default class Fetch {
18 19
 class RNFetchBlobFetchPolyfill {
19 20
 
20 21
   constructor(config:RNFetchBlobConfig) {
21
-    this.build = () => (url, options) => {
22
+    this.build = () => (url, options = {}) => {
22 23
       options.headers = options.headers || {}
23 24
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
24 25
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
25 26
       return RNFetchBlob.config(config)
26 27
         .fetch(options.method, url, options.headers, options.body)
27 28
         .then((resp) => {
29
+          log.verbose('response', resp)
28 30
           let info = resp.info()
29 31
           return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
30 32
         })
@@ -91,7 +93,21 @@ function readText(resp, info):Promise<string> {
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 109
 function readJSON(resp, info):Promise<object> {
110
+  log.verbose('readJSON', resp, info)
95 111
   switch (info.rnfbEncode) {
96 112
     case 'base64':
97 113
       return Promise.resolve(resp.json())