mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
d90da57f12
* core: store remote files (wip) * fix/test store remote file * get remote file * get file * validate remote file metadata before sending to controller * CLI commands, test * update store method
28 lines
1.0 KiB
Haskell
28 lines
1.0 KiB
Haskell
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
module Simplex.Chat.Files where
|
|
|
|
import Control.Monad.IO.Class
|
|
import Simplex.Chat.Controller
|
|
import Simplex.Messaging.Util (ifM)
|
|
import System.FilePath (splitExtensions, combine)
|
|
import UnliftIO.Directory (doesFileExist, getTemporaryDirectory, getHomeDirectory, doesDirectoryExist)
|
|
|
|
uniqueCombine :: MonadIO m => FilePath -> String -> m FilePath
|
|
uniqueCombine fPath fName = tryCombine (0 :: Int)
|
|
where
|
|
tryCombine n =
|
|
let (name, ext) = splitExtensions fName
|
|
suffix = if n == 0 then "" else "_" <> show n
|
|
f = fPath `combine` (name <> suffix <> ext)
|
|
in ifM (doesFileExist f) (tryCombine $ n + 1) (pure f)
|
|
|
|
getChatTempDirectory :: ChatMonad m => m FilePath
|
|
getChatTempDirectory = chatReadVar tempDirectory >>= maybe getTemporaryDirectory pure
|
|
|
|
getDefaultFilesFolder :: ChatMonad m => m FilePath
|
|
getDefaultFilesFolder = do
|
|
dir <- (`combine` "Downloads") <$> getHomeDirectory
|
|
ifM (doesDirectoryExist dir) (pure dir) getChatTempDirectory
|