{-# LANGUAGE CPP #-}
module Network.Gitit.Cache ( expireCachedFile
, lookupCache
, cacheContents )
where
import qualified Data.ByteString as B (ByteString, readFile, writeFile)
import System.FilePath
import System.Directory (doesFileExist, removeFile, createDirectoryIfMissing, getModificationTime)
import Data.Time.Clock (UTCTime)
#if MIN_VERSION_directory(1,2,0)
#else
import System.Time (ClockTime(..))
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
#endif
import Network.Gitit.State
import Network.Gitit.Types
import Control.Monad
import Control.Monad.Trans (liftIO)
import Text.Pandoc.UTF8 (encodePath)
expireCachedFile :: String -> GititServerPart ()
expireCachedFile :: String -> GititServerPart ()
expireCachedFile String
file = do
cfg <- GititServerPart Config
getConfig
let target = String -> String
encodePath (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
exists <- liftIO $ doesFileExist target
when exists $ liftIO $ liftIO $ removeFile target
lookupCache :: String -> GititServerPart (Maybe (UTCTime, B.ByteString))
lookupCache :: String -> GititServerPart (Maybe (UTCTime, ByteString))
lookupCache String
file = do
cfg <- GititServerPart Config
getConfig
let target = String -> String
encodePath (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
exists <- liftIO $ doesFileExist target
if exists
then liftIO $ do
#if MIN_VERSION_directory(1,2,0)
modtime <- getModificationTime target
#else
TOD secs _ <- getModificationTime target
let modtime = posixSecondsToUTCTime $ fromIntegral secs
#endif
contents <- B.readFile target
return $ Just (modtime, contents)
else return Nothing
cacheContents :: String -> B.ByteString -> GititServerPart ()
cacheContents :: String -> ByteString -> GititServerPart ()
cacheContents String
file ByteString
contents = do
cfg <- GititServerPart Config
getConfig
let target = String -> String
encodePath (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
let targetDir = String -> String
takeDirectory String
target
liftIO $ do
createDirectoryIfMissing True targetDir
B.writeFile target contents