-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.jl
70 lines (55 loc) · 1.6 KB
/
content.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@enum ContentType HTML Image SVG
abstract type Content end
function getContentData(c::Content)::String
error("No data for $(c)")
end
function contentType(::Content)::ContentType
error("No content type for $(c)")
end
struct NoContent <: Content
why::String
end
getContentData(nc::NoContent)::String = "<div>Unable to display: $(nc.why)</div>"
contentType(::NoContent) = HTML
struct MissingContent <: Content end
function getContentData(::MissingContent)::String
fn = "assets/broken-image.png"
s = read(fn)
return "data:image/png;base64," * base64encode(s)
end
contentType(::MissingContent) = Image
struct HTMLContent <: Content
data::String
end
getContentData(h::HTMLContent)::String = h.data
contentType(::HTMLContent) = HTML
struct SVGContent <: Content
data::String
end
getContentData(h::SVGContent)::String = h.data
contentType(::SVGContent) = SVG
struct ImageContent <: Content
data::String
end
getContentData(i::ImageContent)::String = i.data
contentType(::ImageContent) = Image
function contentFromFile(fn::String)
bits = split(fn, ".")
if length(bits) < 2
return NoContent("unsupported file")
end
ext = lowercase(bits[end])
if ext == "html"
return HTMLContent("data:text/html;base64," * base64encode(read(fn)))
end
if ext == "png"
return ImageContent("data:image/png;base64," * base64encode(read(fn)))
end
if ext == "svg"
return ImageContent("data:image/svg+xml;base64," * base64encode(read(fn)))
end
if ext == "jpg" || ext == "jpeg"
return ImageContent("data:image/png;base64," * base64encode(read(fn)))
end
return NoContent("unsupported file")
end