1 -- |MIME Type guesser which guesses by a file extension. This is a
2 -- poor man's way of guessing MIME Types. It is simple and fast.
4 -- In general you don't have to use this module directly.
5 module Network.HTTP.Lucu.MIMEType.Guess
14 import qualified Data.ByteString.Lazy.Char8 as B
15 import qualified Data.Map as M
18 import Language.Haskell.Pretty
19 import Language.Haskell.Syntax
20 import Network.HTTP.Lucu.MIMEType
21 import Network.HTTP.Lucu.Parser
22 import Network.HTTP.Lucu.Parser.Http
23 import Network.HTTP.Lucu.Utils
26 -- |Map from extension to MIME Type.
27 type ExtMap = Map String MIMEType
29 -- |Guess the MIME Type of file.
30 guessTypeByFileName :: ExtMap -> FilePath -> Maybe MIMEType
31 guessTypeByFileName extMap fpath
32 = extMap `seq` fpath `seq`
33 let ext = last $ splitBy (== '.') fpath
35 M.lookup ext extMap >>= return
37 -- |Read an Apache mime.types and parse it.
38 parseExtMapFile :: FilePath -> IO ExtMap
41 do file <- B.readFile fpath
42 case parse (allowEOF extMapP) file of
43 (Success xs, _) -> return $ compile xs
44 (_, input') -> let near = B.unpack $ B.take 100 input'
46 fail ("Failed to parse: " ++ fpath ++ " (near: " ++ near ++ ")")
49 extMapP :: Parser [ (MIMEType, [String]) ]
50 extMapP = do xs <- many (comment <|> validLine <|> emptyLine)
58 many $ satisfy (/= '\n')
61 validLine = do many spc
64 exts <- sepBy token (many spc)
65 return $ Just (mime, exts)
67 emptyLine = oneOf " \t\n" >> return Nothing
70 compile :: [ (MIMEType, [String]) ] -> Map String MIMEType
71 compile = M.fromList . foldr (++) [] . map tr
73 tr :: (MIMEType, [String]) -> [ (String, MIMEType) ]
74 tr (mime, exts) = [ (ext, mime) | ext <- exts ]
76 -- |@'serializeExtMap' extMap moduleName variableName@ generates a
77 -- Haskell source code which contains the following things:
79 -- * A definition of module named @moduleName@.
81 -- * @variableName :: 'ExtMap'@ whose content is a serialization of
84 -- The module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap" is
85 -- surely generated using this function.
86 serializeExtMap :: ExtMap -> String -> String -> String
87 serializeExtMap extMap moduleName variableName
88 = let hsModule = HsModule undefined modName (Just exports) imports decls
89 modName = Module moduleName
90 exports = [HsEVar (UnQual (HsIdent variableName))]
91 imports = [ HsImportDecl undefined (Module "Network.HTTP.Lucu.MIMEType") False Nothing (Just (False, []))
92 , HsImportDecl undefined (Module "Network.HTTP.Lucu.MIMEType.Guess") False Nothing Nothing
93 , HsImportDecl undefined (Module "Data.Map") True (Just (Module "M")) Nothing
95 decls = [ HsTypeSig undefined [HsIdent variableName]
97 (HsTyCon (UnQual (HsIdent "ExtMap"))))
98 , HsFunBind [HsMatch undefined (HsIdent variableName)
99 [] (HsUnGuardedRhs extMapExp) []]
101 extMapExp = HsApp (HsVar (Qual (Module "M") (HsIdent "fromList"))) (HsList records)
102 comment = "{- !!! WARNING !!!\n"
103 ++ " This file is automatically generated.\n"
104 ++ " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
106 comment ++ prettyPrint hsModule ++ "\n"
109 records = map record $ M.assocs extMap
111 record :: (String, MIMEType) -> HsExp
113 = HsTuple [HsLit (HsString ext), mimeToExp mime]
115 mimeToExp :: MIMEType -> HsExp
117 = HsApp (HsVar (UnQual (HsIdent "read"))) (HsLit (HsString $ show mt))