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

Commit

Permalink
Merge branch '0.10.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Jun 26, 2017
2 parents 5e41837 + 2c16a94 commit 0ea4a4a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 37 deletions.
14 changes: 11 additions & 3 deletions android.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@ function actionViewIntent(path:string, mime:string = 'text/plain') {
if(Platform.OS === 'android')
return RNFetchBlob.actionViewIntent(path, mime)
else
return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
return Promise.reject('RNFetchBlob.android.actionViewIntent only supports Android.')
}

function getContentIntent(mime:string) {
if(Platform.OS === 'android')
return RNFetchBlob.getContentIntent(mime)
else
return Promise.reject('RNFetchBlob.getContentIntent only supports Android.')
return Promise.reject('RNFetchBlob.android.getContentIntent only supports Android.')
}

function addCompleteDownload(config) {
if(Platform.OS === 'android')
return RNFetchBlob.addCompleteDownload(config)
else
return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
}


export default {
actionViewIntent,
getContentIntent
getContentIntent,
addCompleteDownload
}
29 changes: 29 additions & 0 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.RNFetchBlob;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.net.Uri;

Expand All @@ -15,6 +16,7 @@
import com.facebook.react.bridge.ReadableMap;

// Cookies
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.CookieJarContainer;
import com.facebook.react.modules.network.OkHttpClientProvider;
Expand Down Expand Up @@ -341,4 +343,31 @@ public void getContentIntent(String mime, Promise promise) {

}

@ReactMethod
public void addCompleteDownload (ReadableMap config, Promise promise) {
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
if(path == null) {
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
return;
}
try {
WritableMap stat = RNFetchBlobFS.statFile(path);
dm.addCompletedDownload(
config.hasKey("title") ? config.getString("title") : "",
config.hasKey("description") ? config.getString("description") : "",
true,
config.hasKey("mime") ? config.getString("mime") : null,
path,
Long.valueOf(stat.getString("size")),
config.hasKey("showNotification") && config.getBoolean("showNotification")
);
promise.resolve(null);
}
catch(Exception ex) {
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
}

}

}
17 changes: 11 additions & 6 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,7 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
while ((cursor = fs.read(buffer)) != -1) {
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
String chunk = new String(buffer);
if(cursor != bufferSize) {
chunk = chunk.substring(0, cursor);
}
String chunk = new String(buffer, 0, cursor);
emitStreamEvent(streamId, "data", chunk);
if(tick > 0)
SystemClock.sleep(tick);
Expand Down Expand Up @@ -882,13 +879,21 @@ static boolean isAsset(String path) {
return false;
}

/**
* Normalize the path, remove URI scheme (xxx://) so that we can handle it.
* @param path URI string.
* @return Normalized string
*/
static String normalizePath(String path) {
if(path == null)
return null;
Uri uri = Uri.parse(path);
if(uri.getScheme() == null) {
if(!path.matches("\\w+\\:.*"))
return path;
if(path.startsWith("file://")) {
return path.replace("file://", "");
}

Uri uri = Uri.parse(path);
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
return path;
}
Expand Down
22 changes: 8 additions & 14 deletions ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ @interface RNFetchBlobNetwork ()
NSMutableArray * redirects;
ResponseFormat responseFormat;
BOOL * followRedirect;
BOOL backgroundTask;
}

@end
Expand Down Expand Up @@ -168,6 +169,8 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
self.expectedBytes = 0;
self.receivedBytes = 0;
self.options = options;

backgroundTask = [options valueForKey:@"IOSBackgroundTask"] == nil ? NO : [[options valueForKey:@"IOSBackgroundTask"] boolValue];
followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue];
isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
redirects = [[NSMutableArray alloc] init];
Expand All @@ -192,13 +195,12 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options

// the session trust any SSL certification
NSURLSessionConfiguration *defaultConfigObject;
if(!followRedirect)
{
defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
}
else

defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

if(backgroundTask)
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
}

// set request timeout
Expand Down Expand Up @@ -247,14 +249,6 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
__block UIApplication * app = [UIApplication sharedApplication];

// #115 handling task expired when application entering backgound for a long time
UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
[expirationTable setObject:task forKey:taskId];
// comment out this one as it might cause app crash #271
// [app endBackgroundTask:tid];
}];

}

// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
Expand Down
42 changes: 28 additions & 14 deletions polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,38 @@ class RNFetchBlobFetchPolyfill {
// task is a progress reportable and cancellable Promise, however,
// task.then is not, so we have to extend task.then with progress and
// cancel function
let task = promise
let progressHandler, uploadHandler, cancelHandler
let statefulPromise = promise
.then((body) => {
return RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, body)
let task = RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, body)
if(progressHandler)
task.progress(progressHandler)
if(uploadHandler)
task.uploadProgress(uploadHandler)
if(cancelHandler)
task.cancel()
return task.then((resp) => {
log.verbose('response', resp)
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})
})

let statefulPromise = task.then((resp) => {
log.verbose('response', resp)
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})

// extend task.then progress with report and cancelling functions
statefulPromise.cancel = task.cancel
statefulPromise.progress = task.progress
statefulPromise.uploadProgress = task.uploadProgress
statefulPromise.progress = (fn) => {
progressHandler = fn
}
statefulPromise.uploadProgress = (fn) => {
uploadHandler = fn
}
statefulPromise.cancel = () => {
cancelHandler = true
if(task.cancel)
task.cancel()
}

return statefulPromise

Expand Down

0 comments on commit 0ea4a4a

Please sign in to comment.