Skip to content

Instantly share code, notes, and snippets.

@voidlizard
Last active July 14, 2023 17:11
Show Gist options
  • Save voidlizard/609b77b388f4b003771ff98e15b46034 to your computer and use it in GitHub Desktop.
Save voidlizard/609b77b388f4b003771ff98e15b46034 to your computer and use it in GitHub Desktop.
import Streaming.Prelude qualified as S
import Streaming qualified as S
-- FIXME: move-to-library
readBlob :: forall m . ( MonadIO m
, HasStorage m
, Block ByteString ~ ByteString
)
=> HashRef
-> m (Maybe ByteString)
readBlob hr = do
sto <- getStorage
let readBlock h = liftIO $ getBlock sto h
chunks <- S.toList_ $
deepScan ScanDeep (const $ S.yield Nothing) (fromHashRef hr) readBlock $ \ha -> do
unless (fromHashRef hr == ha) do
readBlock ha >>= S.yield
let mfo acc el = case (acc, el) of
(Nothing, Just s) -> Just [s]
(_, Nothing) -> Nothing
(Just ss, Just s) -> Just (s:ss)
pure $ LBS.concat . reverse <$> foldl mfo Nothing chunks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment