2 Module : Gargantext.Prelude.Utils
3 Description : Useful Tools near Prelude of the project
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE FlexibleContexts #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE OverloadedStrings #-}
16 module Gargantext.Prelude.Utils
19 import Control.Lens (view)
20 import Control.Monad.Random.Class (MonadRandom)
21 import Control.Monad.Reader (MonadReader)
22 import Control.Monad.Reader (ask)
23 import Data.Text (Text)
24 import GHC.IO (FilePath)
25 import Gargantext.API.Admin.Settings
26 import Gargantext.Database.Admin.Types.Node (NodeId, NodeType)
27 import Gargantext.Prelude
28 import System.Directory (createDirectoryIfMissing)
29 import System.Random (newStdGen)
30 import qualified Data.ByteString.Lazy.Char8 as Char
31 import qualified Data.Digest.Pure.SHA as SHA (sha256, showDigest)
32 import qualified Data.Text as Text
33 import qualified System.Random.Shuffle as SRS
35 --------------------------------------------------------------------------
36 shuffle :: MonadRandom m => [a] -> m [a]
37 shuffle ns = SRS.shuffleM ns
39 --------------------------------------------------------------------------
47 --------------------------------------------------------------------------
48 data NodeToHash = NodeToHash { nodeType :: NodeType
52 type FolderPath = FilePath
53 type FileName = FilePath
55 toPath :: Int -> Text -> (FolderPath,FileName)
56 toPath n x = (Text.unpack $ Text.intercalate "/" [x1,x2], Text.unpack xs)
58 (x1,x') = Text.splitAt n x
59 (x2,xs) = Text.splitAt n x'
61 class SaveFile a where
62 saveFile' :: FilePath -> a -> IO ()
64 class ReadFile a where
65 readFile' :: FilePath -> IO a
68 writeFile :: (MonadReader env m, MonadBase IO m, HasSettings env, SaveFile a)
71 dataPath <- view (settings . fileFolder) <$> ask
72 (fp,fn) <- liftBase $ (toPath 3) . sha . Text.pack . show <$> newStdGen
74 let foldPath = dataPath <> "/" <> fp
75 filePath = foldPath <> "/" <> fn
77 _ <- liftBase $ createDirectoryIfMissing True foldPath
78 _ <- liftBase $ saveFile' filePath a
83 readFile :: (MonadReader env m, MonadBase IO m, HasSettings env, ReadFile a)
86 dataPath <- view (settings . fileFolder) <$> ask
87 liftBase $ readFile' $ dataPath <> "/" <> fp