diff --git a/src/data_uri.jl b/src/data_uri.jl index 3a7aaad..bb38b4f 100644 --- a/src/data_uri.jl +++ b/src/data_uri.jl @@ -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 @@ -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}[] @@ -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 diff --git a/test/data_uri.jl b/test/data_uri.jl index a42f7ee..711aff0 100644 --- a/test/data_uri.jl +++ b/test/data_uri.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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