Skip to content

Commit

Permalink
Merge pull request #8 from JuliaWeb/mime_from_contenttype
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Nov 5, 2024
2 parents c3ce1f5 + 01ebfe6 commit 1a99c17
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
28 changes: 26 additions & 2 deletions src/MIMEs.jl
Original file line number Diff line number Diff line change
@@ -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"))

Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

4 comments on commit 1a99c17

@fonsp
Copy link
Member Author

@fonsp fonsp commented on 1a99c17 Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118787

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" 1a99c173c09c7f602862cdebb93368e06a7a9b66
git push origin v1.0.0

@fonsp
Copy link
Member Author

@fonsp fonsp commented on 1a99c17 Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

1.0 release!

Yay! This package is now stable :)

There is only one 'breaking change':

  • The new function mime_from_contenttype is now exported

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/118787

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" 1a99c173c09c7f602862cdebb93368e06a7a9b66
git push origin v1.0.0

Please sign in to comment.