Skip to content

Commit 08414c1

Browse files
committed
Improve auto-mime filename recognition
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.
1 parent 6938494 commit 08414c1

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DocInventories"
22
uuid = "43dc2714-ed3b-44b5-b226-857eda1aa7de"
33
authors = ["Michael Goerz <mail@michaelgoerz.net>"]
4-
version = "0.4.0"
4+
version = "0.4.0+dev"
55

66
[deps]
77
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"

src/mimetypes.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ const MIME_TYPES = Dict(
2929
function splitfullext(filepath::String)
3030
root, ext = splitext(filepath)
3131
full_ext = ext
32-
33-
# Keep splitting until no more extensions are found
34-
while ext != ""
32+
if ext == ".gz"
3533
root, ext = splitext(root)
36-
(ext == "") && break
37-
full_ext = ext * full_ext
34+
if (ext != "")
35+
full_ext = ext * full_ext
36+
end
3837
end
3938
return root, full_ext
4039
end

test/test_inventory.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Test
22
using TestingUtilities: @Test
33
using DocInventories
4-
using DocInventories: uri, spec, find_in_inventory, split_url, show_full, set_metadata
4+
using DocInventories:
5+
uri, spec, find_in_inventory, split_url, show_full, set_metadata, auto_mime
56
using DocInventories: InventoryFormatError
67
using Downloads: RequestError
78
using IOCapture: IOCapture
@@ -757,3 +758,22 @@ end
757758
end
758759

759760
end
761+
762+
763+
@testset "auto_mime" begin
764+
@test auto_mime("objects.inv") == MIME("application/x-intersphinx")
765+
@test auto_mime("Julia-1.10.2.inv") == MIME("application/x-intersphinx")
766+
@test auto_mime("objects.txt") == MIME("text/x-intersphinx")
767+
@test auto_mime("Julia-1.10.2.txt") == MIME("text/x-intersphinx")
768+
@test auto_mime("objects.txt.gz") == MIME("text/x-intersphinx+gzip")
769+
@test auto_mime("Julia-1.10.2.txt.gz") == MIME("text/x-intersphinx+gzip")
770+
@test auto_mime("inventory.toml") == MIME("application/toml")
771+
@test auto_mime("Julia-1.10.2.toml") == MIME("application/toml")
772+
@test auto_mime("inventory.toml.gz") == MIME("application/toml+gzip")
773+
@test auto_mime("Julia-1.10.2.toml.gz") == MIME("application/toml+gzip")
774+
captured = IOCapture.capture(rethrow=Union{}) do
775+
auto_mime("inventory.toml.zip")
776+
end
777+
@test captured.value isa ArgumentError
778+
@test contains(captured.output, "Cannot determine MIME type")
779+
end

0 commit comments

Comments
 (0)