mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-10 03:42:00 +02:00
Inline download resume policy
This commit is contained in:
@@ -1,23 +0,0 @@
|
|||||||
package com.audiobookshelf.app.managers
|
|
||||||
|
|
||||||
internal object DownloadResumePolicy {
|
|
||||||
enum class InitialAction { COMPLETE, RESTART, FULL_DOWNLOAD, RANGE_DOWNLOAD }
|
|
||||||
|
|
||||||
fun initialAction(existingBytes: Long, expectedSize: Long): InitialAction =
|
|
||||||
when {
|
|
||||||
expectedSize > 0L && existingBytes == expectedSize -> InitialAction.COMPLETE
|
|
||||||
expectedSize > 0L && existingBytes > expectedSize -> InitialAction.RESTART
|
|
||||||
existingBytes > 0L -> InitialAction.RANGE_DOWNLOAD
|
|
||||||
else -> InitialAction.FULL_DOWNLOAD
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unsatisfiedRangeSize(contentRange: String?): Long? {
|
|
||||||
if (contentRange == null) return null
|
|
||||||
return UNSATISFIED_CONTENT_RANGE.matchEntire(contentRange)
|
|
||||||
?.groupValues
|
|
||||||
?.get(1)
|
|
||||||
?.toLongOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val UNSATISFIED_CONTENT_RANGE = Regex("bytes \\*/(\\d+)")
|
|
||||||
}
|
|
||||||
+13
-15
@@ -61,21 +61,18 @@ class InternalDownloadManager(
|
|||||||
allowRestart: Boolean
|
allowRestart: Boolean
|
||||||
) {
|
) {
|
||||||
var existingBytes = destinationFile.takeIf { it.exists() }?.length() ?: 0L
|
var existingBytes = destinationFile.takeIf { it.exists() }?.length() ?: 0L
|
||||||
when (DownloadResumePolicy.initialAction(existingBytes, expectedSize)) {
|
if (expectedSize > 0L && existingBytes == expectedSize) {
|
||||||
DownloadResumePolicy.InitialAction.COMPLETE -> {
|
progressCallback.onProgress(existingBytes, 100L)
|
||||||
progressCallback.onProgress(existingBytes, 100L)
|
progressCallback.onComplete(false)
|
||||||
progressCallback.onComplete(false)
|
return
|
||||||
|
}
|
||||||
|
if (expectedSize > 0L && existingBytes > expectedSize) {
|
||||||
|
if (!destinationFile.delete()) {
|
||||||
|
Log.e(tag, "Could not delete oversized staging file ${destinationFile.name}")
|
||||||
|
progressCallback.onComplete(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
DownloadResumePolicy.InitialAction.RESTART -> {
|
existingBytes = 0L
|
||||||
if (!destinationFile.delete()) {
|
|
||||||
Log.e(tag, "Could not delete oversized staging file ${destinationFile.name}")
|
|
||||||
progressCallback.onComplete(true)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
existingBytes = 0L
|
|
||||||
}
|
|
||||||
else -> Unit
|
|
||||||
}
|
}
|
||||||
val request =
|
val request =
|
||||||
Request.Builder()
|
Request.Builder()
|
||||||
@@ -98,8 +95,9 @@ class InternalDownloadManager(
|
|||||||
try {
|
try {
|
||||||
if (response.code == 416) {
|
if (response.code == 416) {
|
||||||
val serverSize =
|
val serverSize =
|
||||||
DownloadResumePolicy.unsatisfiedRangeSize(
|
response.header("Content-Range")
|
||||||
response.header("Content-Range"))
|
?.removePrefix("bytes */")
|
||||||
|
?.toLongOrNull()
|
||||||
if (serverSize != null && serverSize > 0L && existingBytes == serverSize) {
|
if (serverSize != null && serverSize > 0L && existingBytes == serverSize) {
|
||||||
progressCallback.onProgress(existingBytes, 100L)
|
progressCallback.onProgress(existingBytes, 100L)
|
||||||
progressCallback.onComplete(false)
|
progressCallback.onComplete(false)
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
package com.audiobookshelf.app.managers
|
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Assert.assertNull
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class DownloadResumePolicyTest {
|
|
||||||
@Test
|
|
||||||
fun completeKnownFileDoesNotIssueRequest() {
|
|
||||||
assertEquals(
|
|
||||||
DownloadResumePolicy.InitialAction.COMPLETE,
|
|
||||||
DownloadResumePolicy.initialAction(100L, 100L))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun partialAndUnknownFilesUseRange() {
|
|
||||||
assertEquals(
|
|
||||||
DownloadResumePolicy.InitialAction.RANGE_DOWNLOAD,
|
|
||||||
DownloadResumePolicy.initialAction(25L, 100L))
|
|
||||||
assertEquals(
|
|
||||||
DownloadResumePolicy.InitialAction.RANGE_DOWNLOAD,
|
|
||||||
DownloadResumePolicy.initialAction(25L, 0L))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun oversizedFileRestartsAndEmptyFileDownloadsFully() {
|
|
||||||
assertEquals(
|
|
||||||
DownloadResumePolicy.InitialAction.RESTART,
|
|
||||||
DownloadResumePolicy.initialAction(101L, 100L))
|
|
||||||
assertEquals(
|
|
||||||
DownloadResumePolicy.InitialAction.FULL_DOWNLOAD,
|
|
||||||
DownloadResumePolicy.initialAction(0L, 100L))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun parsesUnsatisfiedContentRange() {
|
|
||||||
assertEquals(787913771L, DownloadResumePolicy.unsatisfiedRangeSize("bytes */787913771"))
|
|
||||||
assertNull(DownloadResumePolicy.unsatisfiedRangeSize("bytes 0-99/100"))
|
|
||||||
assertNull(DownloadResumePolicy.unsatisfiedRangeSize(null))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user