1 -- |MIME Type guessing by a file extension. This is a poor man's way
2 -- 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
25 -- |'Data.Map.Map' from extension to MIME Type.
26 type ExtMap = Map String MIMEType
28 -- |Guess the MIME Type of file.
29 guessTypeByFileName :: ExtMap -> FilePath -> Maybe MIMEType
30 guessTypeByFileName extMap fpath
31 = extMap `seq` fpath `seq`
32 let ext = last $ splitBy (== '.') fpath
34 M.lookup ext extMap >>= return
36 -- |Read an Apache mime.types and parse it.
37 parseExtMapFile :: FilePath -> IO ExtMap
40 do file <- B.readFile fpath
41 case parse (allowEOF extMapP) file of
43 -> return $ compile xs
46 -> let near = B.unpack $ B.take 100 input'
48 fail ("Failed to parse: " ++ fpath ++ " (near: " ++ near ++ ")")
51 extMapP :: Parser [ (MIMEType, [String]) ]
52 extMapP = do xs <- many (comment <|> validLine <|> emptyLine)
60 ( many $ satisfy (/= '\n') ) >>
63 validLine = do _ <- many spc
66 exts <- sepBy token (many spc)
67 return $ Just (mime, exts)
69 emptyLine = oneOf " \t\n" >> return Nothing
72 compile :: [ (MIMEType, [String]) ] -> Map String MIMEType
73 compile = M.fromList . foldr (++) [] . map tr
75 tr :: (MIMEType, [String]) -> [ (String, MIMEType) ]
76 tr (mime, exts) = [ (ext, mime) | ext <- exts ]
78 -- |@'serializeExtMap' extMap moduleName variableName@ generates a
79 -- Haskell source code which contains the following things:
81 -- * A definition of module named @moduleName@.
83 -- * @variableName :: 'ExtMap'@ whose content is a serialization of
86 -- The module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap" is
87 -- surely generated using this function.
88 serializeExtMap :: ExtMap -> String -> String -> String
89 serializeExtMap extMap moduleName variableName
90 = let hsModule = HsModule undefined modName (Just exports) imports decls
91 modName = Module moduleName
92 exports = [HsEVar (UnQual (HsIdent variableName))]
93 imports = [ HsImportDecl undefined (Module "Network.HTTP.Lucu.MIMEType") False Nothing (Just (False, []))
94 , HsImportDecl undefined (Module "Network.HTTP.Lucu.MIMEType.Guess") False Nothing Nothing
95 , HsImportDecl undefined (Module "Data.Map") True (Just (Module "M")) Nothing
97 decls = [ HsTypeSig undefined [HsIdent variableName]
99 (HsTyCon (UnQual (HsIdent "ExtMap"))))
100 , HsFunBind [HsMatch undefined (HsIdent variableName)
101 [] (HsUnGuardedRhs extMapExp) []]
103 extMapExp = HsApp (HsVar (Qual (Module "M") (HsIdent "fromList"))) (HsList records)
104 comment = "{- !!! WARNING !!!\n"
105 ++ " This file is automatically generated.\n"
106 ++ " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
108 comment ++ prettyPrint hsModule ++ "\n"
111 records = map record $ M.assocs extMap
113 record :: (String, MIMEType) -> HsExp
115 = HsTuple [HsLit (HsString ext), mimeToExp mime]
117 mimeToExp :: MIMEType -> HsExp
119 = HsApp (HsVar (UnQual (HsIdent "read"))) (HsLit (HsString $ show mt))