@kesha-antonov/react-native-background-downloader 4.6.3 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -7
- package/android/src/main/java/com/eko/DownloadConstants.kt +13 -2
- package/android/src/main/java/com/eko/DownloadResult.kt +3 -1
- package/android/src/main/java/com/eko/Downloader.kt +25 -4
- package/android/src/main/java/com/eko/ProgressReporter.kt +14 -35
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +162 -76
- package/android/src/main/java/com/eko/ResumableDownloadService.kt +2 -1
- package/android/src/main/java/com/eko/ResumableDownloader.kt +75 -3
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +34 -8
- package/android/src/main/java/com/eko/Uploader.kt +15 -11
- package/android/src/main/java/com/eko/handlers/OnBegin.kt +6 -1
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +3 -1
- package/android/src/main/java/com/eko/uidt/UIDTJobManager.kt +18 -2
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +5 -13
- package/android/src/main/java/com/eko/utils/StorageManager.kt +5 -0
- package/android/src/test/java/com/eko/DownloaderRecoveryTest.kt +78 -0
- package/android/src/test/java/com/eko/DownloaderResumeTest.kt +97 -0
- package/android/src/test/java/com/eko/ResumableDownloaderStallTest.kt +145 -0
- package/android/src/test/java/com/eko/UploaderMultipartTest.kt +84 -0
- package/android/src/test/java/com/eko/handlers/OnBeginTest.kt +51 -0
- package/android/src/test/java/com/eko/uidt/CancelDownloadReceiverTest.kt +84 -0
- package/android/src/test/java/com/eko/utils/RedirectResolverTest.kt +67 -0
- package/ios/RNBGDTaskConfig.h +2 -0
- package/ios/RNBGDTaskConfig.mm +6 -0
- package/ios/RNBackgroundDownloader.mm +640 -90
- package/lib/DownloadTask.d.ts +2 -0
- package/lib/DownloadTask.js +21 -1
- package/lib/UploadTask.d.ts +2 -0
- package/lib/UploadTask.js +25 -3
- package/lib/index.d.ts +1 -1
- package/lib/index.js +83 -70
- package/lib/tasks.d.ts +4 -0
- package/lib/tasks.js +7 -0
- package/package.json +14 -11
- package/plugin/build/index.d.ts +1 -0
- package/plugin/build/index.js +18 -10
- package/react-native-background-downloader.podspec +8 -0
- package/src/DownloadTask.ts +22 -1
- package/src/UploadTask.ts +25 -3
- package/src/index.ts +74 -43
- package/src/tasks.ts +7 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
package com.eko.uidt
|
|
2
|
+
|
|
3
|
+
import android.app.job.JobScheduler
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import androidx.test.core.app.ApplicationProvider
|
|
7
|
+
import com.eko.ResumableDownloader
|
|
8
|
+
import org.junit.After
|
|
9
|
+
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertTrue
|
|
11
|
+
import org.junit.Before
|
|
12
|
+
import org.junit.Test
|
|
13
|
+
import org.junit.runner.RunWith
|
|
14
|
+
import org.robolectric.RobolectricTestRunner
|
|
15
|
+
import org.robolectric.annotation.Config
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Cancel on the notification has to tell JS about any download it stops. A job the
|
|
19
|
+
* system stopped (e.g. waiting for Wi-Fi) isn't running, but is still scheduled and
|
|
20
|
+
* still shows the notification.
|
|
21
|
+
*/
|
|
22
|
+
@RunWith(RobolectricTestRunner::class)
|
|
23
|
+
@Config(sdk = [34])
|
|
24
|
+
class CancelDownloadReceiverTest {
|
|
25
|
+
|
|
26
|
+
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
|
27
|
+
private val jobScheduler: JobScheduler
|
|
28
|
+
get() = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
|
29
|
+
|
|
30
|
+
private val errors = mutableListOf<Pair<String, Int>>()
|
|
31
|
+
|
|
32
|
+
@Before
|
|
33
|
+
fun setUp() {
|
|
34
|
+
jobScheduler.cancelAll()
|
|
35
|
+
UIDTJobRegistry.activeJobs.clear()
|
|
36
|
+
UIDTJobRegistry.downloadListener = object : ResumableDownloader.DownloadListener {
|
|
37
|
+
override fun onBegin(id: String, expectedBytes: Long, headers: Map<String, String>) = Unit
|
|
38
|
+
override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
39
|
+
override fun onComplete(id: String, location: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
40
|
+
override fun onError(id: String, error: String, errorCode: Int) {
|
|
41
|
+
errors.add(id to errorCode)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@After
|
|
47
|
+
fun tearDown() {
|
|
48
|
+
UIDTJobRegistry.downloadListener = null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private fun cancel(configId: String) {
|
|
52
|
+
CancelDownloadReceiver().onReceive(
|
|
53
|
+
context,
|
|
54
|
+
Intent(CancelDownloadReceiver.ACTION_CANCEL_DOWNLOAD)
|
|
55
|
+
.putExtra(CancelDownloadReceiver.EXTRA_CONFIG_ID, configId)
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `cancelling a scheduled job that isn't running reports the cancel and removes the job`() {
|
|
61
|
+
assertTrue(
|
|
62
|
+
UIDTJobManager.scheduleDownload(
|
|
63
|
+
context = context,
|
|
64
|
+
configId = "waiting",
|
|
65
|
+
url = "https://example.com/file",
|
|
66
|
+
destination = "/tmp/waiting",
|
|
67
|
+
headers = emptyMap(),
|
|
68
|
+
isAllowedOverMetered = false
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
cancel("waiting")
|
|
73
|
+
|
|
74
|
+
assertEquals(listOf("waiting" to -1), errors)
|
|
75
|
+
assertTrue(jobScheduler.allPendingJobs.isEmpty())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
fun `cancelling a download that already ended reports nothing`() {
|
|
80
|
+
cancel("finished")
|
|
81
|
+
|
|
82
|
+
assertTrue(errors.isEmpty())
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
package com.eko.utils
|
|
2
|
+
|
|
3
|
+
import okhttp3.mockwebserver.Dispatcher
|
|
4
|
+
import okhttp3.mockwebserver.MockResponse
|
|
5
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
6
|
+
import okhttp3.mockwebserver.RecordedRequest
|
|
7
|
+
import org.junit.After
|
|
8
|
+
import org.junit.Assert.assertEquals
|
|
9
|
+
import org.junit.Before
|
|
10
|
+
import org.junit.Test
|
|
11
|
+
import org.junit.runner.RunWith
|
|
12
|
+
import org.robolectric.RobolectricTestRunner
|
|
13
|
+
import org.robolectric.annotation.Config
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A Location header may be relative. It has to be resolved against the URL that
|
|
17
|
+
* answered with it, keeping that URL's scheme, host, port and directory.
|
|
18
|
+
*/
|
|
19
|
+
@RunWith(RobolectricTestRunner::class)
|
|
20
|
+
@Config(sdk = [34])
|
|
21
|
+
class RedirectResolverTest {
|
|
22
|
+
|
|
23
|
+
private lateinit var server: MockWebServer
|
|
24
|
+
private val redirects = mutableMapOf<String, String>()
|
|
25
|
+
|
|
26
|
+
@Before
|
|
27
|
+
fun startServer() {
|
|
28
|
+
server = MockWebServer()
|
|
29
|
+
server.dispatcher = object : Dispatcher() {
|
|
30
|
+
override fun dispatch(request: RecordedRequest): MockResponse {
|
|
31
|
+
val location = redirects[request.path]
|
|
32
|
+
?: return MockResponse().setResponseCode(200)
|
|
33
|
+
return MockResponse().setResponseCode(302).setHeader("Location", location)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
server.start()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@After
|
|
40
|
+
fun stopServer() {
|
|
41
|
+
server.shutdown()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private fun resolve(path: String) = RedirectResolver.resolve(server.url(path).toString(), 5, null)
|
|
45
|
+
|
|
46
|
+
@Test
|
|
47
|
+
fun `a relative location keeps the port and directory`() {
|
|
48
|
+
redirects["/files/start"] = "next/file.zip"
|
|
49
|
+
|
|
50
|
+
assertEquals(server.url("/files/next/file.zip").toString(), resolve("/files/start"))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Test
|
|
54
|
+
fun `an absolute path keeps the port`() {
|
|
55
|
+
redirects["/files/start"] = "/other/file.zip"
|
|
56
|
+
|
|
57
|
+
assertEquals(server.url("/other/file.zip").toString(), resolve("/files/start"))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Test
|
|
61
|
+
fun `a chain of relative redirects resolves each step against the previous one`() {
|
|
62
|
+
redirects["/a/start"] = "../b/step"
|
|
63
|
+
redirects["/b/step"] = "final.zip"
|
|
64
|
+
|
|
65
|
+
assertEquals(server.url("/b/final.zip").toString(), resolve("/a/start"))
|
|
66
|
+
}
|
|
67
|
+
}
|
package/ios/RNBGDTaskConfig.h
CHANGED
|
@@ -17,6 +17,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
17
17
|
// iOS Data Protection level key for the saved file
|
|
18
18
|
// (e.g. NSFileProtectionCompleteUntilFirstUserAuthentication). nil = library default.
|
|
19
19
|
@property (nonatomic, copy, nullable) NSString *dataProtection;
|
|
20
|
+
// Per-download cellular access (isAllowedOverMetered), kept so rebuilt requests honor it after a relaunch
|
|
21
|
+
@property (nonatomic, assign) BOOL allowsCellularAccess;
|
|
20
22
|
|
|
21
23
|
- (instancetype)initWithDictionary:(NSDictionary *)dict;
|
|
22
24
|
|
package/ios/RNBGDTaskConfig.mm
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
self.state = NSURLSessionTaskStateRunning;
|
|
24
24
|
self.errorCode = 0;
|
|
25
25
|
self.hasResumeData = NO;
|
|
26
|
+
self.allowsCellularAccess = YES;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
return self;
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
[aCoder encodeInteger:self.state forKey:@"state"];
|
|
42
43
|
[aCoder encodeInteger:self.errorCode forKey:@"errorCode"];
|
|
43
44
|
[aCoder encodeBool:self.hasResumeData forKey:@"hasResumeData"];
|
|
45
|
+
[aCoder encodeBool:self.allowsCellularAccess forKey:@"allowsCellularAccess"];
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder
|
|
@@ -61,6 +63,10 @@
|
|
|
61
63
|
self.state = [aDecoder decodeIntegerForKey:@"state"];
|
|
62
64
|
self.errorCode = [aDecoder decodeIntegerForKey:@"errorCode"];
|
|
63
65
|
self.hasResumeData = [aDecoder decodeBoolForKey:@"hasResumeData"];
|
|
66
|
+
// Configs saved before this field existed had no per-download restriction
|
|
67
|
+
self.allowsCellularAccess = [aDecoder containsValueForKey:@"allowsCellularAccess"]
|
|
68
|
+
? [aDecoder decodeBoolForKey:@"allowsCellularAccess"]
|
|
69
|
+
: YES;
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
return self;
|