Skip to content

Commit

Permalink
Make getbinary return ByteString
Browse files Browse the repository at this point in the history
  • Loading branch information
Xandaros committed Jul 27, 2017
1 parent 7b89d2d commit fb563ba
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/Network/FTP/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ module Network.FTP.Client(-- * Establishing\/Removing connections
)
where
import Control.Exception
import Data.ByteString (hGet, hPut)
import Data.ByteString (hGet, hPut, ByteString)
import qualified Data.ByteString as BS
import Data.String.Utils
import qualified Network
import Network.BSD
Expand Down Expand Up @@ -416,25 +417,20 @@ retrlines h cmd =

{- | Retrieves binary data from the remote. The string gives the command
to issue. -}
retrbinary :: FTPConnection -> String -> IO (String, FTPResult)
retrbinary h cmd =
let foo h2 [] = do hClose h2
r <- getresp h
return ([], r)
foo h2 (x:xs) = do next <- unsafeInterleaveIO $ foo h2 xs
return $ (x : fst next, snd next)
in do
sendcmd h "TYPE I"
newh <- transfercmd h cmd
c <- hGetContents newh
foo newh c
retrbinary :: FTPConnection -> String -> IO (ByteString, FTPResult)
retrbinary h cmd = do sendcmd h "TYPE I"
newh <- transfercmd h cmd
c <- BS.hGetContents newh
hClose newh
r <- getresp h
pure (c, r)

{- | Retrieves the specified file in text mode. -}
getlines :: FTPConnection -> String -> IO ([String], FTPResult)
getlines h fn = retrlines h ("RETR " ++ fn)

{- | Retrieves the specified file in binary mode. -}
getbinary :: FTPConnection -> String -> IO (String, FTPResult)
getbinary :: FTPConnection -> String -> IO (ByteString, FTPResult)
getbinary h fn = retrbinary h ("RETR " ++ fn)

{- | Puts data in the specified file in text mode. The first string
Expand All @@ -454,7 +450,7 @@ uploadbinary h fn = do input <- readBinaryFile fn
{- | Downloads a file from remote and saves to disk in binary mode. Note: filename is used for both local and remote. -}
downloadbinary :: FTPConnection -> String -> IO FTPResult
downloadbinary h fn = do (r0, r1) <- getbinary h fn
writeBinaryFile fn r0
BS.writeFile fn r0
return r1

{- | Similar to downloadbinary, but downloads the file in blocks of 4096 bytes
Expand Down

0 comments on commit fb563ba

Please sign in to comment.