-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08f5d0d
commit 7502628
Showing
7 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package vdx.fetchfile | ||
package examples | ||
|
||
import cats.effect._ | ||
import cats.syntax.functor._ | ||
import java.net.URL | ||
import java.io.ByteArrayOutputStream | ||
|
||
object Main extends IOApp { | ||
def run(args: List[String]): IO[ExitCode] = { | ||
|
||
implicit val backend: Backend[IO] = HttpURLConnectionBackend[IO] | ||
|
||
Blocker[IO].use { blocker => | ||
val out = new ByteArrayOutputStream() | ||
Downloader[IO].fetch( | ||
new URL("http://localhost:8088/100MB.bin"), | ||
Resource.fromAutoCloseable(IO.delay(out)), | ||
blocker, | ||
1024, | ||
Progress.consoleProgress | ||
).as(ExitCode.Success) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package vdx.fetchfile | ||
|
||
import cats.effect.Sync | ||
import fs2._ | ||
|
||
import scala.concurrent.duration.{Duration, NANOSECONDS} | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
import java.util.Locale | ||
|
||
object Progress { | ||
def noop[F[_]]: Int => Pipe[F, Byte, Unit] = _ => _.map(_ => ()) | ||
|
||
def consoleProgress[F[_]: Sync]: Int => Pipe[F, Byte, Unit] = | ||
custom[F] { (downloadedBytes, contentLength, _, downloadSpeed) => | ||
println( | ||
s"\u001b[1A\u001b[100D\u001b[0KDownloaded ${bytesToString(downloadedBytes)} of" + | ||
s" ${bytesToString(contentLength.toLong)} | ${bytesToString(downloadSpeed)}/s" | ||
) | ||
} | ||
|
||
def custom[F[_]: Sync](f: (Long, Int, Duration, Long) => Unit): Int => Pipe[F, Byte, Unit] = | ||
contentLength => { s => | ||
Stream.eval(Sync[F].delay(System.nanoTime())) | ||
.flatMap { startTime => | ||
val downloadedBytes = new AtomicLong(0) | ||
|
||
s.chunks.map { chunk => | ||
val down = downloadedBytes.addAndGet(chunk.size.toLong) | ||
val elapsedTime = Duration(System.nanoTime() - startTime, NANOSECONDS) | ||
val speed = (down * 1000) / Math.max(elapsedTime.toMillis, 1) | ||
|
||
f(down, contentLength, elapsedTime, speed) | ||
|
||
|
||
} | ||
} | ||
} | ||
|
||
// https://stackoverflow.com/questions/45885151/bytes-in-human-readable-format-with-idiomatic-scala | ||
def bytesToString(size: Long): String = { | ||
val TB = 1L << 40 | ||
val GB = 1L << 30 | ||
val MB = 1L << 20 | ||
val KB = 1L << 10 | ||
|
||
val (value, unit) = { | ||
if (size >= 2 * TB) { | ||
(size.asInstanceOf[Double] / TB, "TB") | ||
} else if (size >= 2 * GB) { | ||
(size.asInstanceOf[Double] / GB, "GB") | ||
} else if (size >= 2 * MB) { | ||
(size.asInstanceOf[Double] / MB, "MB") | ||
} else if (size >= 2 * KB) { | ||
(size.asInstanceOf[Double] / KB, "KB") | ||
} else { | ||
(size.asInstanceOf[Double], "B") | ||
} | ||
} | ||
"%.1f %s".formatLocal(Locale.US, value, unit) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters