diff --git a/core/network/build.gradle.kts b/core/network/build.gradle.kts index d999602b..30f7a031 100644 --- a/core/network/build.gradle.kts +++ b/core/network/build.gradle.kts @@ -2,6 +2,7 @@ plugins { alias(libs.plugins.looker.jvm.library) alias(libs.plugins.looker.hilt) alias(libs.plugins.looker.lint) + alias(libs.plugins.ktor) } dependencies { diff --git a/core/network/src/main/kotlin/com/looker/network/KtorDownloader.kt b/core/network/src/main/kotlin/com/looker/network/KtorDownloader.kt index 18fd9f1e..90aa4ab8 100644 --- a/core/network/src/main/kotlin/com/looker/network/KtorDownloader.kt +++ b/core/network/src/main/kotlin/com/looker/network/KtorDownloader.kt @@ -27,14 +27,9 @@ import io.ktor.http.HttpStatusCode import io.ktor.http.etag import io.ktor.http.isSuccess import io.ktor.http.lastModified -import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.CancellationException -import io.ktor.utils.io.core.ByteReadPacket -import io.ktor.utils.io.core.isEmpty -import io.ktor.utils.io.core.readBytes +import io.ktor.utils.io.jvm.javaio.copyTo import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.isActive import kotlinx.coroutines.withContext import java.io.File import java.io.IOException @@ -88,7 +83,7 @@ internal class KtorDownloader( if (networkResponse !is NetworkResponse.Success) { return@execute networkResponse } - response.bodyAsChannel() saveTo target + response.bodyAsChannel().copyTo(target.outputStream()) validator?.validate(target) networkResponse } @@ -139,27 +134,14 @@ internal class KtorDownloader( } onDownload { read, total -> if (block != null) { - block(DataSize(read + (fileSize ?: 0L)), DataSize(total + (fileSize ?: 0L))) + block( + DataSize(read + (fileSize ?: 0L)), + DataSize((total ?: 0L) + (fileSize ?: 0L)) + ) } } } - suspend infix fun ByteReadChannel.saveTo(target: File) = - withContext(Dispatchers.IO) { - while (!isClosedForRead && isActive) { - val packet = readRemaining(DEFAULT_BUFFER_SIZE.toLong()) - packet.appendTo(target) - } - } - - suspend fun ByteReadPacket.appendTo(file: File) = - withContext(Dispatchers.IO) { - while (!isEmpty && isActive) { - val bytes = readBytes() - file.appendBytes(bytes) - } - } - fun HttpResponse.asNetworkResponse(): NetworkResponse = if (status.isSuccess() || status == HttpStatusCode.NotModified) { NetworkResponse.Success(status.value, lastModified(), etag()) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d812bd4e..79ea0f5b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,7 +32,7 @@ kotlinxDatetime = "0.6.1" kotlinxSerializationJson = "1.7.3" ksp = "2.1.0-1.0.29" ktlint = "12.1.2" -ktor = "2.3.13" +ktor = "3.0.3" libsu = "6.0.0" room = "2.6.1" shizuku = "13.0.0" @@ -83,9 +83,9 @@ kotlinx-coroutines-guava = { group = "org.jetbrains.kotlinx", name = "kotlinx-co kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" } kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" } kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } -ktor-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" } -ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" } -ktor-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" } +ktor-core = { group = "io.ktor", name = "ktor-client-core" } +ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp" } +ktor-mock = { group = "io.ktor", name = "ktor-client-mock" } libsu-core = { group = "com.github.topjohnwu.libsu", name = "core", version.ref = "libsu" } room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" } room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" } @@ -113,6 +113,7 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +ktor = { id = "io.ktor.plugin", version.ref = "ktor" } # Plugins defined by this project looker-android-application = { id = "looker.android.application", version = "unspecified" }