diff --git a/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadResumePolicy.kt b/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadResumePolicy.kt deleted file mode 100644 index 337cf351..00000000 --- a/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadResumePolicy.kt +++ /dev/null @@ -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+)") -} diff --git a/android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt b/android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt index 1f251064..aa42fe0e 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt @@ -61,21 +61,18 @@ class InternalDownloadManager( allowRestart: Boolean ) { var existingBytes = destinationFile.takeIf { it.exists() }?.length() ?: 0L - when (DownloadResumePolicy.initialAction(existingBytes, expectedSize)) { - DownloadResumePolicy.InitialAction.COMPLETE -> { - progressCallback.onProgress(existingBytes, 100L) - progressCallback.onComplete(false) + if (expectedSize > 0L && existingBytes == expectedSize) { + progressCallback.onProgress(existingBytes, 100L) + 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 } - DownloadResumePolicy.InitialAction.RESTART -> { - if (!destinationFile.delete()) { - Log.e(tag, "Could not delete oversized staging file ${destinationFile.name}") - progressCallback.onComplete(true) - return - } - existingBytes = 0L - } - else -> Unit + existingBytes = 0L } val request = Request.Builder() @@ -98,8 +95,9 @@ class InternalDownloadManager( try { if (response.code == 416) { val serverSize = - DownloadResumePolicy.unsatisfiedRangeSize( - response.header("Content-Range")) + response.header("Content-Range") + ?.removePrefix("bytes */") + ?.toLongOrNull() if (serverSize != null && serverSize > 0L && existingBytes == serverSize) { progressCallback.onProgress(existingBytes, 100L) progressCallback.onComplete(false) diff --git a/android/app/src/test/java/com/audiobookshelf/app/managers/DownloadResumePolicyTest.kt b/android/app/src/test/java/com/audiobookshelf/app/managers/DownloadResumePolicyTest.kt deleted file mode 100644 index 092f495f..00000000 --- a/android/app/src/test/java/com/audiobookshelf/app/managers/DownloadResumePolicyTest.kt +++ /dev/null @@ -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)) - } -}