4 -- |Guessing MIME Types by file extensions. It's not always accurate
5 -- but 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
18 import qualified Data.Ascii as A
19 import Data.Attoparsec.Char8 as P
20 import qualified Data.Attoparsec.Lazy as LP
21 import qualified Data.ByteString.Lazy.Char8 as B
22 import qualified Data.Map as M
25 import Data.Monoid.Unicode
26 import Data.Text (Text)
27 import qualified Data.Text as T
28 import Data.Text.Encoding
29 import Language.Haskell.Exts.Build
30 import Language.Haskell.Exts.Extension
31 import Language.Haskell.Exts.Pretty
32 import Language.Haskell.Exts.Syntax
33 import Network.HTTP.Lucu.MIMEType
34 import Prelude.Unicode
35 import System.FilePath
37 -- |A 'Map' from file extensions to 'MIMEType's.
38 type ExtMap = Map Text MIMEType
40 -- |Guess the MIME Type of a file.
41 guessTypeByFileName ∷ ExtMap → FilePath → Maybe MIMEType
42 guessTypeByFileName extMap fpath
43 = case takeExtension fpath of
45 (_:ext) → M.lookup (T.pack ext) extMap
47 -- |Read an Apache mime.types and parse it.
48 parseExtMapFile ∷ FilePath → IO ExtMap
50 = do file ← B.readFile fpath
51 case LP.parse extMapP file of
55 Left e → fail (concat [ "Duplicate extension \""
61 → fail ("Failed to parse: " ⧺ fpath ⧺ ": " ⧺ e)
63 extMapP ∷ Parser [ (MIMEType, [Text]) ]
64 extMapP = do xs ← P.many (try comment <|> try validLine <|> emptyLine)
69 isSpc c = c ≡ '\x20' ∨ c ≡ '\x09'
71 comment ∷ Parser (Maybe (MIMEType, [Text]))
72 comment = do skipWhile isSpc
77 validLine ∷ Parser (Maybe (MIMEType, [Text]))
78 validLine = do skipWhile isSpc
81 exts ← sepBy extP (skipWhile isSpc)
82 return $ Just (mime, exts)
85 extP = decodeUtf8 <$> takeWhile1 (\c → (¬) (isSpc c ∨ c ≡ '\x0A'))
87 emptyLine ∷ Parser (Maybe (MIMEType, [Text]))
88 emptyLine = do skipWhile isSpc
92 compile ∷ Ord k ⇒ [(v, [k])] → Either (k, v, v) (Map k v)
93 compile = go (∅) ∘ concat ∘ map tr
95 tr ∷ (v, [k]) → [(k, v)]
96 tr (v, ks) = [(k, v) | k ← ks]
98 go ∷ Ord k ⇒ Map k v → [(k, v)] → Either (k, v, v) (Map k v)
101 = case M.insertLookupWithKey' f k v m of
102 (Nothing, m') → go m' xs
103 (Just v0, _ ) → Left (k, v0, v)
108 -- |@'serializeExtMap' extMap moduleName variableName@ generates a
109 -- Haskell source code which contains the following things:
111 -- * A definition of module named @moduleName@.
113 -- * @variableName :: 'ExtMap'@ whose content is the serialised
116 -- The module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap" is
117 -- surely generated using this function.
118 serializeExtMap ∷ ExtMap → String → String → String
119 serializeExtMap extMap moduleName variableName
120 = let hsModule = Module (⊥) (ModuleName moduleName) modPragma
121 Nothing (Just exports) imports decls
122 modPragma = [ LanguagePragma (⊥) [ name (show OverloadedStrings) ] ]
123 exports = [ EVar (UnQual (name variableName)) ]
124 imports = [ ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType")
125 False False Nothing Nothing Nothing
126 , ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType.Guess")
127 False False Nothing Nothing Nothing
128 , ImportDecl (⊥) (ModuleName "Data.Ascii")
129 False False Nothing Nothing (Just (False, []))
130 , ImportDecl (⊥) (ModuleName "Data.Map")
131 True False Nothing (Just (ModuleName "M")) Nothing
133 decls = [ TypeSig (⊥) [name variableName]
134 (TyCon (UnQual (name "ExtMap")))
135 , nameBind (⊥) (name variableName) extMapExp
136 , InlineSig (⊥) False AlwaysActive (UnQual (name variableName))
138 comment = concat [ "{- !!! WARNING !!!\n"
139 , " This file is automatically generated.\n"
140 , " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
142 extMapExp = qvar (ModuleName "M") (name "fromList") `app` listE records
144 comment ⧺ prettyPrint hsModule ⧺ "\n"
147 records = map record $ M.assocs extMap
149 record ∷ (Text, MIMEType) → Exp
151 = tuple [ strE (T.unpack ext)
152 , function "parseMIMEType" `app` strE (mimeToString mime)
155 mimeToString ∷ MIMEType → String
156 mimeToString = A.toString ∘ A.fromAsciiBuilder ∘ printMIMEType