From 8bc10f7bdf1ae8aec364f0aa36f767a51a775c23 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 31 Mar 2020 10:25:24 +0800 Subject: [PATCH 1/2] Fix cuiCloud --- buildSrc/src/main/kotlin/upload/CuiCloud.kt | 53 ++++++++++++++++----- buildSrc/src/main/kotlin/upload/GitHub.kt | 22 +++++---- gradle/wrapper/gradle-wrapper.properties | 4 +- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/buildSrc/src/main/kotlin/upload/CuiCloud.kt b/buildSrc/src/main/kotlin/upload/CuiCloud.kt index 9c08aa93c..54feca358 100644 --- a/buildSrc/src/main/kotlin/upload/CuiCloud.kt +++ b/buildSrc/src/main/kotlin/upload/CuiCloud.kt @@ -9,16 +9,20 @@ package upload +import io.ktor.client.request.forms.MultiPartFormDataContent +import io.ktor.client.request.forms.formData +import io.ktor.client.request.post +import io.ktor.client.statement.HttpResponse +import io.ktor.http.isSuccess import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.gradle.api.Project import org.gradle.kotlin.dsl.provideDelegate -import org.jsoup.Connection -import org.jsoup.Jsoup import java.io.File import java.util.* +@Suppress("DEPRECATION") object CuiCloud { private fun getUrl(project: Project): String { kotlin.runCatching { @@ -83,23 +87,48 @@ object CuiCloud { } } + @UseExperimental(ExperimentalStdlibApi::class) private suspend fun uploadToCuiCloud( cuiCloudUrl: String, cuiToken: String, filePath: String, content: ByteArray ) { + println("uploading to $cuiCloudUrl") + println("filePath=$filePath") + println("key=$cuiToken") + println("content=${content.size / 1024 / 1024} MB") + val response = withContext(Dispatchers.IO) { - Jsoup.connect(cuiCloudUrl).method(Connection.Method.POST) - .data("base64", Base64.getEncoder().encodeToString(content)) - .data("filePath", filePath) - .data("key", cuiToken) - .timeout(Int.MAX_VALUE) - .execute() + Http.post(cuiCloudUrl) { + body = MultiPartFormDataContent( + formData { + append("base64", Base64.getEncoder().encodeToString(content)) + append("filePath", filePath) + append("key", cuiToken) + } + ) + } } - if (response.statusCode() != 200) { - println(response.body()) - error("Cui Cloud Does Not Return 200") + if (response.status.isSuccess()) { + println(response.status) + + val buffer = ByteArray(4096) + val resp = buildList { + while (true) { + val read = response.content.readAvailable(buffer, 0, buffer.size) + if (read == -1) { + break + } + addAll(buffer.toList().take(read)) + } + } + println(String(resp.toByteArray())) + error("Cui cloud response: ${response.status}") } } -} \ No newline at end of file +} + +inline fun buildList(builderAction: MutableList.() -> Unit): List { + return ArrayList().apply(builderAction) +} diff --git a/buildSrc/src/main/kotlin/upload/GitHub.kt b/buildSrc/src/main/kotlin/upload/GitHub.kt index d273f22f7..486247e33 100644 --- a/buildSrc/src/main/kotlin/upload/GitHub.kt +++ b/buildSrc/src/main/kotlin/upload/GitHub.kt @@ -18,6 +18,17 @@ import org.jsoup.Jsoup import java.io.File import java.util.* +internal val Http = HttpClient(CIO) { + engine { + requestTimeout = 600_000 + } + install(HttpTimeout) { + socketTimeoutMillis = 600_000 + requestTimeoutMillis = 600_000 + connectTimeoutMillis = 600_000 + } +} + object GitHub { private fun getGithubToken(project: Project): String { @@ -53,16 +64,7 @@ object GitHub { fun upload(file: File, url: String, project: Project) = runBlocking { val token = getGithubToken(project) println("token.length=${token.length}") - HttpClient(CIO) { - engine { - requestTimeout = 600_000 - } - install(HttpTimeout) { - socketTimeoutMillis = 600_000 - requestTimeoutMillis = 600_000 - connectTimeoutMillis = 600_000 - } - }.put("$url?access_token=$token") { + Http.put("$url?access_token=$token") { val sha = getGithubSha("mirai-repo", "shadow/${project.name}/${file.name}", "master", project) println("sha=$sha") val content = String(Base64.getEncoder().encode(file.readBytes())) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5c2c5bd90..63d90d376 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ -#Thu Feb 06 14:10:33 CST 2020 -distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-bin.zip +#Tue Mar 31 10:18:00 CST 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists From ef86de230d0ae1bfca4de6a7292308dc0273088b Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 31 Mar 2020 10:30:11 +0800 Subject: [PATCH 2/2] Fix cuiCloud --- buildSrc/src/main/kotlin/upload/CuiCloud.kt | 25 ++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/buildSrc/src/main/kotlin/upload/CuiCloud.kt b/buildSrc/src/main/kotlin/upload/CuiCloud.kt index 54feca358..71a3c89bd 100644 --- a/buildSrc/src/main/kotlin/upload/CuiCloud.kt +++ b/buildSrc/src/main/kotlin/upload/CuiCloud.kt @@ -94,9 +94,7 @@ object CuiCloud { filePath: String, content: ByteArray ) { - println("uploading to $cuiCloudUrl") println("filePath=$filePath") - println("key=$cuiToken") println("content=${content.size / 1024 / 1024} MB") val response = withContext(Dispatchers.IO) { @@ -110,20 +108,21 @@ object CuiCloud { ) } } - if (response.status.isSuccess()) { - println(response.status) + println(response.status) - val buffer = ByteArray(4096) - val resp = buildList { - while (true) { - val read = response.content.readAvailable(buffer, 0, buffer.size) - if (read == -1) { - break - } - addAll(buffer.toList().take(read)) + val buffer = ByteArray(4096) + val resp = buildList { + while (true) { + val read = response.content.readAvailable(buffer, 0, buffer.size) + if (read == -1) { + break } + addAll(buffer.toList().take(read)) } - println(String(resp.toByteArray())) + } + println(String(resp.toByteArray())) + + if (!response.status.isSuccess()) { error("Cui cloud response: ${response.status}") } }