in src/dagger/src.main.kts [106:133]
fun downloadAsTempFile(
url: String,
maxRetries: Int = 3,
backoffMillis: Long = 1000L,
): Path {
val tempFile = Files
.createTempFile("", url.substringAfterLast("/"))
.toAbsolutePath()
.also { it.toFile().deleteOnExit() }
for (attempt in 1..maxRetries) {
println("Downloading $url to $tempFile")
try {
URI(url).toURL().openStream().use { input ->
Files.newOutputStream(tempFile).use { output ->
input.copyTo(output)
}
}
return tempFile
} catch (e: Throwable) {
if (attempt == maxRetries) throw e
val time = backoffMillis * attempt
System.err.println("Download error: ${e.message}")
System.err.println("Failed to download $url on $attempt attempt, will retry in ${time / 1000}s")
Thread.sleep(time)
}
}
return tempFile
}