|
1 | 1 | module MIMEs |
2 | 2 |
|
3 | | -export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime |
| 3 | +export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime, mime_from_contenttype |
4 | 4 |
|
5 | 5 | const _mimedb, _ext2mime, _mime2ext = include(joinpath(@__DIR__, "..", "mimedb", "mimedb.jlon")) |
6 | 6 |
|
@@ -147,11 +147,35 @@ contenttype_from_mime(mime_from_extension(".png", MIME"application/octet-stream" |
147 | 147 | ``` |
148 | 148 |
|
149 | 149 | # See also: |
150 | | -[`charset_from_mime`](@ref) |
| 150 | +[`charset_from_mime`](@ref), [`mime_from_contenttype`](@ref) |
151 | 151 | """ |
152 | 152 | contenttype_from_mime(mime::MIME) = let c = charset_from_mime(mime) |
153 | 153 | c === nothing ? string(mime) : "$(string(mime)); charset=$(lowercase(c))" |
154 | 154 | end |
155 | 155 |
|
156 | 156 |
|
| 157 | +""" |
| 158 | +```julia |
| 159 | +mime_from_contenttype(content_type::String[, default::T=nothing])::Union{MIME,T} |
| 160 | +``` |
| 161 | +
|
| 162 | +Extract a MIME from a Content-Type header value. If the input is empty, `default` is returned. |
| 163 | +
|
| 164 | +# Examples: |
| 165 | +```julia |
| 166 | +mime_from_contenttype("application/json; charset=utf-8") == MIME"application/json"() |
| 167 | +mime_from_contenttype("application/x-bogus") == MIME"application/x-bogus"() |
| 168 | +mime_from_contenttype("") == nothing |
| 169 | +mime_from_contenttype("", MIME"application/octet-stream"()) == MIME"application/octet-stream"() |
| 170 | +``` |
| 171 | +
|
| 172 | +# See also: |
| 173 | +[`contenttype_from_mime`](@ref) |
| 174 | +""" |
| 175 | +function mime_from_contenttype(content_type::String, default=nothing) |
| 176 | + result = strip(split(content_type, ';')[1]) |
| 177 | + isempty(result) ? default : MIME(result) |
| 178 | +end |
| 179 | + |
| 180 | + |
157 | 181 | end |
0 commit comments