Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 119 lines (99 sloc) 2.576 kb
56ea8bb Initial commit of project
Louis Gesbert authored
1 // Contre-jour
2 // a simple image gallery viewer in OPA
3 //
4 // written by Louis Gesbert
5
67dae6a [fix] compilation with latest version of opa (change in hierarchy of std...
François-Régis Sinot authored
6 import stdlib.io.file
1dc25c3 [fix] compilation on the last version of opa (minor stdlib layout change...
Louis Gesbert authored
7
56ea8bb Initial commit of project
Louis Gesbert authored
8 thumbsize=200
9 imgwidth=800
10
11 Jpeg = {{
12 thumb(file) =
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
13 { jpg = (%%Jpeg.thumb%%)(file, thumbsize, thumbsize) }
56ea8bb Initial commit of project
Louis Gesbert authored
14 resize(file,w,h) =
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
15 { jpg = (%%Jpeg.resize%%)(file,w,h) }
56ea8bb Initial commit of project
Louis Gesbert authored
16 }}
17
18 get_image_files(dir) =
19 fold_dir = %%BslFile.fold_dir_rec_opt%%
20 lst = fold_dir((lst,_name,path -> "{path}" +> lst), {nil}, dir)
21 Option.map(List.sort,lst)
22
23 disp(img) =
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
24 [ #imgdiv <- <a href="/full/{Uri.encode_string(img)}"><img class=medium src="/medium/{img}" /></a> ]
56ea8bb Initial commit of project
Louis Gesbert authored
25
26 image_elt(img) =
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
27 <img class=thumb src="/thumb/{Uri.encode_string(img)}" onclick={_ -> Dom.transform(disp(img))} />
56ea8bb Initial commit of project
Louis Gesbert authored
28
29 page(dir) =
30 <div id=#main>
31 <h3>Showing pictures in {dir}</h3>
32 <div id=#imgdiv />
33 </div>
34 <div id=#list>{
35 match get_image_files(dir) with
36 ~{some} -> <ul>{List.fold((x,acc -> acc <+> <li>{image_elt(x)}</li> ),some,<></>)}</ul>
37 {none} -> <>Error: could not load images</>
38 }</div>
39
40 fullimage(jpg) =
1dc25c3 [fix] compilation on the last version of opa (minor stdlib layout change...
Louis Gesbert authored
41 Resource.image({ jpg = File.content(jpg) })
56ea8bb Initial commit of project
Louis Gesbert authored
42 medimage(jpg) =
43 Resource.image(cache(@/medium[jpg], -> Jpeg.resize(jpg, imgwidth, imgwidth)))
44 thumb(jpg) =
45 Resource.image(cache(@/thumb[jpg], -> Jpeg.thumb(jpg)))
46
47 database ./thumbcache/
48 db /thumb : stringmap(option(image))
49 db /thumb[_] full
50 db /medium : stringmap(option(image))
51 db /medium[_] full
52
53 cache(dbpath,compute) =
54 match Db.read(dbpath) with
55 ~{some} -> some
56 {none} ->
57 img = compute()
58 do dbpath <- {some = img}
59 img
60
61 server =
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
62 dirname = parser f=([a-zA-Z0-9][-a-zA-Z0-9._ ]*) -> Text.to_string(f)
56ea8bb Initial commit of project
Louis Gesbert authored
63 ext_jpg = parser ext=(".jpg"|".jpeg"|".JPG") -> ext
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
64 jpg = parser f=(dirname "/" [a-zA-Z0-9] (!ext_jpg [-a-zA-Z0-9._ ])* ext_jpg) -> Text.to_string(f)
56ea8bb Initial commit of project
Louis Gesbert authored
65 simple_server(
66 parser
da1339d Made safer by restricting the filenames we accept to parse
Louis Gesbert authored
67 | "/full/" ~jpg -> fullimage(jpg)
68 | "/medium/" ~jpg -> medimage(jpg)
69 | "/thumb/" ~jpg -> thumb(jpg)
70 | "/" ~dirname -> html("Contre-jour: a simple gallery in OPA ({dirname})", page(dirname))
56ea8bb Initial commit of project
Louis Gesbert authored
71 )
72
73 css = css
74 body {
75 color: white;
76 background-color: black;
77 }
78
79 div#main {
80 position: fixed;
81 right: {px(float_of_int(thumbsize) * 1.2)};
82 top: 0px;
83 left: 0px;
84 bottom: 0px;
85 }
86
87 div#list {
88 position: absolute;
89 top: 0px;
90 width: {px(float_of_int(thumbsize) * 1.2)};
91 right: 0px;
92 }
93
94 #list ul {
95 list-style-type: none;
96 padding: 0;
97 }
98
99 #list li {
100 margin-top: 20px;
101 margin-bottom: 20px;
102 }
103
104 h3 {
105 text-align: center;
106 }
107
108 img.thumb {
109 display: block;
110 margin: auto;
111 border: 3px solid white;
112 }
113
114 img.medium {
115 display: block;
116 margin: auto;
117 border: 8px solid white;
118 }
Something went wrong with that request. Please try again.