mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2026-06-04 02:36:06 +00:00
added the ability to add header to the downloading request (#2)
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"presets": ["module:metro-react-native-babel-preset"]
|
||||
"presets": ["react-native"]
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
import com.facebook.react.bridge.WritableArray;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
||||
@@ -149,6 +150,7 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
|
||||
String id = options.getString("id");
|
||||
String url = options.getString("url");
|
||||
String destination = options.getString("destination");
|
||||
ReadableMap headers = options.getMap("headers");
|
||||
|
||||
if (id == null || url == null || destination == null) {
|
||||
Log.e(getName(), "id, url and destination must be set");
|
||||
@@ -157,6 +159,13 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
|
||||
|
||||
RNBGDTaskConfig config = new RNBGDTaskConfig(id);
|
||||
Request request = new Request(url, destination);
|
||||
if (headers != null) {
|
||||
ReadableMapKeySetIterator it = headers.keySetIterator();
|
||||
while (it.hasNextKey()) {
|
||||
String headerKey = it.nextKey();
|
||||
request.addHeader(headerKey, headers.getString(headerKey));
|
||||
}
|
||||
}
|
||||
request.setPriority(options.hasKey("priority") ? Priority.valueOf(options.getInt("priority")) : Priority.NORMAL);
|
||||
request.setNetworkType(options.hasKey("network") ? NetworkType.valueOf(options.getInt("network")) : NetworkType.ALL);
|
||||
fetch.enqueue(request, null, null);
|
||||
|
||||
@@ -4,6 +4,7 @@ const RNBackgroundDownloaderEmitter = new NativeEventEmitter(RNBackgroundDownloa
|
||||
import DownloadTask from './lib/downloadTask';
|
||||
|
||||
const tasksMap = new Map();
|
||||
let headers = {};
|
||||
|
||||
RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => {
|
||||
for (let event of events) {
|
||||
@@ -37,6 +38,10 @@ RNBackgroundDownloaderEmitter.addListener('downloadBegin', event => {
|
||||
}
|
||||
});
|
||||
|
||||
export function setHeaders(h = {}) {
|
||||
headers = h;
|
||||
}
|
||||
|
||||
export function checkForExistingDownloads() {
|
||||
return RNBackgroundDownloader.checkForExistingDownloads()
|
||||
.then(foundTasks => {
|
||||
@@ -67,6 +72,14 @@ export function download(options) {
|
||||
if (!options.id || !options.url || !options.destination) {
|
||||
throw new Error('[RNBackgroundDownloader] id, url and destination are required');
|
||||
}
|
||||
if (options.headers && typeof options.headers === 'object') {
|
||||
options.headers = {
|
||||
...headers,
|
||||
...options.headers
|
||||
};
|
||||
} else {
|
||||
options.headers = headers;
|
||||
}
|
||||
RNBackgroundDownloader.download(options);
|
||||
let task = new DownloadTask(options.id);
|
||||
tasksMap.set(options.id, task);
|
||||
@@ -91,6 +104,7 @@ export const Priority = {
|
||||
export default {
|
||||
download,
|
||||
checkForExistingDownloads,
|
||||
setHeaders,
|
||||
directories,
|
||||
Network,
|
||||
Priority
|
||||
|
||||
@@ -109,13 +109,21 @@ RCT_EXPORT_METHOD(download: (NSDictionary *) options) {
|
||||
NSString *identifier = options[@"id"];
|
||||
NSString *url = options[@"url"];
|
||||
NSString *destination = options[@"destination"];
|
||||
NSDictionary *headers = options[@"headers"];
|
||||
if (identifier == nil || url == nil || destination == nil) {
|
||||
NSLog(@"[RNFileBackgroundDownload] - [Error] id, url and destination must be set");
|
||||
NSLog(@"[RNBackgroundDownloader] - [Error] id, url and destination must be set");
|
||||
return;
|
||||
}
|
||||
|
||||
[self lazyInitSession];
|
||||
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithURL:[NSURL URLWithString:url]];
|
||||
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
|
||||
if (headers != nil) {
|
||||
for (NSString *headerKey in headers) {
|
||||
[request setValue:[headers valueForKey:headerKey] forHTTPHeaderField:headerKey];
|
||||
}
|
||||
}
|
||||
|
||||
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithRequest:request];
|
||||
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc] initWithDictionary: @{@"id": identifier, @"destination": destination}];
|
||||
taskToConfigMap[task] = taskConfig;
|
||||
idToTaskMap[identifier] = task;
|
||||
|
||||
+3
-3
@@ -43,11 +43,11 @@
|
||||
"react-native": ">=0.41.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-eslint": "^8.2.5",
|
||||
"eslint": "^4.19.1",
|
||||
"jest": "^23.1.0",
|
||||
"metro-react-native-babel-preset": "0.48.5",
|
||||
"react-native": "^0.57.8",
|
||||
"react": "16.6.3"
|
||||
"react": "16.3.1",
|
||||
"react-native": "^0.55.4"
|
||||
},
|
||||
"jest": {
|
||||
"preset": "react-native",
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>LSApplicationCategoryType</key>
|
||||
<string></string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
@@ -40,13 +42,10 @@
|
||||
</array>
|
||||
<key>UIViewControllerBasedStatusBarAppearance</key>
|
||||
<false/>
|
||||
<key>NSLocationWhenInUseUsageDescription</key>
|
||||
<string></string>
|
||||
<key>NSAppTransportSecurity</key>
|
||||
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
|
||||
<dict>
|
||||
<key>NSAllowsArbitraryLoads</key>
|
||||
<true/>
|
||||
<key>NSAllowsArbitraryLoads</key>
|
||||
<true/>
|
||||
<key>NSExceptionDomains</key>
|
||||
<dict>
|
||||
<key>localhost</key>
|
||||
|
||||
Reference in New Issue
Block a user