Commit 588d2d16 authored by Him188's avatar Him188

Gradle tasks for uploading artifacts

parent b70627f3
...@@ -81,11 +81,11 @@ subprojects { ...@@ -81,11 +81,11 @@ subprojects {
acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0) acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0)
} }
}?.let { (_, file) -> }?.let { (_, file) ->
val filename = file.nameWithoutExtension.substringAfterLast('-') val filename = file.name
println("filename=$filename") println("filename=$filename")
upload.GitToken.upload( upload.GitToken.upload(
file, file,
"https://api.github.com/repos/mamoe/mirai/contents/shdaow/${project.name}/$filename" "https://api.github.com/repos/mamoe/mirai-repo/contents/shdaow/${project.name}/$filename"
) )
} }
} }
......
...@@ -4,4 +4,23 @@ plugins { ...@@ -4,4 +4,23 @@ plugins {
repositories { repositories {
jcenter() jcenter()
}
kotlin {
sourceSets {
all {
languageSettings.useExperimentalAnnotation("kotlin.Experimental")
}
}
}
dependencies {
fun kotlinx(id: String, version: String) = "org.jetbrains.kotlinx:kotlinx-$id:$version"
fun ktor(id: String, version: String) = "io.ktor:ktor-$id:$version"
api(kotlinx("coroutines-core", "1.3.3"))
api(ktor("client-core", "1.3.2"))
api(ktor("client-cio", "1.3.2"))
api(ktor("client-json", "1.3.2"))
} }
\ No newline at end of file
@file:Suppress("EXPERIMENTAL_API_USAGE")
package upload package upload
import java.io.* import io.ktor.client.HttpClient
import java.net.HttpURLConnection import io.ktor.client.engine.cio.CIO
import java.net.URL import io.ktor.client.features.HttpTimeout
import io.ktor.client.features.timeout
import io.ktor.client.request.put
import kotlinx.coroutines.runBlocking
import java.io.File
import java.util.* import java.util.*
object GitToken { object GitToken {
...@@ -14,56 +20,22 @@ object GitToken { ...@@ -14,56 +20,22 @@ object GitToken {
} }
} }
fun upload(file: File, url: String) { fun upload(file: File, url: String) = runBlocking {
val begin = System.currentTimeMillis() HttpClient(CIO) {
println("上传开始...") install(HttpTimeout)
//StringBuffer result = new StringBuffer(); }.put<String>("""$url?access_token=${getGitToken()}""") {
//StringBuffer result = new StringBuffer(); timeout {
var `in`: BufferedReader? = null connectTimeoutMillis = 600_000
var conn: HttpURLConnection? = null requestTimeoutMillis = 600_000
conn = URL(url).openConnection() as HttpURLConnection socketTimeoutMillis = 600_000
conn.connectTimeout = 120000
conn.readTimeout = 120000
// 设置
conn.doOutput = true // 需要输出
conn.doInput = true // 需要输入
conn.useCaches = false // 不允许缓存
conn.requestMethod = "PUT" // 设置PUT方式连接
conn.setRequestProperty("Content-Type", "application/json")
conn.setRequestProperty("Authorization", "token " + getGitToken())
conn.setRequestProperty("User-Agent", "Github File Uploader App")
conn.connect()
// 传输数据
val dos = DataOutputStream(conn.outputStream)
// 传输json头部
dos.writeBytes("{\"message\":\".\",\"content\":\"")
// 传输文件内容
val buffer = ByteArray(1024 * 1002) // 3的倍数
val raf = RandomAccessFile(file, "r")
var size: Long = raf.read(buffer).toLong()
while (size > -1) {
if (size == buffer.size.toLong()) {
dos.write(Base64.getEncoder().encode(buffer))
} else {
val tmp = ByteArray(size.toInt())
System.arraycopy(buffer, 0, tmp, 0, size.toInt())
dos.write(Base64.getEncoder().encode(tmp))
} }
size = raf.read(buffer).toLong() val content = String(Base64.getEncoder().encode(file.readBytes()))
} body = """
raf.close() {
// 传输json尾部 "message": "automatic upload",
dos.writeBytes("\"}") "content": "$content"
dos.flush() }
dos.close() """.trimIndent()
`in` = BufferedReader(InputStreamReader(conn.inputStream))
var line: String?
while (`in`.readLine().also { line = it } != null) {
//result.append(line).append("\n");
} }
val end = System.currentTimeMillis()
System.out.printf("Upload finished within %d seconds\n", (end - begin) / 1000)
//result.toString()
//result.toString()
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment