4 -- |MIME Type guessing by a file extension. This is a poor man's way
5 -- of guessing MIME Types. It is simple and fast.
7 -- In general you don't have to use this module directly.
8 module Network.HTTP.Lucu.MIMEType.Guess
16 import Control.Applicative
17 import qualified Data.Ascii as A
18 import Data.Attoparsec.Char8 as P
19 import qualified Data.Attoparsec.Lazy as LP
20 import qualified Data.ByteString.Lazy.Char8 as B
21 import qualified Data.Map as M
24 import Data.Text (Text)
25 import qualified Data.Text as T
26 import Data.Text.Encoding
27 import Language.Haskell.Exts.Build
28 import Language.Haskell.Exts.Extension
29 import Language.Haskell.Exts.Pretty
30 import Language.Haskell.Exts.Syntax
31 import Network.HTTP.Lucu.MIMEType
32 import Prelude.Unicode
33 import System.FilePath
35 -- |'Map' from extension to 'MIMEType'.
36 type ExtMap = Map Text MIMEType
38 -- |Guess the MIME Type of file.
39 guessTypeByFileName ∷ ExtMap → FilePath → Maybe MIMEType
40 guessTypeByFileName extMap fpath
41 = let ext = T.pack $ takeExtension fpath
45 -- |Read an Apache mime.types and parse it.
46 parseExtMapFile ∷ FilePath → IO ExtMap
48 = do file ← B.readFile fpath
49 case LP.parse extMapP file of
50 LP.Done _ xs → return $ compile xs
51 LP.Fail _ _ e → fail ("Failed to parse: " ⧺ fpath ⧺ ": " ⧺ e)
53 extMapP ∷ Parser [ (MIMEType, [Text]) ]
54 extMapP = do xs ← P.many (comment <|> validLine <|> emptyLine)
59 isSpc c = c ≡ '\x20' ∨ c ≡ '\x09'
61 comment ∷ Parser (Maybe (MIMEType, [Text]))
68 validLine ∷ Parser (Maybe (MIMEType, [Text]))
73 exts ← sepBy extP (skipWhile isSpc)
74 return $ Just (mime, exts)
77 extP = decodeUtf8 <$> takeWhile1 (\c → (¬) (isSpc c ∨ c ≡ '\x0A'))
79 emptyLine ∷ Parser (Maybe (MIMEType, [Text]))
85 compile ∷ [ (MIMEType, [Text]) ] → Map Text MIMEType
86 compile = M.fromList ∘ concat ∘ map tr
88 tr ∷ (MIMEType, [Text]) → [ (Text, MIMEType) ]
89 tr (mime, exts) = [ (ext, mime) | ext ← exts ]
91 -- |@'serializeExtMap' extMap moduleName variableName@ generates a
92 -- Haskell source code which contains the following things:
94 -- * A definition of module named @moduleName@.
96 -- * @variableName ∷ 'ExtMap'@ whose content is a serialization of
99 -- The module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap" is
100 -- surely generated using this function.
101 serializeExtMap ∷ ExtMap → String → String → String
102 serializeExtMap extMap moduleName variableName
103 = let hsModule = Module (⊥) (ModuleName moduleName) modPragma
104 Nothing (Just exports) imports decls
105 modPragma = [ LanguagePragma (⊥) [ name (show OverloadedStrings) ] ]
106 exports = [ EVar (UnQual (name variableName)) ]
107 imports = [ ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType")
108 False False Nothing Nothing Nothing
109 , ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType.Guess")
110 False False Nothing Nothing Nothing
111 , ImportDecl (⊥) (ModuleName "Data.Ascii")
112 False False Nothing Nothing (Just (False, []))
113 , ImportDecl (⊥) (ModuleName "Data.Map")
114 True False Nothing (Just (ModuleName "M")) Nothing
116 decls = [ TypeSig (⊥) [name variableName]
117 (TyCon (UnQual (name "ExtMap")))
118 , nameBind (⊥) (name variableName) extMapExp
120 comment = concat [ "{- !!! WARNING !!!\n"
121 , " This file is automatically generated.\n"
122 , " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
124 extMapExp = qvar (ModuleName "M") (name "fromList") `app` listE records
126 comment ⧺ prettyPrint hsModule ⧺ "\n"
129 records = map record $ M.assocs extMap
131 record ∷ (Text, MIMEType) → Exp
133 = tuple [ strE (T.unpack ext)
134 , metaFunction "parseMIMEType" [strE $ mimeToString mime]
137 mimeToString ∷ MIMEType → String
138 mimeToString = A.toString ∘ A.fromAsciiBuilder ∘ printMIMEType