mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
087acd9180
* Revert "raise lower bound on mtl to a real version (#3499)" This reverts commitf94c0311c1. * Revert "core: expand ranges to fit ghc 8.10 & 9.6 (#3496)" This reverts commit9a1c7f41f7. * update simplexmq * remove netword-transport fork * compatibility with GHC 8.10.7 * simplexmq * fix test * simplexmq, deps * update sqlcipher deps in sha256nix * fix index-state in cabal.project * index-state * remove import * add cabal.project.freeze * simplexmq * remove freeze * simplexmq * bytestring,simplexmq * template-haskell, simplexmq * simplexmq * simplexmq * simplexmq * mtl * simplexmq * remove duplicate index-state
33 lines
1.1 KiB
Haskell
33 lines
1.1 KiB
Haskell
module Simplex.Chat.Util (week, encryptFile, chunkSize) where
|
|
|
|
import Control.Monad
|
|
import Control.Monad.Except
|
|
import Control.Monad.IO.Class
|
|
import qualified Data.ByteString.Lazy as LB
|
|
import Data.Time (NominalDiffTime)
|
|
import Simplex.Messaging.Crypto.File (CryptoFile (..), CryptoFileArgs (..))
|
|
import qualified Simplex.Messaging.Crypto.File as CF
|
|
import UnliftIO.IO (IOMode (..), withFile)
|
|
|
|
week :: NominalDiffTime
|
|
week = 7 * 86400
|
|
|
|
encryptFile :: FilePath -> FilePath -> CryptoFileArgs -> ExceptT String IO ()
|
|
encryptFile fromPath toPath cfArgs = do
|
|
let toFile = CryptoFile toPath $ Just cfArgs
|
|
-- uncomment to test encryption error in runTestFileTransferEncrypted
|
|
-- throwError "test error"
|
|
withExceptT show $
|
|
withFile fromPath ReadMode $ \r -> CF.withFile toFile WriteMode $ \w -> do
|
|
encryptChunks r w
|
|
liftIO $ CF.hPutTag w
|
|
where
|
|
encryptChunks r w = do
|
|
ch <- liftIO $ LB.hGet r chunkSize
|
|
unless (LB.null ch) $ liftIO $ CF.hPut w ch
|
|
unless (LB.length ch < chunkSize) $ encryptChunks r w
|
|
|
|
chunkSize :: Num a => a
|
|
chunkSize = 65536
|
|
{-# INLINE chunkSize #-}
|