Fix HTTP Error 416 not being handled properly for resumable downloads

This commit is contained in:
AntsyLich
2026-07-10 00:02:34 +06:00
parent 52f827c6ff
commit 4a66b8b5df
2 changed files with 10 additions and 12 deletions
@@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.library.LibraryUpdateNotifier
import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.network.HttpException
import eu.kanade.tachiyomi.source.UnmeteredSource
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
@@ -42,7 +43,6 @@ import mihon.core.archive.ZipWriter
import nl.adaptivity.xmlutil.serialization.XML
import okhttp3.Response
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.storage.extension
import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.core.common.util.lang.launchNow
import tachiyomi.core.common.util.lang.withIOContext
@@ -482,22 +482,19 @@ class Downloader(
val file = tmpDir.findFile("$filename.tmp")
?: tmpDir.createFile("$filename.tmp")!!
val response = source.getImage(page, file.length())
try {
response.body
.source()
.saveTo(
source.getImage(page, file.length()).use {
it.body.source().saveTo(
// If the server supports partial downloads (HTTP 206),
// append to the existing file.
// Otherwise, start from scratch and overwrite the file.
file.openOutputStream(response.code == 206),
stream = file.openOutputStream(it.code == 206),
)
val extension = getImageExtension(response, file)
file.renameTo("$filename.$extension")
} catch (e: Exception) {
response.close()
if (response.code == 416) {
val extension = getImageExtension(it, file)
file.renameTo("$filename.$extension")
}
} catch (e: HttpException) {
if (e.code == 416) {
file.delete()
}
throw e