1 {-# LANGUAGE OverloadedStrings #-}
    2 module Web.GZip
    3   ( generateGZip
    4   ) where
    5 
    6 import qualified Codec.Compression.GZip as GZ
    7 import Control.Monad (mzero)
    8 import Control.Monad.IO.Class
    9 import qualified Data.ByteString.Lazy as BSL
   10 import System.FilePath (takeExtension)
   11 
   12 import Files
   13 import Web
   14 import Web.Types
   15 import Web.Generate
   16 
   17 generateGZip :: WebGenerator
   18 generateGZip fo@(f, _) = do
   19   (b, ext) <- liftIO $ splitWebExtension f
   20   b' <- liftIO $ unRawFilePath $ webFileAbs b
   21   if takeExtension b' `notElem` [".png"] && ext == ".gz" -- things that don't compress
   22     then do
   23       f' <- liftIO $ unRawFilePath $ webFileAbs f
   24       webRegenerate
   25         (BSL.writeFile f' . GZ.compress =<< BSL.readFile b')
   26         [] [b] fo
   27     else mzero