danyx23 / elm-mimetype / MimeType

This modules provides the union type MimeType to model some of the most common mime types and a parsing function that tries to parse a MimeType. The possible values for MimeType are all union types as well that specify the Sub-type. It was originally developed to classify files dropped into the browser via the HTML5 Drag and Drop api.

This library ATM provides only an incomplete, somewhat arbitrary mapping of the most common browser mime types. See https://code.google.com/p/chromium/codesearch#chromium/src/net/base/mime_util.cc&l=201 for a full list of Mime types as implemented in chromium.

Mime type


type MimeType
    = Image MimeImage
    | Audio MimeAudio
    | Video MimeVideo
    | Text MimeText
    | App MimeApp
    | OtherMimeType String

Models the major types image, audio, video and text with a subtype or OtherMimeType

Parsing function & toString

parseMimeType : String -> Maybe MimeType

Tries to parse the Mime type from a string.

-- normal use of a type/subtype that is modelled:
parseMimeType "image/jpeg" == Just (Image Jpeg)


-- use of a subtype that is not modelled ATM
parseMimeType "image/tiff" == Just (Image OtherImage)


-- use with an empty string
parseMimeType "" == Nothing


-- use with something else
parseMimeType "bla" == Just OtherMimeType

toString : MimeType -> String

Transforms a MimeType back to a string represenation. Note that this only works properly for correctly recognized mime types at the moment. A future version of this library will instead store the originally parsed mime type.

toString (Image Jpeg) == "image/jpeg"

Subtypes


type MimeText
    = PlainText
    | Html
    | Css
    | Xml
    | Json
    | OtherText String

Models the most common text subtypes


type MimeImage
    = Jpeg
    | Png
    | Gif
    | OtherImage String

Models the most common image subtypes


type MimeAudio
    = Mp3
    | Ogg
    | Wav
    | OtherAudio String

Models the most common audio subtypes


type MimeVideo
    = Mp4
    | Mpeg
    | Quicktime
    | Avi
    | Webm
    | OtherVideo String

Models the most common video subtypes


type MimeApp
    = Word
    | WordXml
    | Excel
    | ExcelXml
    | PowerPoint
    | PowerPointXml
    | Pdf
    | OtherApp String

Models the most common app subtypes