module Network.HTTP.Lucu.MIMEType.Guess
- ( parseExtMapFile -- FilePath -> IO (Map String MIMEType)
- , outputExtMapAsHS -- Map String MIMEType -> FilePath -> IO ()
+ ( ExtMap
+ , guessTypeByFileName -- ExtMap -> FilePath -> Maybe MIMEType
+
+ , parseExtMapFile -- FilePath -> IO ExtMap
+ , outputExtMapAsHS -- ExtMap -> FilePath -> IO ()
)
where
import Network.HTTP.Lucu.MIMEType
import Network.HTTP.Lucu.Parser
import Network.HTTP.Lucu.Parser.Http
+import Network.HTTP.Lucu.Utils
import System.IO
-import Debug.Trace
+type ExtMap = Map String MIMEType
+
+
+guessTypeByFileName :: ExtMap -> FilePath -> Maybe MIMEType
+guessTypeByFileName extMap fpath
+ = let ext = head $ reverse $ splitBy (== '.') fpath
+ in
+ M.lookup ext extMap >>= return
+
-parseExtMapFile :: FilePath -> IO (Map String MIMEType)
+parseExtMapFile :: FilePath -> IO ExtMap
parseExtMapFile fpath
= do file <- B.readFile fpath
case parse (allowEOF extMapP) file of
tr (mime, exts) = [ (ext, mime) | ext <- exts ]
-outputExtMapAsHS :: Map String MIMEType -> FilePath -> IO ()
+outputExtMapAsHS :: ExtMap -> FilePath -> IO ()
outputExtMapAsHS extMap fpath
= let hsModule = HsModule undefined modName (Just exports) imports decls
modName = Module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap"