1 module Data.Gargantext.Utils.SaveGetHash where
3 import System.FilePath (addExtension, joinPath)
4 import Data.ByteString.Lazy.Internal (ByteString)
5 import Data.List (elem, intersperse, insert)
6 import Data.List.Extra (chunksOf)
7 import Data.Digest.Pure.MD5 (md5)
8 import System.Directory (getDirectoryContents, createDirectory, findFile, createDirectoryIfMissing)
9 import Control.Monad (foldM)
11 import Data.List (splitAt)
12 import Data.ByteString.Lazy.Internal (packChars)
13 import qualified Data.ByteString.Lazy as BL
15 import Codec.Compression.Zlib (compress, decompress)
17 data Config = Config {
29 chunkUrl :: Int -> ByteString -> [[Char]]
30 chunkUrl a url = chunksOf a $ show $ md5 url
32 -- replace it with createDirectoryIfMissing
33 existOrCreate :: [[Char]] -> FilePath -> IO [[Char]]
34 existOrCreate path_ dir = do
35 let path = joinPath path_
36 let returnPath = return $ path_ ++ [dir]
38 is <- elem dir <$> getDirectoryContents path -- ?
43 createDirectory $ path ++ "/" ++ dir
46 doPath :: [[Char]] -> [FilePath] -> IO [[Char]]
47 doPath root path = foldM (\x y -> existOrCreate x y) root path
50 splitAt' :: Int -> Int -> [Char] -> ([Char], [Char], [Char])
51 splitAt' i1 i2 x = (a, b, c) where
52 (a, a') = splitAt i1 x
53 (b, c) = splitAt i2 a'
56 -- ne pas écraser le fichier s'il existe
57 -- spliter l'url proprement
58 saveFile :: ByteString -> String -> IO String
59 saveFile url'' file = do
60 let url' = chunkUrl (chunkSize conf) url''
63 -- add extension according to the filetype
64 let filename = Prelude.foldl addExtension (last url') ["html", "zlib"]
66 doPath [(root conf)] url
68 let path = (root conf) ++ "/" ++ joinPath url ++ "/" ++ filename
70 --case (findFile ["/tmp/sdfs"] "file.hmtl.zib"
73 case (compression conf) of
74 True -> BL.writeFile path (compress $ packChars file)
75 False -> writeFile path file
79 getFile :: FilePath -> IO ByteString
81 case (compression conf) of
82 True -> decompress <$> BL.readFile path
83 False -> packChars <$> Prelude.readFile path
90 -- levensthein distance...