{-# 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
Config
cfg <- GititServerPart Config
getConfig
let target :: String
target = String -> String
encodePath forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
Bool
exists <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
target
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
exists forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO ()
removeFile String
target
lookupCache :: String -> GititServerPart (Maybe (UTCTime, B.ByteString))
lookupCache :: String -> GititServerPart (Maybe (UTCTime, ByteString))
lookupCache String
file = do
Config
cfg <- GititServerPart Config
getConfig
let target :: String
target = String -> String
encodePath forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
Bool
exists <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
target
if Bool
exists
then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
#if MIN_VERSION_directory(1,2,0)
UTCTime
modtime <- String -> IO UTCTime
getModificationTime String
target
#else
TOD secs _ <- getModificationTime target
let modtime = posixSecondsToUTCTime $ fromIntegral secs
#endif
ByteString
contents <- String -> IO ByteString
B.readFile String
target
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (UTCTime
modtime, ByteString
contents)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
cacheContents :: String -> B.ByteString -> GititServerPart ()
cacheContents :: String -> ByteString -> GititServerPart ()
cacheContents String
file ByteString
contents = do
Config
cfg <- GititServerPart Config
getConfig
let target :: String
target = String -> String
encodePath forall a b. (a -> b) -> a -> b
$ Config -> String
cacheDir Config
cfg String -> String -> String
</> String
file
let targetDir :: String
targetDir = String -> String
takeDirectory String
target
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Bool -> String -> IO ()
createDirectoryIfMissing Bool
True String
targetDir
String -> ByteString -> IO ()
B.writeFile String
target ByteString
contents