mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
MD5 uploads.
This commit is contained in:
parent
3f0f062f2b
commit
b7ad814ff0
4 changed files with 64 additions and 28 deletions
|
@ -52,17 +52,6 @@ class ArtifactFetcher @Inject constructor(@Assisted("url") val url: String,
|
||||||
private val estimatedSize: Int
|
private val estimatedSize: Int
|
||||||
get() = if (url.contains("kotlin-compiler")) 18000000 else 1000000
|
get() = if (url.contains("kotlin-compiler")) 18000000 else 1000000
|
||||||
|
|
||||||
private fun toMd5(bytes: ByteArray) : String {
|
|
||||||
val result = StringBuilder()
|
|
||||||
val md5 = MessageDigest.getInstance("MD5").digest(bytes)
|
|
||||||
md5.forEach {
|
|
||||||
val byte = it.toInt() and 0xff
|
|
||||||
if (byte < 16) result.append("0")
|
|
||||||
result.append(Integer.toHexString(byte))
|
|
||||||
}
|
|
||||||
return result.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getBytes(url: String) : ByteArray {
|
private fun getBytes(url: String) : ByteArray {
|
||||||
log(2, "$url: downloading to $fileName")
|
log(2, "$url: downloading to $fileName")
|
||||||
val body = http.get(url)
|
val body = http.get(url)
|
||||||
|
@ -86,7 +75,7 @@ class ArtifactFetcher @Inject constructor(@Assisted("url") val url: String,
|
||||||
val file = File(fileName)
|
val file = File(fileName)
|
||||||
file.parentFile.mkdirs()
|
file.parentFile.mkdirs()
|
||||||
val bytes = getBytes(url)
|
val bytes = getBytes(url)
|
||||||
if (remoteMd5 != null && remoteMd5 != toMd5(bytes)) {
|
if (remoteMd5 != null && remoteMd5 != Md5.toMd5(bytes)) {
|
||||||
throw KobaltException("MD5 not matching for $url")
|
throw KobaltException("MD5 not matching for $url")
|
||||||
} else {
|
} else {
|
||||||
log(2, "No md5 found for $url, skipping md5 check")
|
log(2, "No md5 found for $url, skipping md5 check")
|
||||||
|
|
25
src/main/kotlin/com/beust/kobalt/maven/Md5.kt
Normal file
25
src/main/kotlin/com/beust/kobalt/maven/Md5.kt
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package com.beust.kobalt.maven
|
||||||
|
|
||||||
|
import com.beust.kobalt.file
|
||||||
|
import java.io.File
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.Paths
|
||||||
|
import java.security.MessageDigest
|
||||||
|
|
||||||
|
public class Md5 {
|
||||||
|
companion object {
|
||||||
|
fun toMd5(file: File) = toMd5(Files.readAllBytes(Paths.get(file.toURI())))
|
||||||
|
|
||||||
|
fun toMd5(bytes: ByteArray): String {
|
||||||
|
val result = StringBuilder()
|
||||||
|
val md5 = MessageDigest.getInstance("MD5").digest(bytes)
|
||||||
|
md5.forEach {
|
||||||
|
val byte = it.toInt() and 0xff
|
||||||
|
if (byte < 16) result.append("0")
|
||||||
|
result.append(Integer.toHexString(byte))
|
||||||
|
}
|
||||||
|
return result.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.internal.TaskResult
|
import com.beust.kobalt.internal.TaskResult
|
||||||
import com.beust.kobalt.maven.Http
|
import com.beust.kobalt.maven.Http
|
||||||
import com.beust.kobalt.maven.KobaltException
|
import com.beust.kobalt.maven.KobaltException
|
||||||
|
import com.beust.kobalt.maven.Md5
|
||||||
import com.beust.kobalt.misc.KobaltLogger
|
import com.beust.kobalt.misc.KobaltLogger
|
||||||
import com.google.inject.assistedinject.Assisted
|
import com.google.inject.assistedinject.Assisted
|
||||||
import com.squareup.okhttp.Response
|
import com.squareup.okhttp.Response
|
||||||
|
@ -12,9 +13,12 @@ import org.jetbrains.annotations.Nullable
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Paths
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
data class JCenterPackage(val jo: JsonObject) {
|
data class JCenterPackage(val jo: JsonObject) {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
val latestPublishedVersion = (jo.get("versions") as JsonArray<String>).get(0)
|
val latestPublishedVersion = (jo.get("versions") as JsonArray<String>).get(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,35 +86,54 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
||||||
project.group!!.replace(".", "/"),
|
project.group!!.replace(".", "/"),
|
||||||
project.artifactId!!,
|
project.artifactId!!,
|
||||||
project.version!!,
|
project.version!!,
|
||||||
f.getName())
|
f.name)
|
||||||
.join("/")
|
.join("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
return upload(files, configuration, fileToPath)
|
return upload(files, configuration, fileToPath, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun uploadFile(file: File, url: String, configuration: JCenterConfiguration) =
|
fun uploadFile(file: File, url: String, configuration: JCenterConfiguration, generateMd5: Boolean = false) =
|
||||||
upload(arrayListOf(file), configuration, {
|
upload(arrayListOf(file), configuration, {
|
||||||
f: File -> "${UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT}/${username}/generic/${url}"
|
f: File -> "${UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT}/$username/generic/$url"
|
||||||
})
|
}, generateMd5)
|
||||||
|
|
||||||
private fun upload(files: List<File>, configuration : JCenterConfiguration?, fileToPath: (File) -> String)
|
private fun upload(files: List<File>, configuration : JCenterConfiguration?, fileToPath: (File) -> String,
|
||||||
: TaskResult {
|
generateMd5: Boolean = false) : TaskResult {
|
||||||
val successes = arrayListOf<File>()
|
val successes = arrayListOf<File>()
|
||||||
val failures = hashMapOf<File, String>()
|
val failures = hashMapOf<File, String>()
|
||||||
|
val filesToUpload = arrayListOf<File>()
|
||||||
files.forEach {
|
files.forEach {
|
||||||
var path = fileToPath(it)
|
|
||||||
|
|
||||||
// Apply the configurations for this project, if any
|
// Create the md5 for this file
|
||||||
|
filesToUpload.add(it)
|
||||||
|
if (generateMd5) {
|
||||||
|
with(File(it.absolutePath)) {
|
||||||
|
val md5: String = Md5.toMd5(this)
|
||||||
|
val md5File = File(absolutePath + ".md5")
|
||||||
|
md5File.writeText(md5)
|
||||||
|
filesToUpload.add(md5File)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// If any configuration was given, apply them so the URL reflects them, e.g. ?publish=1
|
||||||
|
//
|
||||||
val options = arrayListOf<String>()
|
val options = arrayListOf<String>()
|
||||||
if (configuration?.publish == true) options.add("publish=1")
|
if (configuration?.publish == true) options.add("publish=1")
|
||||||
// This actually needs to be done
|
|
||||||
// options.add("list_in_downloads=1")
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: These files should be uploaded from a thread pool instead of serially
|
||||||
|
//
|
||||||
|
log(1, "Found ${filesToUpload.size()} artifacts to upload")
|
||||||
|
filesToUpload.forEach {
|
||||||
|
var path = fileToPath(it)
|
||||||
if (options.size() > 0) {
|
if (options.size() > 0) {
|
||||||
path += "?" + options.join("&")
|
path += "?" + options.join("&")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(1, " Uploading $it to $path")
|
||||||
http.uploadFile(username, password, path, it,
|
http.uploadFile(username, password, path, it,
|
||||||
{ r: Response -> successes.add(it) },
|
{ r: Response -> successes.add(it) },
|
||||||
{ r: Response ->
|
{ r: Response ->
|
||||||
|
@ -120,7 +143,7 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
||||||
}
|
}
|
||||||
|
|
||||||
val result: TaskResult
|
val result: TaskResult
|
||||||
if (successes.size() == files.size()) {
|
if (successes.size() == filesToUpload.size()) {
|
||||||
log(1, "All artifacts successfully uploaded")
|
log(1, "All artifacts successfully uploaded")
|
||||||
result = TaskResult(true)
|
result = TaskResult(true)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -52,7 +52,6 @@ public class PublishPlugin @Inject constructor(val http: Http, val files: com.be
|
||||||
val result = files.findRecursively(File(project.directory, project.buildDirectory)) { file ->
|
val result = files.findRecursively(File(project.directory, project.buildDirectory)) { file ->
|
||||||
VALID.any { file.endsWith(it)} and file.contains(project.version!!)
|
VALID.any { file.endsWith(it)} and file.contains(project.version!!)
|
||||||
}.map { it -> File(it) }
|
}.map { it -> File(it) }
|
||||||
log(1, "${project.name}: Found ${result.size()} artifacts to upload")
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue