From 74e5f6fab88a02e18364a935ba025350cde6a42a Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 29 Mar 2022 12:51:50 +0200 Subject: [PATCH 1/5] mime_from_contenttype(::String)::MIME --- src/MIMEs.jl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/MIMEs.jl b/src/MIMEs.jl index aa6440c..b2a6dad 100644 --- a/src/MIMEs.jl +++ b/src/MIMEs.jl @@ -146,11 +146,33 @@ contenttype_from_mime(MIME"application/x-bogus"()) == "application/x-bogus" ``` # 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 +contenttype_from_mime("application/json; charset=utf-8") == MIME"application/json"() +contenttype_from_mime("application/x-bogus") == MIME"application/x-bogus"() +``` + +# 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 From 67b81ae24c9a19111478495a8329559bea58ecb6 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 29 Mar 2022 12:53:00 +0200 Subject: [PATCH 2/5] Update MIMEs.jl --- src/MIMEs.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/MIMEs.jl b/src/MIMEs.jl index b2a6dad..b989dc6 100644 --- a/src/MIMEs.jl +++ b/src/MIMEs.jl @@ -164,6 +164,8 @@ Extract a MIME from a Content-Type header value. If the input is empty, `default ```julia contenttype_from_mime("application/json; charset=utf-8") == MIME"application/json"() contenttype_from_mime("application/x-bogus") == MIME"application/x-bogus"() +contenttype_from_mime("") == nothing +contenttype_from_mime("", MIME"application/octet-stream"()) == MIME"application/octet-stream"() ``` # See also: From 86aacc07ab3503be2eb420052a7e570ff5c64bd9 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 29 Mar 2022 12:57:10 +0200 Subject: [PATCH 3/5] tests --- test/runtests.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 1addf64..4bb06b8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -37,6 +37,12 @@ sub(s) = SubString(s, 1) @test contenttype_from_mime(MIME"application/json"()) == "application/json; charset=utf-8" @test contenttype_from_mime(MIME"application/x-bogus"()) == "application/x-bogus" + +@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", From d0ac0672f1a3dce58d2ff59d74c5a59d6e560bd1 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 29 Mar 2022 12:57:16 +0200 Subject: [PATCH 4/5] docstring fix --- src/MIMEs.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MIMEs.jl b/src/MIMEs.jl index b989dc6..4336514 100644 --- a/src/MIMEs.jl +++ b/src/MIMEs.jl @@ -162,10 +162,10 @@ Extract a MIME from a Content-Type header value. If the input is empty, `default # Examples: ```julia -contenttype_from_mime("application/json; charset=utf-8") == MIME"application/json"() -contenttype_from_mime("application/x-bogus") == MIME"application/x-bogus"() -contenttype_from_mime("") == nothing -contenttype_from_mime("", MIME"application/octet-stream"()) == MIME"application/octet-stream"() +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: From 01ebfe69f8c207e8dc5a413c05d8cce8190df764 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 5 Nov 2024 23:26:31 +0100 Subject: [PATCH 5/5] updates --- README.md | 6 ++++++ src/MIMEs.jl | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) 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 891cde0..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"))