diff --git a/build.gradle.kts b/build.gradle.kts index 62f873a..bffacb4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -73,7 +73,11 @@ dependencies { } api(project(":common")) - shadow(project(":common", configuration = "shadow")) + configurations.create("compileLib") { + defaultDependencies { + add(project(":common", configuration = "shadow")) + } + } compileOnly(project(":compat-fake-source")) } @@ -91,18 +95,18 @@ loom { } } -val java = +val javaVersion = if (stonecutter.eval(mcVersion, ">=1.20.6")) 21 else 17 java { withSourcesJar() - targetCompatibility = JavaVersion.toVersion(java) - sourceCompatibility = JavaVersion.toVersion(java) + targetCompatibility = JavaVersion.toVersion(javaVersion) + sourceCompatibility = JavaVersion.toVersion(javaVersion) } kotlin { - jvmToolchain(java) + jvmToolchain(javaVersion) } tasks.processResources { @@ -143,7 +147,8 @@ tasks { from("LICENSE") configurations = listOf( - project.configurations.shadow.get() + project.configurations.shadow.get(), + project.configurations["compileLib"] ) archiveClassifier.set("dev-all") @@ -158,8 +163,14 @@ tasks { exclude("org/sqlite/native/$it/**") } - val relocPath = "com.github.zly2006.xbackup." - relocate("org.jetbrains.exposed", relocPath + "org.jetbrains.exposed") + val relocatePath = "com.github.zly2006.xbackup.libs." + listOf( + "org.jetbrains.exposed", + "org.apache", + "io.ktor" + ).forEach { + relocate(it, relocatePath + it) + } } remapJar { diff --git a/cli/build.gradle.kts b/cli/build.gradle.kts index 42eea75..e8da20f 100644 --- a/cli/build.gradle.kts +++ b/cli/build.gradle.kts @@ -49,9 +49,6 @@ tasks { attributes[Attributes.Name.IMPLEMENTATION_VERSION.toString()] = rootProject.property("mod.version") attributes[Attributes.Name.IMPLEMENTATION_TITLE.toString()] = rootProject.property("mod.name") } - - val relocPath = "com.github.zly2006.xbackup." - relocate("org.jetbrains.exposed", relocPath + "org.jetbrains.exposed") } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 63f7428..175da5a 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -33,7 +33,7 @@ dependencies { sharedLib("io.ktor:ktor-client-content-negotiation-jvm:$ktorVersion") sharedLib("io.ktor:ktor-client-core-jvm:$ktorVersion") sharedLib("io.ktor:ktor-client-cio:$ktorVersion") - sharedLib("io.ktor:ktor-client-apache5:$ktorVersion") + sharedLib("io.ktor:ktor-client-java:$ktorVersion") sharedLib("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion") sharedLib(project(":api")) // kotlin @@ -51,8 +51,5 @@ tasks { project.configurations.shadow.get() ) archiveClassifier.set("all") - - val relocPath = "com.github.zly2006.xbackup." - relocate("org.jetbrains.exposed", relocPath + "org.jetbrains.exposed") } } diff --git a/settings.gradle.kts b/settings.gradle.kts index d135a53..e62d50d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -12,6 +12,7 @@ plugins { } rootProject.name = "X Backup" +include("common") stonecutter { kotlinController = true @@ -30,7 +31,6 @@ stonecutter { create(rootProject) } -include("common") include("compat-fake-source") include("cli") include("api") diff --git a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt index 3387ef0..97c1462 100644 --- a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt @@ -2,9 +2,10 @@ package com.github.zly2006.xbackup import com.github.zly2006.xbackup.Utils.broadcast import com.github.zly2006.xbackup.api.XBackupApi +import com.github.zly2006.xbackup.cloud.OnedriveSupport import com.mojang.brigadier.exceptions.SimpleCommandExceptionType import io.ktor.client.* -import io.ktor.client.engine.apache5.* +import io.ktor.client.engine.java.Java import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* import io.ktor.serialization.kotlinx.json.* @@ -29,6 +30,7 @@ import org.sqlite.SQLiteConfig import org.sqlite.SQLiteConnection import org.sqlite.SQLiteDataSource import java.io.File +import java.net.http.HttpClient.Redirect.NORMAL import java.nio.file.Files import java.nio.file.Path import java.nio.file.StandardCopyOption @@ -183,16 +185,11 @@ object XBackup : ModInitializer { } XBackupApi.setInstance(service) if (config.cloudBackupToken != null) { - val httpClient = HttpClient(Apache5) { + val httpClient = HttpClient(Java) { + followRedirects = true engine { - followRedirects = true - socketTimeout = 20_000 - connectTimeout = 20_000 - connectionRequestTimeout = 20_000 - customizeClient { - - } - customizeRequest { + config { + this.followRedirects(NORMAL) } } diff --git a/common/src/main/kotlin/com/github/zly2006/xbackup/OnedriveSupport.kt b/src/main/kotlin/com/github/zly2006/xbackup/cloud/OnedriveSupport.kt similarity index 78% rename from common/src/main/kotlin/com/github/zly2006/xbackup/OnedriveSupport.kt rename to src/main/kotlin/com/github/zly2006/xbackup/cloud/OnedriveSupport.kt index 07887a8..11469eb 100644 --- a/common/src/main/kotlin/com/github/zly2006/xbackup/OnedriveSupport.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/cloud/OnedriveSupport.kt @@ -1,16 +1,27 @@ -package com.github.zly2006.xbackup +package com.github.zly2006.xbackup.cloud +import com.github.zly2006.xbackup.BackupDatabaseService +import com.github.zly2006.xbackup.Config import com.github.zly2006.xbackup.api.CloudStorageProvider import com.github.zly2006.xbackup.api.XBackupKotlinAsyncApi -import io.ktor.client.* -import io.ktor.client.call.* -import io.ktor.client.request.* +import com.github.zly2006.xbackup.retry +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody import io.ktor.http.ContentType import io.ktor.http.contentType -import io.ktor.util.* -import io.ktor.util.cio.* -import kotlinx.atomicfu.atomic -import kotlinx.coroutines.* +import io.ktor.util.cio.readChannel +import io.ktor.util.toByteArray +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.asExecutor +import kotlinx.coroutines.async +import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.future.asDeferred import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject @@ -21,8 +32,25 @@ import org.jetbrains.exposed.sql.update import org.slf4j.LoggerFactory import java.net.URI import java.net.http.HttpRequest +import java.net.http.HttpResponse import java.util.zip.ZipOutputStream -import kotlin.io.path.* +import kotlin.apply +import kotlin.collections.map +import kotlin.getOrThrow +import kotlin.io.path.Path +import kotlin.io.path.absolutePathString +import kotlin.io.path.createParentDirectories +import kotlin.io.path.deleteIfExists +import kotlin.io.path.fileSize +import kotlin.io.path.name +import kotlin.io.path.outputStream +import kotlin.io.use +import kotlin.let +import kotlin.ranges.step +import kotlin.ranges.until +import kotlin.runCatching +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds private const val STEP = 10 * 1024 * 1024L @@ -67,7 +95,7 @@ class OnedriveSupport( var uploadJo: JsonObject? = null (0 until fileSize step STEP).map { start -> retry(3) { - val endInclusive = minOf(start + STEP, fileSize) - 1 + val endInclusive = kotlin.comparisons.minOf(start + STEP, fileSize) - 1 val uploadUrl = uploadSession["uploadUrl"]!!.jsonPrimitive.content val part = file.readChannel(start, endInclusive).toByteArray() log.info("Uploading part: $start-$endInclusive") @@ -77,7 +105,7 @@ class OnedriveSupport( PUT(HttpRequest.BodyPublishers.ofByteArray(part)) header("Content-Range", "bytes $start-${endInclusive}/$fileSize") }.build(), - java.net.http.HttpResponse.BodyHandlers.ofString() + HttpResponse.BodyHandlers.ofString() ).asDeferred().await() require(res.statusCode() in 200..299) { "Failed to upload part: ${res.statusCode()}: ${res.body()}" diff --git a/src/main/resources/META-INF/services/com.github.zly2006.xbackup.api.IOnedriveUtils b/src/main/resources/META-INF/services/com.github.zly2006.xbackup.api.IOnedriveUtils index 74af4a0..2c306a7 100644 --- a/src/main/resources/META-INF/services/com.github.zly2006.xbackup.api.IOnedriveUtils +++ b/src/main/resources/META-INF/services/com.github.zly2006.xbackup.api.IOnedriveUtils @@ -1 +1 @@ -com.github.zly2006.xbackup.OnedriveSupport +com.github.zly2006.xbackup.cloud.OnedriveSupport