Tuesday, July 24, 2018

Ways to stream yesod data to the client


model:

Rawdata
  bytes ByteString


way 1:

  addHeader "Content-Disposition" $
    T.concat ["attachment; filename=\"", filename, "\""]
  rawdata <- runDB $ get404 rawdataId                                                                                                                                         
  let bytes = rawdataBytes rawdata                                                                                                                                            
  sendResponse (TE.encodeUtf8 mimetype, toContent bytes)                                                                                                                      


way 2:

  let bytesSource = selectSource [RawdataId ==. rawdataId] []                                                                                                                 
  respondSourceDB (TE.encodeUtf8 mimetype) $ bytesSource $= awaitForever toBuilder'                                                                                           
  where                                                                                                                                                                       
    toBuilder' (Entity _ rawdata) = do                                                                                                                                        
      sendChunkBS $ rawdataBytes rawdata                                                                                                                                      
      sendFlush                                                                                                                                                               


way 3:

  let bytesSource = E.selectSource $ E.from $ \rd -> do
        E.where_ (rd E.^. RawdataId E.==. E.val rawdataId)
        return $ rd E.^. RawdataBytes
  respondSourceDB (TE.encodeUtf8 mimetype) $ bytesSource $= awaitForever toBuilder'
  where
    toBuilder' (E.Value bytes) = do
      sendChunkBS bytes
      sendFlush

No comments: