Add md5 in shadow

This commit is contained in:
Karlatemp 2020-09-21 22:59:00 +08:00
parent c9200852de
commit 34c325e014
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 48 additions and 2 deletions

View File

@ -132,6 +132,18 @@ subprojects {
it.printStackTrace() // force show stacktrace
throw it
}
runCatching {
upload.GitHub.upload(
file.inputStream().use { upload.GitHub.run { it.md5().hex().toByteArray(Charsets.UTF_8) } },
project,
"mirai-repo",
"shadow/${project.name}/$filename.md5"
)
}.exceptionOrNull()?.let {
System.err.println("GitHub Upload failed")
it.printStackTrace() // force show stacktrace
throw it
}
}
}
}

View File

@ -26,6 +26,9 @@ import org.gradle.kotlin.dsl.provideDelegate
import org.jsoup.Connection
import org.jsoup.Jsoup
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.security.MessageDigest
import java.util.*
internal val Http = HttpClient(CIO) {
@ -71,7 +74,38 @@ object GitHub {
)
}
fun upload(file: File, project: Project, repo: String, targetFilePath: String) = runBlocking {
fun InputStream.md5(): ByteArray {
val digest = MessageDigest.getInstance("md5")
digest.reset()
use { input ->
object : OutputStream() {
override fun write(b: Int) {
digest.update(b.toByte())
}
override fun write(b: ByteArray, off: Int, len: Int) {
digest.update(b, off, len)
}
}.use { output ->
input.copyTo(output)
}
}
return digest.digest()
}
fun ByteArray.hex(): String = buildString(size * 2) {
this@hex.forEach { byte ->
val uint = Integer.toHexString(byte.toInt() and 0xFF)
if (uint.length == 1) append('0')
append(uint)
}
}
fun upload(file: File, project: Project, repo: String, targetFilePath: String) = upload(
file.readBytes(), project, repo, targetFilePath
)
fun upload(source: ByteArray, project: Project, repo: String, targetFilePath: String) = runBlocking {
val token = getGithubToken(project)
println("token.length=${token.length}")
val url = "https://api.github.com/repos/project-mirai/$repo/contents/$targetFilePath"
@ -86,7 +120,7 @@ object GitHub {
)
}.getOrNull()
println("sha=$sha")
val content = String(Base64.getEncoder().encode(file.readBytes()))
val content = String(Base64.getEncoder().encode(source))
body = """
{
"message": "Automatically upload on release ${project.name}:${project.version}",