mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-17 09:09:23 +08:00
Gradle tasks for uploading artifacts
This commit is contained in:
parent
b70627f35e
commit
588d2d16ca
@ -81,11 +81,11 @@ subprojects {
|
||||
acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0)
|
||||
}
|
||||
}?.let { (_, file) ->
|
||||
val filename = file.nameWithoutExtension.substringAfterLast('-')
|
||||
val filename = file.name
|
||||
println("filename=$filename")
|
||||
upload.GitToken.upload(
|
||||
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 {
|
||||
|
||||
repositories {
|
||||
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"))
|
||||
}
|
@ -1,8 +1,14 @@
|
||||
@file:Suppress("EXPERIMENTAL_API_USAGE")
|
||||
|
||||
package upload
|
||||
|
||||
import java.io.*
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
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.*
|
||||
|
||||
object GitToken {
|
||||
@ -14,56 +20,22 @@ object GitToken {
|
||||
}
|
||||
}
|
||||
|
||||
fun upload(file: File, url: String) {
|
||||
val begin = System.currentTimeMillis()
|
||||
println("上传开始...")
|
||||
//StringBuffer result = new StringBuffer();
|
||||
//StringBuffer result = new StringBuffer();
|
||||
var `in`: BufferedReader? = null
|
||||
var conn: HttpURLConnection? = null
|
||||
conn = URL(url).openConnection() as HttpURLConnection
|
||||
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))
|
||||
fun upload(file: File, url: String) = runBlocking {
|
||||
HttpClient(CIO) {
|
||||
install(HttpTimeout)
|
||||
}.put<String>("""$url?access_token=${getGitToken()}""") {
|
||||
timeout {
|
||||
connectTimeoutMillis = 600_000
|
||||
requestTimeoutMillis = 600_000
|
||||
socketTimeoutMillis = 600_000
|
||||
}
|
||||
size = raf.read(buffer).toLong()
|
||||
val content = String(Base64.getEncoder().encode(file.readBytes()))
|
||||
body = """
|
||||
{
|
||||
"message": "automatic upload",
|
||||
"content": "$content"
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
raf.close()
|
||||
// 传输json尾部
|
||||
dos.writeBytes("\"}")
|
||||
dos.flush()
|
||||
dos.close()
|
||||
`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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user