Skip to content

Commit

Permalink
Improve auto-mime filename recognition
Browse files Browse the repository at this point in the history
Fix the MIME identification of file names like "Julia-1.10.2.inv" and
"Julia-1.10.2.toml.gz". That is, split file extensions with ".gz"
correctly, but don't split off too much.
  • Loading branch information
goerz committed Mar 27, 2024
1 parent 6938494 commit 08414c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DocInventories"
uuid = "43dc2714-ed3b-44b5-b226-857eda1aa7de"
authors = ["Michael Goerz <[email protected]>"]
version = "0.4.0"
version = "0.4.0+dev"

[deps]
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Expand Down
9 changes: 4 additions & 5 deletions src/mimetypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ const MIME_TYPES = Dict(
function splitfullext(filepath::String)
root, ext = splitext(filepath)
full_ext = ext

# Keep splitting until no more extensions are found
while ext != ""
if ext == ".gz"
root, ext = splitext(root)
(ext == "") && break
full_ext = ext * full_ext
if (ext != "")
full_ext = ext * full_ext
end
end
return root, full_ext
end
Expand Down
22 changes: 21 additions & 1 deletion test/test_inventory.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Test
using TestingUtilities: @Test
using DocInventories
using DocInventories: uri, spec, find_in_inventory, split_url, show_full, set_metadata
using DocInventories:
uri, spec, find_in_inventory, split_url, show_full, set_metadata, auto_mime
using DocInventories: InventoryFormatError
using Downloads: RequestError
using IOCapture: IOCapture
Expand Down Expand Up @@ -757,3 +758,22 @@ end
end

end


@testset "auto_mime" begin
@test auto_mime("objects.inv") == MIME("application/x-intersphinx")
@test auto_mime("Julia-1.10.2.inv") == MIME("application/x-intersphinx")
@test auto_mime("objects.txt") == MIME("text/x-intersphinx")
@test auto_mime("Julia-1.10.2.txt") == MIME("text/x-intersphinx")
@test auto_mime("objects.txt.gz") == MIME("text/x-intersphinx+gzip")
@test auto_mime("Julia-1.10.2.txt.gz") == MIME("text/x-intersphinx+gzip")
@test auto_mime("inventory.toml") == MIME("application/toml")
@test auto_mime("Julia-1.10.2.toml") == MIME("application/toml")
@test auto_mime("inventory.toml.gz") == MIME("application/toml+gzip")
@test auto_mime("Julia-1.10.2.toml.gz") == MIME("application/toml+gzip")
captured = IOCapture.capture(rethrow=Union{}) do
auto_mime("inventory.toml.zip")
end
@test captured.value isa ArgumentError
@test contains(captured.output, "Cannot determine MIME type")
end

0 comments on commit 08414c1

Please sign in to comment.