refactor sinkResponseFile and add downloadC
Remote.S3 and Remote.Helper.Http both had similar code to sink a http-conduit Response to a file; refactor out sinkResponseFile. downloadC downloads an url to a file using http-conduit, and supports resuming. Falls back to curl to handle urls that http-conduit does not support. This is not used yet, but the goal is to replace download with it. git-annex.cabal: conduit-extra was not actually used for a long time, remove the dep. conduit moves into the main dependency list, but since http-conduit was already in there, and it depends on conduit, that's not really adding a new build dep. This commit was supported by the NSF-funded DataLad project.
This commit is contained in:
parent
0e8564201e
commit
0f6775f1ff
6 changed files with 138 additions and 35 deletions
|
@ -15,6 +15,7 @@ import Utility.Metered
|
|||
import Remote.Helper.Special
|
||||
import Network.HTTP.Client (RequestBody(..), Response, responseStatus, responseBody, BodyReader, NeedsPopper)
|
||||
import Network.HTTP.Types
|
||||
import Network.HTTP.Conduit
|
||||
|
||||
import qualified Data.ByteString.Lazy as L
|
||||
import qualified Data.ByteString as S
|
||||
|
@ -71,15 +72,5 @@ handlePopper numchunks chunksize meterupdate h sink = do
|
|||
httpBodyRetriever :: FilePath -> MeterUpdate -> Response BodyReader -> IO ()
|
||||
httpBodyRetriever dest meterupdate resp
|
||||
| responseStatus resp /= ok200 = giveup $ show $ responseStatus resp
|
||||
| otherwise = bracket (openBinaryFile dest WriteMode) hClose (go zeroBytesProcessed)
|
||||
where
|
||||
reader = responseBody resp
|
||||
go sofar h = do
|
||||
b <- reader
|
||||
if S.null b
|
||||
then return ()
|
||||
else do
|
||||
let sofar' = addBytesProcessed sofar $ S.length b
|
||||
S.hPut h b
|
||||
meterupdate sofar'
|
||||
go sofar' h
|
||||
| otherwise = runResourceT $
|
||||
sinkResponseFile meterupdate zeroBytesProcessed dest WriteMode resp
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue