diff --git a/src/Resource.jl b/src/Resource.jl index b66d1581..4910232b 100644 --- a/src/Resource.jl +++ b/src/Resource.jl @@ -138,12 +138,18 @@ struct DownloadButton end DownloadButton(data) = DownloadButton(data, "result") - function Base.show(io::IO, m::MIME"text/html", db::DownloadButton) - write(io, " ") @@ -151,12 +157,22 @@ function Base.show(io::IO, m::MIME"text/html", db::DownloadButton) write(io, "") end +function downloadbutton_data(object::Any, mime::Union{MIME, Nothing})::String + data = if object isa String || object isa AbstractVector{UInt8} || isnothing(mime) + object + else + repr(mime, object) + end + Base64.base64encode(data) +end + + ### # MIMES ### "Attempt to find the MIME pair corresponding to the extension of a filename. Defaults to `text/plain`." -function mime_fromfilename(filename; default=nothing, filename_maxlength=2000) +function mime_fromfilename(filename; default::T=nothing, filename_maxlength=2000)::Union{MIME, T} where T if length(filename) > filename_maxlength default else diff --git a/test/runtests.jl b/test/runtests.jl index 00fa42f2..ce5e0132 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -133,6 +133,17 @@ end end +@testset "DownloadButton" begin + data = "test" + db1 = DownloadButton(data, "test.txt") + db2 = DownloadButton(html"test", "test.html") + + hr(x) = repr(MIME"text/html"(), x) + + hr(db1) + hr(db2) +end + function default(x) new = AbstractPlutoDingetjes.Bonds.initial_value(x) if Core.applicable(Base.get, x)