Skip to content

Commit

Permalink
Merge pull request #948 from Vlix/add-extension-map-to-mime-types
Browse files Browse the repository at this point in the history
Add extension map to `mime-types`
  • Loading branch information
snoyberg authored Oct 18, 2023
2 parents db5c7a5 + 1eb50f1 commit 02589e3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ cabal.sandbox.config
*~
stack*.yaml.lock
dist-newstyle/
.hie
6 changes: 6 additions & 0 deletions mime-types/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.1.2.0

* Added `defaultExtensionMap` to provide the inverse of `defaultMimeMap`.

See PR [#930](https://github.com/yesodweb/wai/pull/930) and [#948](https://github.com/yesodweb/wai/pull/948).

## 0.1.1.0

* Replace `audio/x-mpegurl` with IANA registered type
Expand Down
26 changes: 24 additions & 2 deletions mime-types/Network/Mime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ module Network.Mime
-- * Defaults
, defaultMimeType
, defaultMimeMap
, defaultExtensionMap
-- * Utilities
, fileNameExtensions
-- * Types
, FileName
, MimeType
, MimeMap
, Extension
, ExtensionMap
) where

import qualified Data.List as L
import Data.Text (Text)
import qualified Data.Text as T
import Data.ByteString (ByteString)
Expand All @@ -24,6 +27,12 @@ import qualified Data.Map as Map
-- | Maps extensions to mime types.
type MimeMap = Map.Map Extension MimeType

-- | Maps mime types to extensions.
-- The list of extensions is in alphabetical order.
--
-- @since 0.1.2.0
type ExtensionMap = Map.Map MimeType [Extension]

-- | The filename component of a filepath, leaving off the directory but
-- keeping all extensions.
type FileName = Text
Expand Down Expand Up @@ -75,8 +84,21 @@ defaultMimeType = "application/octet-stream"
--
-- Generated from the Apache and nginx mime.types files.
defaultMimeMap :: MimeMap
defaultMimeMap = Map.fromAscList [
("123", "application/vnd.lotus-1-2-3")
defaultMimeMap = Map.fromAscList mimeAscList

-- | A mapping of 'MimeType' to a set of 'Extension's.
--
-- @since 0.1.2.0
defaultExtensionMap :: ExtensionMap
defaultExtensionMap =
L.foldr go mempty mimeAscList
where
go (ext, mimeType) =
Map.alter (Just . maybe [ext] (ext :)) mimeType

mimeAscList :: [(Extension, MimeType)]
mimeAscList =
[ ("123", "application/vnd.lotus-1-2-3")
, ("3dml", "text/vnd.in3d.3dml")
, ("3ds", "image/x-3ds")
, ("3g2", "video/3gpp2")
Expand Down
2 changes: 1 addition & 1 deletion mime-types/mime-types.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mime-types
version: 0.1.1.0
version: 0.1.2.0
synopsis: Basic mime-type handling types and functions
description: API docs and the README are available at <http://www.stackage.org/package/mime-types>.
homepage: https://github.com/yesodweb/wai
Expand Down

0 comments on commit 02589e3

Please sign in to comment.