mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-14 13:52:09 +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
|
||||
) {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user