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

Commit

Permalink
Add fetch polyfill Blob reader method #70
Browse files Browse the repository at this point in the history
Fix Blob unicode data convertion #73
  • Loading branch information
wkh237 committed Aug 4, 2016
1 parent f69ced2 commit 1bbe292
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -792,8 +794,16 @@ private static byte[] stringToBytes(String data, String encoding) {
if(encoding.equalsIgnoreCase("ascii")) {
return data.getBytes(Charset.forName("US-ASCII"));
}
else if(encoding.equalsIgnoreCase("base64")) {
return Base64.decode(data, Base64.NO_WRAP);
else if(encoding.toLowerCase().contains("base64")) {
byte [] b = Base64.decode(data, Base64.NO_WRAP);
if(encoding.toLowerCase().contains("urlencode")) {
try {
b = URLDecoder.decode(new String(b), "UTF-8").getBytes();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return b;
}
else if(encoding.equalsIgnoreCase("utf8")) {
return data.getBytes(Charset.forName("UTF-8"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public Response intercept(Chain chain) throws IOException {
});


if(options.timeout > 0) {
if(options.timeout >= 0) {
clientBuilder.connectTimeout(options.timeout, TimeUnit.MILLISECONDS);
clientBuilder.readTimeout(options.timeout, TimeUnit.MILLISECONDS);
}
Expand Down
7 changes: 6 additions & 1 deletion src/ios/RNFetchBlobFS.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,13 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
}
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
NSData * content = nil;
if([encoding isEqualToString:@"base64"]) {
if([encoding containsString:@"base64"]) {
content = [[NSData alloc] initWithBase64EncodedString:data options:0];
if([encoding containsString:@"urlencode"])
{
NSString * decode = [[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];
content = [decode dataUsingEncoding:NSUTF8StringEncoding];
}
}
else if([encoding isEqualToString:@"uri"]) {
NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];
Expand Down
1 change: 0 additions & 1 deletion src/ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
NSLog(@"timeout = %f",timeout);
if(timeout > 0)
{
defaultConfigObject.timeoutIntervalForRequest = timeout/1000;
Expand Down
4 changes: 2 additions & 2 deletions src/polyfill/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import EventTarget from './EventTarget'
const log = new Log('Blob')
const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'

log.disable()
log.level(3)

/**
* A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
Expand Down Expand Up @@ -123,7 +123,7 @@ export default class Blob extends EventTarget {
// when content type contains application/octet* or *;base64, RNFetchBlob
// fs will treat it as BASE64 encoded string binary data
if(/(application\/octet|\;base64)/i.test(mime))
encoding = 'base64'
encoding = 'base64+urlencode'
else
data = data.toString()
// create cache file
Expand Down
18 changes: 17 additions & 1 deletion src/polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import RNFetchBlob from '../index.js'
import Log from '../utils/log.js'
import fs from '../fs'
import unicode from '../utils/unicode'
import Blob from './Blob'

const log = new Log('FetchPolyfill')

Expand All @@ -18,13 +19,14 @@ export default class Fetch {
class RNFetchBlobFetchPolyfill {

constructor(config:RNFetchBlobConfig) {
this.build = () => (url, options) => {
this.build = () => (url, options = {}) => {
options.headers = options.headers || {}
options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
return RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, options.body)
.then((resp) => {
log.verbose('response', resp)
let info = resp.info()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})
Expand Down Expand Up @@ -91,7 +93,21 @@ function readText(resp, info):Promise<string> {
}
}

function readBlob(resp, info):Promise<object> {
log.verbose('readBlob', resp, info)
let cType = info.headers['Content-Type']
switch (info.rnfbEncode) {
case 'base64':
return Blob.build(resp.data, { type : `${cType};BASE64` })
case 'path':
return Blob.build(RNFetchBlob.wrap(resp.data), { type : `${cType}`})
default:
return Blob.build(resp.data, { type : `${cType}`})
}
}

function readJSON(resp, info):Promise<object> {
log.verbose('readJSON', resp, info)
switch (info.rnfbEncode) {
case 'base64':
return Promise.resolve(resp.json())
Expand Down

0 comments on commit 1bbe292

Please sign in to comment.