Last active
August 2, 2021 10:03
-
-
Save andrevdm/b44473470e124ffa065ccc7ef586c71d to your computer and use it in GitHub Desktop.
Haskell chunked/streamed file downloads with progress notifications
This file contains hidden or 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
import Data.Conduit ((.|)) | |
import qualified Data.Conduit as C | |
import qualified Data.Conduit.Combinators as C | |
import Network.HTTP.Req ((/:)) | |
import qualified Network.HTTP.Req as R | |
import qualified Network.HTTP.Req.Conduit as R | |
downloadChunkedUrl | |
:: FilePath | |
-> (ByteString -> IO ()) | |
-> IO () | |
downloadChunkedUrl dest onProgress = do | |
let httpsUrl = undefined -- Set however you usually do with req | |
let httpsOptions = mempty | |
R.runReq R.defaultHttpConfig $ do | |
R.reqBr R.GET httpsUrl R.NoReqBody httpsOptions $ \r -> | |
C.runConduitRes $ | |
R.responseBodySource r | |
.| C.iterM (liftIO . onProgress) | |
.| C.sinkFile dest | |
pure () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment