@@ -1,5 +1,6 @@
11{-# LANGUAGE ScopedTypeVariables #-}
22{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE BangPatterns #-}
34-- | Access files on the filesystem.
45module WaiAppStatic.Storage.Filesystem
56 ( -- * Types
@@ -12,6 +13,7 @@ module WaiAppStatic.Storage.Filesystem
1213
1314import WaiAppStatic.Types
1415import System.FilePath ((</>))
16+import System.IO (withBinaryFile, IOMode(..))
1517import System.Directory (doesFileExist, doesDirectoryExist, getDirectoryContents)
1618import Data.List (foldl')
1719import Control.Monad (forM)
@@ -25,7 +27,7 @@ import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime, isRe
2527import Data.Maybe (catMaybes)
2628import Data.ByteArray.Encoding
2729import Crypto.Hash (hashlazy, MD5, Digest)
28−import qualified Data.ByteString.Lazy as BL (readFile)
30+import qualified Data.ByteString.Lazy as BL (hGetContents)
2931import qualified Data.Text as T
3032
3133-- | Construct a new path from a root and some @Pieces@.
@@ -118,9 +120,10 @@ webAppLookup hashFunc prefix pieces =
118120-- | MD5 hash and base64-encode the file contents. Does not check if the file
119121-- exists.
120122hashFile :: FilePath -> IO ByteString
121−hashFile fp = do
122− f <- BL.readFile fp
123− return $ convertToBase Base64 (hashlazy f :: Digest MD5)
123+hashFile fp = withBinaryFile fp ReadMode $ \h -> do
124+ f <- BL.hGetContents h
125+ let !hash = hashlazy f :: Digest MD5
126+ return $ convertToBase Base64 hash
124127
125128hashFileIfExists :: ETagLookup
126129hashFileIfExists fp = do