diff --git a/README.md b/README.md index bfff724..07187da 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,15 @@ A small package to transform between file extensions and MIME types, with bonus ```julia julia> using MIMEs +### For filename extensions: julia> m = mime_from_extension(".json") MIME type application/json julia> extension_from_mime(m) ".json" + +### For web servers: julia> compressible_from_mime(m) # whether content of this MIME can/should be gzipped true @@ -19,6 +22,9 @@ julia> charset_from_mime(m) julia> contenttype_from_mime(m) # the Content-Type HTTP header "application/json; charset=utf-8" + +julia> mime_from_contenttype("application/json; charset=utf-8") +MIME type application/json ``` # Implementation diff --git a/src/MIMEs.jl b/src/MIMEs.jl index 9d82ac0..c8726ff 100644 --- a/src/MIMEs.jl +++ b/src/MIMEs.jl @@ -1,6 +1,6 @@ module MIMEs -export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime +export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime, mime_from_contenttype const _mimedb, _ext2mime, _mime2ext = include(joinpath(@__DIR__, "..", "mimedb", "mimedb.jlon")) @@ -147,11 +147,35 @@ contenttype_from_mime(mime_from_extension(".png", MIME"application/octet-stream" ``` # See also: -[`charset_from_mime`](@ref) +[`charset_from_mime`](@ref), [`mime_from_contenttype`](@ref) """ contenttype_from_mime(mime::MIME) = let c = charset_from_mime(mime) c === nothing ? string(mime) : "$(string(mime)); charset=$(lowercase(c))" end +""" +```julia +mime_from_contenttype(content_type::String[, default::T=nothing])::Union{MIME,T} +``` + +Extract a MIME from a Content-Type header value. If the input is empty, `default` is returned. + +# Examples: +```julia +mime_from_contenttype("application/json; charset=utf-8") == MIME"application/json"() +mime_from_contenttype("application/x-bogus") == MIME"application/x-bogus"() +mime_from_contenttype("") == nothing +mime_from_contenttype("", MIME"application/octet-stream"()) == MIME"application/octet-stream"() +``` + +# See also: +[`contenttype_from_mime`](@ref) +""" +function mime_from_contenttype(content_type::String, default=nothing) + result = strip(split(content_type, ';')[1]) + isempty(result) ? default : MIME(result) +end + + end diff --git a/test/runtests.jl b/test/runtests.jl index f548dc1..260399d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,6 +38,12 @@ sub(s) = SubString(s, 1) @test contenttype_from_mime(MIME"application/x-bogus"()) == "application/x-bogus" @test contenttype_from_mime(mime_from_extension(".png", MIME"application/octet-stream"())) == "image/png" + +@test mime_from_contenttype("application/json; charset=utf-8") == MIME"application/json"() +@test mime_from_contenttype("application/x-bogus") == MIME"application/x-bogus"() +@test mime_from_contenttype("") == nothing +@test mime_from_contenttype("", MIME"application/octet-stream"()) == MIME"application/octet-stream"() + # from https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types const mdn = Dict( ".bin" => "application/octet-stream",