parallel-chunked-download
Installation
SKILL.md
N ranges, one file
Three decisions in order: may I range this, should I, and then the ranges themselves.
// adapted — the flow's value type is spelled out as a named `Progress` class in place of the
// source's positional triple; the two callback arguments and the single-threaded fallback are
// elided to one line each. The probe test, the threshold and the branch are the source's own.
fun download(url: String, target: Path, maxRetries: Int = 3, parallel: Int = 4): Flow<Progress> =
channelFlow {
with(bareDownloadClient()) { // no logging, no negotiation
val supportsRange = try {
val probe = head(url) { header("Range", "bytes=0-0") }
probe.headers["Content-Range"] != null || probe.status.value in 200..299
} catch (e: Exception) { false }
val fileSize = try {
head(url).headers[HttpHeaders.ContentLength]?.toLong() ?: 0L
} catch (e: Exception) { 0L }