Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 1bbe292

Browse files
committed
Add fetch polyfill Blob reader method #70
Fix Blob unicode data convertion #73
1 parent f69ced2 commit 1bbe292

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.io.OutputStream;
33+
import java.io.UnsupportedEncodingException;
34+
import java.net.URLDecoder;
3335
import java.nio.charset.Charset;
3436
import java.util.HashMap;
3537
import java.util.Map;
@@ -792,8 +794,16 @@ private static byte[] stringToBytes(String data, String encoding) {
792794
if(encoding.equalsIgnoreCase("ascii")) {
793795
return data.getBytes(Charset.forName("US-ASCII"));
794796
}
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;
797807
}
798808
else if(encoding.equalsIgnoreCase("utf8")) {
799809
return data.getBytes(Charset.forName("UTF-8"));

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public Response intercept(Chain chain) throws IOException {
312312
});
313313

314314

315-
if(options.timeout > 0) {
315+
if(options.timeout >= 0) {
316316
clientBuilder.connectTimeout(options.timeout, TimeUnit.MILLISECONDS);
317317
clientBuilder.readTimeout(options.timeout, TimeUnit.MILLISECONDS);
318318
}

src/ios/RNFetchBlobFS.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,13 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
224224
}
225225
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
226226
NSData * content = nil;
227-
if([encoding isEqualToString:@"base64"]) {
227+
if([encoding containsString:@"base64"]) {
228228
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+
}
229234
}
230235
else if([encoding isEqualToString:@"uri"]) {
231236
NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];

src/ios/RNFetchBlobNetwork.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
138138

139139
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
140140
float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
141-
NSLog(@"timeout = %f",timeout);
142141
if(timeout > 0)
143142
{
144143
defaultConfigObject.timeoutIntervalForRequest = timeout/1000;

src/polyfill/Blob.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import EventTarget from './EventTarget'
1111
const log = new Log('Blob')
1212
const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
1313

14-
log.disable()
14+
log.level(3)
1515

1616
/**
1717
* A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
@@ -123,7 +123,7 @@ export default class Blob extends EventTarget {
123123
// when content type contains application/octet* or *;base64, RNFetchBlob
124124
// fs will treat it as BASE64 encoded string binary data
125125
if(/(application\/octet|\;base64)/i.test(mime))
126-
encoding = 'base64'
126+
encoding = 'base64+urlencode'
127127
else
128128
data = data.toString()
129129
// create cache file

src/polyfill/Fetch.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import RNFetchBlob from '../index.js'
22
import Log from '../utils/log.js'
33
import fs from '../fs'
44
import unicode from '../utils/unicode'
5+
import Blob from './Blob'
56

67
const log = new Log('FetchPolyfill')
78

@@ -18,13 +19,14 @@ export default class Fetch {
1819
class RNFetchBlobFetchPolyfill {
1920

2021
constructor(config:RNFetchBlobConfig) {
21-
this.build = () => (url, options) => {
22+
this.build = () => (url, options = {}) => {
2223
options.headers = options.headers || {}
2324
options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
2425
options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
2526
return RNFetchBlob.config(config)
2627
.fetch(options.method, url, options.headers, options.body)
2728
.then((resp) => {
29+
log.verbose('response', resp)
2830
let info = resp.info()
2931
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
3032
})
@@ -91,7 +93,21 @@ function readText(resp, info):Promise<string> {
9193
}
9294
}
9395

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+
94109
function readJSON(resp, info):Promise<object> {
110+
log.verbose('readJSON', resp, info)
95111
switch (info.rnfbEncode) {
96112
case 'base64':
97113
return Promise.resolve(resp.json())

0 commit comments

Comments
 (0)