Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit 2c7102c

Browse files
author
Elad Gil
committed
added the ability to add header to the downloading request (#2)
1 parent 7521378 commit 2c7102c

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["module:metro-react-native-babel-preset"]
2+
"presets": ["react-native"]
33
}

android/src/main/java/com/eko/RNBackgroundDownloaderModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1212
import com.facebook.react.bridge.ReactMethod;
1313
import com.facebook.react.bridge.ReadableMap;
14+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
1415
import com.facebook.react.bridge.WritableArray;
1516
import com.facebook.react.bridge.WritableMap;
1617
import com.facebook.react.modules.core.DeviceEventManagerModule;
@@ -149,6 +150,7 @@ public void download(ReadableMap options) {
149150
String id = options.getString("id");
150151
String url = options.getString("url");
151152
String destination = options.getString("destination");
153+
ReadableMap headers = options.getMap("headers");
152154

153155
if (id == null || url == null || destination == null) {
154156
Log.e(getName(), "id, url and destination must be set");
@@ -157,6 +159,13 @@ public void download(ReadableMap options) {
157159

158160
RNBGDTaskConfig config = new RNBGDTaskConfig(id);
159161
Request request = new Request(url, destination);
162+
if (headers != null) {
163+
ReadableMapKeySetIterator it = headers.keySetIterator();
164+
while (it.hasNextKey()) {
165+
String headerKey = it.nextKey();
166+
request.addHeader(headerKey, headers.getString(headerKey));
167+
}
168+
}
160169
request.setPriority(options.hasKey("priority") ? Priority.valueOf(options.getInt("priority")) : Priority.NORMAL);
161170
request.setNetworkType(options.hasKey("network") ? NetworkType.valueOf(options.getInt("network")) : NetworkType.ALL);
162171
fetch.enqueue(request, null, null);

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const RNBackgroundDownloaderEmitter = new NativeEventEmitter(RNBackgroundDownloa
44
import DownloadTask from './lib/downloadTask';
55

66
const tasksMap = new Map();
7+
let headers = {};
78

89
RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => {
910
for (let event of events) {
@@ -37,6 +38,10 @@ RNBackgroundDownloaderEmitter.addListener('downloadBegin', event => {
3738
}
3839
});
3940

41+
export function setHeaders(h = {}) {
42+
headers = h;
43+
}
44+
4045
export function checkForExistingDownloads() {
4146
return RNBackgroundDownloader.checkForExistingDownloads()
4247
.then(foundTasks => {
@@ -67,6 +72,14 @@ export function download(options) {
6772
if (!options.id || !options.url || !options.destination) {
6873
throw new Error('[RNBackgroundDownloader] id, url and destination are required');
6974
}
75+
if (options.headers && typeof options.headers === 'object') {
76+
options.headers = {
77+
...headers,
78+
...options.headers
79+
};
80+
} else {
81+
options.headers = headers;
82+
}
7083
RNBackgroundDownloader.download(options);
7184
let task = new DownloadTask(options.id);
7285
tasksMap.set(options.id, task);
@@ -91,6 +104,7 @@ export const Priority = {
91104
export default {
92105
download,
93106
checkForExistingDownloads,
107+
setHeaders,
94108
directories,
95109
Network,
96110
Priority

ios/RNBackgroundDownloader.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,21 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
109109
NSString *identifier = options[@"id"];
110110
NSString *url = options[@"url"];
111111
NSString *destination = options[@"destination"];
112+
NSDictionary *headers = options[@"headers"];
112113
if (identifier == nil || url == nil || destination == nil) {
113-
NSLog(@"[RNFileBackgroundDownload] - [Error] id, url and destination must be set");
114+
NSLog(@"[RNBackgroundDownloader] - [Error] id, url and destination must be set");
114115
return;
115116
}
116-
117117
[self lazyInitSession];
118-
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithURL:[NSURL URLWithString:url]];
118+
119+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
120+
if (headers != nil) {
121+
for (NSString *headerKey in headers) {
122+
[request setValue:[headers valueForKey:headerKey] forHTTPHeaderField:headerKey];
123+
}
124+
}
125+
126+
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithRequest:request];
119127
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc] initWithDictionary: @{@"id": identifier, @"destination": destination}];
120128
taskToConfigMap[task] = taskConfig;
121129
idToTaskMap[identifier] = task;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
"react-native": ">=0.41.2"
4444
},
4545
"devDependencies": {
46+
"babel-eslint": "^8.2.5",
4647
"eslint": "^4.19.1",
4748
"jest": "^23.1.0",
48-
"metro-react-native-babel-preset": "0.48.5",
49-
"react-native": "^0.57.8",
50-
"react": "16.6.3"
49+
"react": "16.3.1",
50+
"react-native": "^0.55.4"
5151
},
5252
"jest": {
5353
"preset": "react-native",

testApp/ios/testApp/Info.plist

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>LSApplicationCategoryType</key>
6+
<string></string>
57
<key>CFBundleDevelopmentRegion</key>
68
<string>en</string>
79
<key>CFBundleDisplayName</key>
@@ -40,13 +42,10 @@
4042
</array>
4143
<key>UIViewControllerBasedStatusBarAppearance</key>
4244
<false/>
43-
<key>NSLocationWhenInUseUsageDescription</key>
44-
<string></string>
4545
<key>NSAppTransportSecurity</key>
46-
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
4746
<dict>
48-
<key>NSAllowsArbitraryLoads</key>
49-
<true/>
47+
<key>NSAllowsArbitraryLoads</key>
48+
<true/>
5049
<key>NSExceptionDomains</key>
5150
<dict>
5251
<key>localhost</key>

0 commit comments

Comments
 (0)