Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Dec 16, 2024
1 parent 1954b9b commit e4b8134
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 38 deletions.
27 changes: 19 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand All @@ -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 {
Expand Down Expand Up @@ -143,7 +147,8 @@ tasks {
from("LICENSE")

configurations = listOf(
project.configurations.shadow.get()
project.configurations.shadow.get(),
project.configurations["compileLib"]
)
archiveClassifier.set("dev-all")

Expand 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 {
Expand Down
3 changes: 0 additions & 3 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
5 changes: 1 addition & 4 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
}

rootProject.name = "X Backup"
include("common")

stonecutter {
kotlinController = true
Expand All @@ -30,7 +31,6 @@ stonecutter {
create(rootProject)
}

include("common")
include("compat-fake-source")
include("cli")
include("api")
17 changes: 7 additions & 10 deletions src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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()}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.github.zly2006.xbackup.OnedriveSupport
com.github.zly2006.xbackup.cloud.OnedriveSupport

0 comments on commit e4b8134

Please sign in to comment.