Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 77 lines (66 sloc) 2.737 kb
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
1 {-
2 Copyright (C) 2008 John MacFarlane <jgm@berkeley.edu>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -}
18
19 {- Functions for maintaining user list and session state.
20 -}
21
22 module Network.Gitit.Cache ( expireCachedFile
23 , lookupCache
24 , cacheContents )
25 where
26
27 import qualified Data.ByteString as B (ByteString, readFile, writeFile)
28 import System.FilePath
586399e John MacFarlane Added atom feeds.
authored
29 import System.Directory (doesFileExist, removeFile, createDirectoryIfMissing, getModificationTime)
30 import System.Time (ClockTime)
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
31 import Network.Gitit.State
32 import Network.Gitit.Types
33 import Control.Monad
34 import Control.Monad.Trans (liftIO)
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
35 import Codec.Binary.UTF8.String (encodeString)
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
36
37 -- | Expire a cached file, identified by its filename in the filestore.
0e86a7b John MacFarlane Added caching for exported PDFs.
authored
38 -- If there is an associated exported PDF, expire it too.
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
39 -- Returns () after deleting a file from the cache, fails if no cached file.
40 expireCachedFile :: String -> GititServerPart ()
41 expireCachedFile file = do
42 cfg <- getConfig
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
43 let target = encodeString $ cacheDir cfg </> file
6d918a1 John MacFarlane Moved check for useFeed config option to cacheHtml & cachedHtml.
authored
44 exists <- liftIO $ doesFileExist target
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
45 when exists $ liftIO $ do
46 liftIO $ removeFile target
47 expireCachedPDF target
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
48
0e86a7b John MacFarlane Added caching for exported PDFs.
authored
49 expireCachedPDF :: String -> IO ()
50 expireCachedPDF file =
51 when (takeExtension file == ".page") $ do
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
52 let pdfname = file ++ ".export.pdf"
53 exists <- doesFileExist pdfname
54 when exists $ removeFile pdfname
0e86a7b John MacFarlane Added caching for exported PDFs.
authored
55
586399e John MacFarlane Added atom feeds.
authored
56 lookupCache :: String -> GititServerPart (Maybe (ClockTime, B.ByteString))
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
57 lookupCache file = do
58 cfg <- getConfig
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
59 let target = encodeString $ cacheDir cfg </> file
6d918a1 John MacFarlane Moved check for useFeed config option to cacheHtml & cachedHtml.
authored
60 exists <- liftIO $ doesFileExist target
61 if exists
62 then liftIO $ do
63 modtime <- getModificationTime target
64 contents <- B.readFile target
65 return $ Just (modtime, contents)
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
66 else return Nothing
67
68 cacheContents :: String -> B.ByteString -> GititServerPart ()
69 cacheContents file contents = do
70 cfg <- getConfig
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
71 let target = encodeString $ cacheDir cfg </> file
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
72 let targetDir = takeDirectory target
6d918a1 John MacFarlane Moved check for useFeed config option to cacheHtml & cachedHtml.
authored
73 liftIO $ do
3bd0461 John MacFarlane Changed caching model and simplified ContentTransformer.
authored
74 createDirectoryIfMissing True targetDir
798dfc0 John MacFarlane Fixed caching for unicode page names.
authored
75 B.writeFile target contents
76 expireCachedPDF target
Something went wrong with that request. Please try again.