-
Notifications
You must be signed in to change notification settings - Fork 2
/
contrejour.opa
118 lines (99 loc) · 2.52 KB
/
contrejour.opa
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Contre-jour
// a simple image gallery viewer in OPA
//
// written by Louis Gesbert
import stdlib.io.file
thumbsize=200
imgwidth=800
Jpeg = {{
thumb(file) =
{ jpg = (%%Jpeg.thumb%%)(file, thumbsize, thumbsize) }
resize(file,w,h) =
{ jpg = (%%Jpeg.resize%%)(file,w,h) }
}}
get_image_files(dir) =
fold_dir = %%BslFile.fold_dir_rec_opt%%
lst = fold_dir((lst,_name,path -> "{path}" +> lst), {nil}, dir)
Option.map(List.sort,lst)
disp(img) =
[ #imgdiv <- <a href="/full/{Uri.encode_string(img)}"><img class=medium src="/medium/{img}" /></a> ]
image_elt(img) =
<img class=thumb src="/thumb/{Uri.encode_string(img)}" onclick={_ -> Dom.transform(disp(img))} />
page(dir) =
<div id=#main>
<h3>Showing pictures in {dir}</h3>
<div id=#imgdiv />
</div>
<div id=#list>{
match get_image_files(dir) with
~{some} -> <ul>{List.fold((x,acc -> acc <+> <li>{image_elt(x)}</li> ),some,<></>)}</ul>
{none} -> <>Error: could not load images</>
}</div>
fullimage(jpg) =
Resource.image({ jpg = File.content(jpg) })
medimage(jpg) =
Resource.image(cache(@/medium[jpg], -> Jpeg.resize(jpg, imgwidth, imgwidth)))
thumb(jpg) =
Resource.image(cache(@/thumb[jpg], -> Jpeg.thumb(jpg)))
database ./thumbcache/
db /thumb : stringmap(option(image))
db /thumb[_] full
db /medium : stringmap(option(image))
db /medium[_] full
cache(dbpath,compute) =
match Db.read(dbpath) with
~{some} -> some
{none} ->
img = compute()
do dbpath <- {some = img}
img
server =
dirname = parser f=([a-zA-Z0-9][-a-zA-Z0-9._ ]*) -> Text.to_string(f)
ext_jpg = parser ext=(".jpg"|".jpeg"|".JPG") -> ext
jpg = parser f=(dirname "/" [a-zA-Z0-9] (!ext_jpg [-a-zA-Z0-9._ ])* ext_jpg) -> Text.to_string(f)
simple_server(
parser
| "/full/" ~jpg -> fullimage(jpg)
| "/medium/" ~jpg -> medimage(jpg)
| "/thumb/" ~jpg -> thumb(jpg)
| "/" ~dirname -> html("Contre-jour: a simple gallery in OPA ({dirname})", page(dirname))
)
css = css
body {
color: white;
background-color: black;
}
div#main {
position: fixed;
right: {px(float_of_int(thumbsize) * 1.2)};
top: 0px;
left: 0px;
bottom: 0px;
}
div#list {
position: absolute;
top: 0px;
width: {px(float_of_int(thumbsize) * 1.2)};
right: 0px;
}
#list ul {
list-style-type: none;
padding: 0;
}
#list li {
margin-top: 20px;
margin-bottom: 20px;
}
h3 {
text-align: center;
}
img.thumb {
display: block;
margin: auto;
border: 3px solid white;
}
img.medium {
display: block;
margin: auto;
border: 8px solid white;
}