Skip to content

Commit

Permalink
Fix bad use of MIME
Browse files Browse the repository at this point in the history
  • Loading branch information
s-celles committed Mar 12, 2024
1 parent ce0652c commit 78126e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/data_uri.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ supported keyword arguments, or from a string.
"""
struct DataURI
uri::String
mediatype::MIME
mediatype::DataType
isbase64::Bool
data::SubString{String}
parameters::Vector{Pair{String, String}}

end

function DataURI(; mediatype=MIME("text/plain"), encoded=false, data="", parameters=Pair{String, String}[])
function DataURI(; mediatype=MIME"text/plain", encoded=false, data="", parameters=Pair{String, String}[])
base64 = encoded ? ";base64" : ""
s_parameters = ""
for (key, value) in parameters
s_parameters *= ";$(key)=$(value)"
end
uri = "data:$(mediatype)$(s_parameters)$(base64),$(data)"
end
s_mediatype = string(mediatype.parameters[1])
uri = "data:$(s_mediatype)$(s_parameters)$(base64),$(data)"
DataURI(uri, mediatype, encoded, data, parameters)
end

Expand All @@ -36,7 +37,7 @@ function DataURI(str)
if !isnothing(m)
groups = m.captures
mediatype, parameters, data = groups
mime = MIME(mediatype)
mime = MIME{Symbol(mediatype)}
isbase64 = endswith(parameters, "base64")
_parameters = filter(p -> p != "base64", split(parameters, ";"))
parameters = Pair{String, String}[]
Expand All @@ -47,7 +48,7 @@ function DataURI(str)
else
m = match(DATA_URI_PATTERNS[2], str)
data, = m.captures
mime = MIME("")
mime = MIME""
isbase64 = false
parameters = Pair{String, String}[]
end
Expand Down
16 changes: 8 additions & 8 deletions test/data_uri.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const brief_note = "data:,A%20brief%20note"
@testset "parsing" begin
@testset "larry" begin
data_uri = DataURI(larry)
@test data_uri.mediatype == MIME("image/gif")
@test data_uri.mediatype == MIME"image/gif"
@test data_uri.isbase64
@test length(data_uri.parameters) == 0
end

@testset "greek_characters" begin
data_uri = DataURI(greek_characters)
@test data_uri.mediatype == MIME("text/plain")
@test data_uri.mediatype == MIME"text/plain"
@test !data_uri.isbase64
@test getdata(data_uri) == "%be%fg%be"
@test length(data_uri.parameters) == 1
Expand All @@ -26,7 +26,7 @@ const brief_note = "data:,A%20brief%20note"

@testset "brief_note" begin
data_uri = DataURI(brief_note)
@test data_uri.mediatype == MIME("")
@test data_uri.mediatype == MIME""
@test !data_uri.isbase64
@test getdata(data_uri) == "A%20brief%20note"
@test length(data_uri.parameters) == 0
Expand All @@ -36,7 +36,7 @@ const brief_note = "data:,A%20brief%20note"
open("resources/sample_data_uri/img_pluto.txt") do f
content = read(f, String)
data_uri = DataURI(content)
@test data_uri.mediatype == MIME("image/jpeg")
@test data_uri.mediatype == MIME"image/jpeg"
@test data_uri.isbase64
@test length(data_uri.parameters) == 0
end
Expand All @@ -46,7 +46,7 @@ const brief_note = "data:,A%20brief%20note"
open("resources/sample_data_uri/audio_meow.txt") do f
content = read(f, String)
data_uri = DataURI(content)
@test data_uri.mediatype == MIME("audio/mpeg")
@test data_uri.mediatype == MIME"audio/mpeg"
@test data_uri.isbase64
@test length(data_uri.parameters) == 0
end
Expand All @@ -55,12 +55,12 @@ const brief_note = "data:,A%20brief%20note"

@testset "creating" begin
@testset "larry" begin
data_uri = DataURI(; mediatype=MIME("image/gif"), encoded=true, data="R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7")
data_uri = DataURI(; mediatype=MIME"image/gif", encoded=true, data="R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7")
@test data_uri.uri == larry
end

@testset "greek_characters" begin
data_uri = DataURI(; mediatype=MIME("text/plain"), data="%be%fg%be", parameters=["charset" => "iso-8859-7"])
data_uri = DataURI(; mediatype=MIME"text/plain", data="%be%fg%be", parameters=["charset" => "iso-8859-7"])
@test data_uri.uri == greek_characters
end

Expand All @@ -70,7 +70,7 @@ const brief_note = "data:,A%20brief%20note"
end

@testset "brief_note" begin
data_uri = DataURI(; mediatype=MIME(""), data="A%20brief%20note")
data_uri = DataURI(; mediatype=MIME"", data="A%20brief%20note")
@test data_uri.uri == brief_note
end
end
Expand Down

0 comments on commit 78126e1

Please sign in to comment.