on master help sign in
Fix some lookups
commit3f42a79bb9383a41eabd13f2b8bd053710732f4e authorMichael Snoyman <michael@snoyman.com> dateTue 22 May 2012, 06:36 (14 years ago) parent7eb3b26
3 files changed+45−29
wai-app-static/Backend/Filesystem.hs+30−16 wai-app-static/Listing.hs+8−7 wai-app-static/WaiAppStatic/Types.hs+7−6
wai-app-static/Backend/Filesystem.hs+30−16
@@ -17,6 +17,7 @@ import qualified Network.Wai as W
1717import Listing
1818import Mime
1919import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime)
20+import Data.Maybe (catMaybes)
2021
2122pathFromPieces :: FilePath -> Pieces -> FilePath
2223pathFromPieces = foldl' (\fp p -> fp </> F.fromText (fromPiece p))
@@ -49,15 +50,21 @@ webAppSettingsWithLookup :: FilePath -> ETagLookup -> StaticSettings
4950webAppSettingsWithLookup dir etagLookup =
5051 defaultWebAppSettings { ssLookupFile = webAppLookup etagLookup dir}
5152
53+fileHelperLR :: ETagLookup
54+ -> FilePath -- ^ file location
55+ -> Piece -- ^ file name
56+ -> IO LookupResult
57+fileHelperLR a b c = fmap (maybe LRNotFound LRFile) $ fileHelper a b c
58+
5259fileHelper :: ETagLookup
5360 -> FilePath -- ^ file location
5461 -> Piece -- ^ file name
55− -> IO LookupResult
62+ -> IO (Maybe File)
5663fileHelper hashFunc fp name = do
5764 efs <- try $ getFileStatus $ F.encodeString fp
5865 case efs of
59− Left (_ :: SomeException) -> return LRNotFound
60− Right fs -> return $ LRFile File
66+ Left (_ :: SomeException) -> return Nothing
67+ Right fs -> return $ Just File
6168 { fileGetSize = fromIntegral $ fileSize fs
6269 , fileToResponse = \s h -> W.ResponseFile s h (F.encodeString fp) Nothing
6370 , fileName = name
@@ -69,9 +76,12 @@ type ETagLookup = (FilePath -> IO (Maybe ByteString))
6976
7077webAppLookup :: ETagLookup -> FilePath -> Pieces -> IO LookupResult
7178webAppLookup cachedLookupHash prefix pieces =
72− fileHelper cachedLookupHash fp $ last pieces
79+ fileHelperLR cachedLookupHash fp lastPiece
7380 where
7481 fp = pathFromPieces prefix pieces
82+ lastPiece
83+ | null pieces = unsafeToPiece ""
84+ | otherwise = last pieces
7585
7686defaultFileSystemHash :: ETagLookup
7787defaultFileSystemHash fp = fmap Just $ hashFile fp
@@ -98,7 +108,7 @@ filePathToPiece = unsafeToPiece . either id id . F.toText
98108
99109isVisible :: FilePath -> Bool
100110isVisible =
101− go . F.encodeString
111+ go . F.encodeString . F.filename
102112 where
103113 go ('.':_) = False
104114 go "" = False
@@ -110,21 +120,25 @@ fileSystemLookupHash hashFunc prefix pieces = do
110120 let fp = pathFromPieces prefix pieces
111121 fe <- F.isFile fp
112122 if fe
113− then fileHelper hashFunc fp $ last pieces
123+ then fileHelperLR hashFunc fp lastPiece
114124 else do
115125 de <- F.isDirectory fp
116126 if de
117127 then do
118128 entries' <- fmap (filter isVisible) $ F.listDirectory fp
119129 entries <- forM entries' $ \fp' -> do
120− let name =
121− case toPiece $ either id id $ F.toText $ F.filename fp' of
122− Just p -> p
123− Nothing -> error "fileSystemLookupHash: FIXME"
124− mfile' <- fileHelper hashFunc fp' name
125− return $ case mfile' of
126− LRNotFound -> Left $ Folder (filePathToPiece $ F.filename fp') []
127− LRFolder f -> Left f
128− LRFile f -> Right f
129− return $ LRFolder $ Folder (error "Network.Wai.Application.Static.fileSystemLookup") entries
130+ let name = unsafeToPiece $ either id id $ F.toText $ F.filename fp'
131+ de' <- F.isDirectory fp'
132+ if de'
133+ then return $ Just $ Left name
134+ else do
135+ mfile <- fileHelper hashFunc fp' name
136+ case mfile of
137+ Nothing -> return Nothing
138+ Just file -> return $ Just $ Right file
139+ return $ LRFolder $ Folder lastPiece $ catMaybes entries
130140 else return LRNotFound
141+ where
142+ lastPiece
143+ | null pieces = unsafeToPiece ""
144+ | otherwise = last pieces
wai-app-static/Listing.hs+8−7
@@ -21,8 +21,8 @@ import qualified Text.Blaze.Html.Renderer.Utf8 as HU
2121defaultListing :: Listing
2222defaultListing pieces (Folder _ contents) = do
2323 let isTop = null pieces || map Just pieces == [toPiece ""]
24− let fps'' :: [Either Folder File]
25− fps'' = (if isTop then id else (Left emptyParentFolder :)) contents -- FIXME emptyParentFolder feels like a bit of a hack
24+ let fps'' :: [Either FolderName File]
25+ fps'' = (if isTop then id else (Left (unsafeToPiece "") :)) contents -- FIXME emptyParentFolder feels like a bit of a hack
2626 return $ HU.renderHtmlBuilder
2727 $ H.html $ do
2828 H.head $ do
@@ -72,7 +72,7 @@ defaultListing pieces (Folder _ contents) = do
7272-- see also: 'getMetaData', 'renderDirectoryContents'
7373renderDirectoryContentsTable :: String
7474 -> String
75− -> [Either Folder File]
75+ -> [Either FolderName File]
7676 -> H.Html
7777renderDirectoryContentsTable haskellSrc folderSrc fps =
7878 H.table $ do H.thead $ do H.th ! (A.class_ "first") $ H.img ! (A.src $ H.toValue haskellSrc)
@@ -81,12 +81,13 @@ renderDirectoryContentsTable haskellSrc folderSrc fps =
8181 H.th "Size"
8282 H.tbody $ mapM_ mkRow (zip (sortBy sortMD fps) $ cycle [False, True])
8383 where
84− sortMD :: Either Folder File -> Either Folder File -> Ordering
84+ sortMD :: Either FolderName File -> Either FolderName File -> Ordering
8585 sortMD Left{} Right{} = LT
8686 sortMD Right{} Left{} = GT
87− sortMD (Left a) (Left b) = compare (folderName a) (folderName b)
87+ sortMD (Left a) (Left b) = compare a b
8888 sortMD (Right a) (Right b) = compare (fileName a) (fileName b)
89− mkRow :: (Either Folder File, Bool) -> H.Html
89+
90+ mkRow :: (Either FolderName File, Bool) -> H.Html
9091 mkRow (md, alt) =
9192 (if alt then (! A.class_ "alt") else id) $
9293 H.tr $ do
@@ -95,7 +96,7 @@ renderDirectoryContentsTable haskellSrc folderSrc fps =
9596 Left{} -> H.img ! A.src (H.toValue folderSrc)
9697 ! A.alt "Folder"
9798 Right{} -> return ()
98− let name = either folderName fileName md
99+ let name = either id fileName md
99100 let isFile = either (const False) (const True) md
100101 H.td (H.a ! A.href (H.toValue $ fromPiece name `T.append` if isFile then "" else "/") $ H.toHtml $ fromPiece name)
101102 H.td ! A.class_ "date" $ H.toHtml $
wai-app-static/WaiAppStatic/Types.hs+7−6
@@ -12,15 +12,16 @@ module WaiAppStatic.Types
1212 , MimeType
1313 , Extension
1414 , MimeMap
15− -- * File\/folder serving
15+ -- * Caching
1616 , MaxAge (..)
17+ -- * File\/folder serving
18+ , FolderName
1719 , Folder (..)
1820 , File (..)
1921 , LookupResult (..)
2022 , Listing
2123 -- * Settings
2224 , StaticSettings (..)
23− , emptyParentFolder
2425 ) where
2526
2627import Data.Text (Text)
@@ -84,11 +85,14 @@ data MaxAge = NoMaxAge -- ^ no cache-control set
8485 | MaxAgeSeconds Int -- ^ set to the given number of seconds
8586 | MaxAgeForever -- ^ essentially infinite caching; in reality, probably one year
8687
88+-- | Just the name of a folder.
89+type FolderName = Piece
90+
8791-- | Represent contents of a single folder, which can be itself either a file
8892-- or a folder.
8993data Folder = Folder
9094 { folderName :: Piece
91− , folderContents :: [Either Folder File] -- FIXME replace Folder with FolderName?
95+ , folderContents :: [Either FolderName File]
9296 }
9397
9498-- | Information on an individual file.
@@ -163,6 +167,3 @@ data StaticSettings = StaticSettings
163167 -- FIXME Need clarity on what exactly is going on here.
164168 , ssUseHash :: Bool
165169 }
166−
167−emptyParentFolder :: Folder -- FIXME get rid of this?
168−emptyParentFolder = Folder (Piece "") []