Skip to content

Commit

Permalink
ordered dict
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Nov 5, 2024
1 parent a33d64f commit f787d3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mimedb/mimedb.jlon

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions mimedb/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ Pkg.instantiate()

import JSON
import Downloads: download
import OrderedCollections: OrderedDict

version = v"1.53.0"

mdb = joinpath(@__DIR__, "mimedb.jlon")

_source_preference = ("nginx", "apache", nothing, "iana")



unorder(d::AbstractDict) = Dict(k => unorder(v) for (k, v) in d)
unorder(d::AbstractVector) = map(unorder, d)
unorder(x::Any) = x


# Check if there's a new version and alert the user, don't automatically take
# it as the format of the JSON may change
begin
Expand All @@ -35,7 +43,7 @@ end
begin
@info "📩 downloading the DB..."
url = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@$(version)/db.json"
d = JSON.parse(read(download(url), String))
d = JSON.parse(read(download(url), String); dicttype=OrderedDict)
_mimedb = let
d["text/julia"] = Dict{String,Any}(
"charset" => "UTF-8",
Expand All @@ -54,17 +62,19 @@ begin
# Ported straight from https://github.com/jshttp/mime-types/blob/2.1.35/index.js#L154
for (mime_str, val) in _mimedb
mime = mime_str
@assert mime isa String

exts = get(val, "extensions", nothing)
if exts === nothing
continue
end
@assert exts isa Vector
_mime2ext[mime] = exts

src = get(val, "source", nothing)
for ex in exts
if haskey(_ext2mime, ex)
other_src = get(_mimedb[identity(_ext2mime[ex])], "source", nothing)
other_src = get(_mimedb[_ext2mime[ex]], "source", nothing)

from = findfirst(isequal(other_src), _source_preference)
to = findfirst(isequal(src), _source_preference)
Expand All @@ -73,7 +83,7 @@ begin
!(_ext2mime[ex] isa MIME"application/octet-stream") &&
(
from > to ||
(from == to && startswith(identity(_ext2mime[ex]), "application/")
(from == to && startswith(_ext2mime[ex], "application/")
)
)
)
Expand All @@ -88,7 +98,7 @@ begin
@info "✏ writing to file $mdb..."
open(mdb, "w") do f
write(f, string(
(_mimedb, _ext2mime, _mime2ext)
(unorder(_mimedb), _ext2mime, _mime2ext)
)
)
end
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const mdn = Dict(
".jsonld" => "application/ld+json",
".mjs" => "text/javascript",
".mp3" => "audio/mpeg",
".mp4" => "video/mp4",
".mp4" => "application/mp4",
".mpeg" => "video/mpeg",
".ogx" => "application/ogg", ".otf" => "font/otf",
".png" => "image/png",
Expand Down

0 comments on commit f787d3e

Please sign in to comment.