mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-03-31 09:46:11 +00:00
25 lines
743 B
Haskell
25 lines
743 B
Haskell
module Simplex.FileTransfer.Util
|
|
( uniqueCombine,
|
|
removePath,
|
|
)
|
|
where
|
|
|
|
import Simplex.Messaging.Util (ifM, whenM)
|
|
import System.FilePath (splitExtensions, (</>))
|
|
import UnliftIO
|
|
import UnliftIO.Directory
|
|
|
|
uniqueCombine :: MonadIO m => FilePath -> String -> m FilePath
|
|
uniqueCombine filePath fileName = tryCombine (0 :: Int)
|
|
where
|
|
tryCombine n =
|
|
let (name, ext) = splitExtensions fileName
|
|
suffix = if n == 0 then "" else "_" <> show n
|
|
f = filePath </> (name <> suffix <> ext)
|
|
in ifM (doesPathExist f) (tryCombine $ n + 1) (pure f)
|
|
|
|
removePath :: MonadIO m => FilePath -> m ()
|
|
removePath p = do
|
|
ifM (doesDirectoryExist p) (removeDirectoryRecursive p) $
|
|
whenM (doesFileExist p) (removeFile p)
|