@@ -17,6 +17,7 @@ import qualified Network.Wai as W
17import Listing
17import Listing
18import Mime
18import Mime
19import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime)
19import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime)
20+import Data.Maybe (catMaybes)
21pathFromPieces :: FilePath -> Pieces -> FilePath
22pathFromPieces :: FilePath -> Pieces -> FilePath
22pathFromPieces = foldl' (\fp p -> fp </> F.fromText (fromPiece p))
23pathFromPieces = foldl' (\fp p -> fp </> F.fromText (fromPiece p))
@@ -49,15 +50,21 @@ webAppSettingsWithLookup :: FilePath -> ETagLookup -> StaticSettings
49webAppSettingsWithLookup dir etagLookup =
50webAppSettingsWithLookup dir etagLookup =
50 defaultWebAppSettings { ssLookupFile = webAppLookup etagLookup dir}
51 defaultWebAppSettings { ssLookupFile = webAppLookup etagLookup dir}
53+fileHelperLR :: ETagLookup
54+ -> FilePath -- ^ file location
55+ -> Piece -- ^ file name
57+fileHelperLR a b c = fmap (maybe LRNotFound LRFile) $ fileHelper a b c
52fileHelper :: ETagLookup
59fileHelper :: ETagLookup
53 -> FilePath -- ^ file location
60 -> FilePath -- ^ file location
54 -> Piece -- ^ file name
61 -> Piece -- ^ file name
55− -> IO LookupResult
62+ -> IO (Maybe File)
56fileHelper hashFunc fp name = do
63fileHelper hashFunc fp name = do
57 efs <- try $ getFileStatus $ F.encodeString fp
64 efs <- try $ getFileStatus $ F.encodeString fp
58 case efs of
65 case efs of
59− Left (_ :: SomeException) -> return LRNotFound
66+ Left (_ :: SomeException) -> return Nothing
60− Right fs -> return $ LRFile File
67+ Right fs -> return $ Just File
61 { fileGetSize = fromIntegral $ fileSize fs
68 { fileGetSize = fromIntegral $ fileSize fs
62 , fileToResponse = \s h -> W.ResponseFile s h (F.encodeString fp) Nothing
69 , fileToResponse = \s h -> W.ResponseFile s h (F.encodeString fp) Nothing
63 , fileName = name
70 , fileName = name
@@ -69,9 +76,12 @@ type ETagLookup = (FilePath -> IO (Maybe ByteString))
70webAppLookup :: ETagLookup -> FilePath -> Pieces -> IO LookupResult
77webAppLookup :: ETagLookup -> FilePath -> Pieces -> IO LookupResult
71webAppLookup cachedLookupHash prefix pieces =
78webAppLookup cachedLookupHash prefix pieces =
72− fileHelper cachedLookupHash fp $ last pieces
79+ fileHelperLR cachedLookupHash fp lastPiece
74 fp = pathFromPieces prefix pieces
81 fp = pathFromPieces prefix pieces
83+ | null pieces = unsafeToPiece ""
84+ | otherwise = last pieces
76defaultFileSystemHash :: ETagLookup
86defaultFileSystemHash :: ETagLookup
77defaultFileSystemHash fp = fmap Just $ hashFile fp
87defaultFileSystemHash fp = fmap Just $ hashFile fp
@@ -98,7 +108,7 @@ filePathToPiece = unsafeToPiece . either id id . F.toText
99isVisible :: FilePath -> Bool
109isVisible :: FilePath -> Bool
100isVisible =
110isVisible =
101− go . F.encodeString
111+ go . F.encodeString . F.filename
103 go ('.':_) = False
113 go ('.':_) = False
104 go "" = False
114 go "" = False
@@ -110,21 +120,25 @@ fileSystemLookupHash hashFunc prefix pieces = do
110 let fp = pathFromPieces prefix pieces
120 let fp = pathFromPieces prefix pieces
111 fe <- F.isFile fp
121 fe <- F.isFile fp
113− then fileHelper hashFunc fp $ last pieces
123+ then fileHelperLR hashFunc fp lastPiece
115 de <- F.isDirectory fp
125 de <- F.isDirectory fp
118 entries' <- fmap (filter isVisible) $ F.listDirectory fp
128 entries' <- fmap (filter isVisible) $ F.listDirectory fp
119 entries <- forM entries' $ \fp' -> do
129 entries <- forM entries' $ \fp' -> do
121− case toPiece $ either id id $ F.toText $ F.filename fp' of
130+ let name = unsafeToPiece $ either id id $ F.toText $ F.filename fp'
122− Just p -> p
131+ de' <- F.isDirectory fp'
123− Nothing -> error "fileSystemLookupHash: FIXME"
132+ if de'
133+ then return $ Just $ Left name
124− mfile' <- fileHelper hashFunc fp' name
135+ mfile <- fileHelper hashFunc fp' name
125− return $ case mfile' of
136+ case mfile of
126− LRNotFound -> Left $ Folder (filePathToPiece $ F.filename fp') []
137+ Nothing -> return Nothing
127− LRFolder f -> Left f
138+ Just file -> return $ Just $ Right file
129− return $ LRFolder $ Folder (error "Network.Wai.Application.Static.fileSystemLookup") entries
139+ return $ LRFolder $ Folder lastPiece $ catMaybes entries
130 else return LRNotFound
140 else return LRNotFound
143+ | null pieces = unsafeToPiece ""
144+ | otherwise = last pieces