Skip to content

Commit

Permalink
Merge pull request brendanhay#359 from tolysz/feature/chunked-offset-…
Browse files Browse the repository at this point in the history
…length

Add `chunkedFileOffsetLength` sub parts versions
  • Loading branch information
brendanhay authored Feb 12, 2017
2 parents e69ebd3 + 60b84a5 commit b1a35d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions amazonka/src/Network/AWS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ module Network.AWS
-- *** Request Bodies
, ToHashedBody (..)
, hashedFile
, hashedFileOffsetLength
, hashedBody

-- *** Chunked Request Bodies
, ToBody (..)
, ChunkSize (..)
, defaultChunkSize
, chunkedFile
, chunkedFileOffsetLength
, unsafeChunkedBody

-- *** Response Bodies
Expand Down
35 changes: 35 additions & 0 deletions amazonka/src/Network/AWS/Internal/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ hashedFile f = liftIO $ HashedStream
<*> getFileSize f
<*> pure (Conduit.sourceFile f)

-- | Same as `hashedFile` but from a part of a file.
hashedFileOffsetLength :: MonadIO m => FilePath -> Integer -> Integer -> m HashedBody
hashedFileOffsetLength f o l = liftIO $ HashedStream
<$> runResourceT (Conduit.sourceFileRange f (Just o) (Just l) $$ sinkSHA256)
<*> getFileSize f
<*> pure (Conduit.sourceFileRange f (Just o) (Just l) )

-- | Construct a 'HashedBody' from a source, manually specifying the
-- 'SHA256' hash and file size.
--
Expand All @@ -68,6 +75,15 @@ chunkedFile c f = do
then return $ unsafeChunkedBody c n (sourceFileChunks c f)
else Hashed `liftM` hashedFile f

-- | Same as `chunkedFile` but for a apart of a file
chunkedFileOffsetLength :: MonadIO m => ChunkSize -> FilePath -> Integer -> Integer -> m RqBody
chunkedFileOffsetLength c f o l = do
n <- getFileSize f
let n1 = min (n - o) l
if n1 > toInteger c
then return $ unsafeChunkedBody c n1 (sourceFileOffsetLengthChunks c f o l)
else Hashed `liftM` hashedFileOffsetLength f o l

-- | Something something.
--
-- Marked as unsafe because it does nothing to enforce the chunk size.
Expand Down Expand Up @@ -100,6 +116,25 @@ sourceFileChunks (ChunkSize sz) f =
yield bs
go h

sourceFileOffsetLengthChunks :: MonadResource m
=> ChunkSize
-> FilePath
-> Integer -- ^ offset
-> Integer -- ^ length
-> Source m ByteString
sourceFileOffsetLengthChunks (ChunkSize sz) f offset len =
bracketP (openBinaryFile f ReadMode) hClose (\h -> liftIO (hSeek h AbsoluteSeek offset) >> go len h)
where
go r h
| r <= fromIntegral sz = do
bs <- liftIO (BS.hGet h (fromIntegral r))
unless (BS.null bs) $ yield bs
| otherwise = do
bs <- liftIO (BS.hGet h sz)
unless (BS.null bs) $ do
yield bs
go (r - fromIntegral sz) h

-- | Incrementally calculate a 'MD5' 'Digest'.
sinkMD5 :: Monad m => Consumer ByteString m (Digest MD5)
sinkMD5 = sinkHash
Expand Down

0 comments on commit b1a35d3

Please sign in to comment.