Cleanup smart downloader utility.

This commit is contained in:
2023-03-18 14:55:19 -07:00
parent aa2750bdc0
commit 1a968a6489
3 changed files with 15 additions and 10 deletions

View File

@ -128,7 +128,7 @@ publishing {
}
tasks.withType<Wrapper> {
gradleVersion = "8.0"
gradleVersion = "8.0.2"
}
java {

View File

@ -1,7 +1,6 @@
package gay.pizza.foundation.concrete
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.get
import org.gradle.language.jvm.tasks.ProcessResources

View File

@ -1,9 +1,13 @@
package gay.pizza.foundation.concrete
import java.net.URI
import java.nio.file.Files
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse.BodyHandlers
import java.nio.file.Path
import java.security.MessageDigest
import kotlin.io.path.exists
import kotlin.io.path.inputStream
class SmartDownloader(
private val localFilePath: Path,
@ -20,23 +24,25 @@ class SmartDownloader(
}
private fun downloadRemoteFile() {
val url = remoteDownloadUrl.toURL()
val remoteFileStream = url.openStream()
val localFileStream = Files.newOutputStream(localFilePath)
remoteFileStream.transferTo(localFileStream)
val httpClient = HttpClient.newHttpClient()
val request = HttpRequest.newBuilder()
.GET()
.uri(remoteDownloadUrl)
.build()
httpClient.send(request, BodyHandlers.ofFile(localFilePath))
val hashResult = checkLocalFileHash()
if (hashResult != HashResult.ValidHash) {
throw RuntimeException("Download of $remoteDownloadUrl did not result in valid hash.")
throw RuntimeException("Download of $remoteDownloadUrl did not result in a valid hash.")
}
}
private fun checkLocalFileHash(): HashResult {
if (!Files.exists(localFilePath)) {
if (!localFilePath.exists()) {
return HashResult.DoesNotExist
}
val digest = MessageDigest.getInstance("SHA-256")
val localFileStream = Files.newInputStream(localFilePath)
val localFileStream = localFilePath.inputStream()
val buffer = ByteArray(16 * 1024)
while (true) {