forked from orchid-hybrid/Gallury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallury.ur
342 lines (297 loc) · 9.53 KB
/
gallury.ur
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
(*
* Configuration variables and utility functions
*)
val maximumFileSize = 18*1024*1024 (* 18 MB *)
val thumbsPerPage = 16
fun mapDml f l =
case l of
[] => return ()
| x :: xs => dml (f x); mapDml f xs
fun range ( n : int) (m : int) : list int =
let
val operator = if n < m then plus else minus
fun f n m a = if eq n m then List.rev a else f (operator n 1) m (n :: a)
in
f n m []
end
(*
* Sql database stuff
*)
sequence s
table images : {
Id : int, Title : string,
MimeType : string, Image : blob, Thumbnail : blob
} PRIMARY KEY Id
table tags : {
Id : int, Tag : string
} CONSTRAINT Id FOREIGN KEY Id REFERENCES images(Id)
(*
* CSS stuff
*)
style header
style wrapper
style sidebar
style content
style thumb
style pageNumbers
val cssString = "
.header h1 {
text-align: center;
}
.header a {
text-decoration: none;
}
.wrapper {
display: flex;
}
.sidebar {
float: left;
padding: 10px;
width: 150px;
}
.sidebar a {
text-decoration: none;
font-size: 15px;
}
.content {
/* margin-left: 180px;*/
/* equal to sidebar width + 2*padding. Replaced by flex */
}
span.thumb {
height: 200px;
width: 200px;
display: inline-block;
float: left;
text-align: center;
vertical-align: middle;
}
.page-numbers {
clear: both;
text-align: center;
}
.page-numbers span {
margin: 0px 3px;
padding: 2px 6px;
font-weight: normal;
border: 1px solid;
border-color: pink;
}
.page-numbers a {
text-decoration: none;
}
div.sidebar {
border-width: thick;
border-style: groove;
border-color: gold;
}
div.header {
border-width: thick;
border-style: groove;
border-color: gold;
}
div.content {
border-width: thick;
border-style: groove;
border-color: gold;
width: 100%;
}
div.page-numbers {
border-width: thick;
border-style: groove;
border-color: gold;
padding: 5px;
}
"
fun css () =
returnBlob (textBlob cssString) (blessMime "text/css")
(*
* Image/Thumbnail blobs
*)
fun image id =
r <- oneRow (SELECT images.Image, images.MimeType
FROM images
WHERE images.Id={[id]});
returnBlob r.Images.Image (blessMime r.Images.MimeType)
fun thumbnail id =
r <- oneRow (SELECT images.Thumbnail
FROM images
WHERE images.Id={[id]});
returnBlob r.Images.Thumbnail (blessMime "image/jpeg")
(*
* Query parsing
*)
type query = { Positive : list string, Negative : list string }
fun parseList (s : string) (a : list string) : list string =
case String.split s #"," of
Some (x, xs) => parseList xs (String.trim x :: a)
| None => ((String.trim s) :: a)
fun partition (p : string -> bool) : list string -> query =
List.foldr
(fn x a => if p x then { Positive = a.Positive, Negative = x :: a.Negative }
else { Positive = x :: a.Positive, Negative = a.Negative })
{ Positive = [], Negative = [] }
fun parseQuery (s : string) : query =
let val x = partition (fn s' => String.isPrefix {Full = s', Prefix = "-"}) (parseList s []) in
{ Positive = x.Positive, Negative = (List.mp (fn s' => strsuffix s' 1) x.Negative) } end
(*
* Dynamic query generation
*)
type tag_query = sql_query [] [] [] [Id = int]
fun positiveCondition (tag : string) (q : tag_query) : tag_query =
(SELECT I.Id AS Id
FROM ({{q}}) AS I
JOIN tags ON tags.Id = I.Id AND tags.Tag = {[tag]})
fun negativeCondition (tag : string) (q : tag_query) : tag_query =
(SELECT I.Id AS Id
FROM ({{q}}) AS I
LEFT JOIN tags ON tags.Id = I.Id AND tags.Tag = {[tag]}
WHERE tags.Tag IS NULL)
fun withConditions (q : query) : tag_query =
List.foldl negativeCondition
(List.foldl positiveCondition
(SELECT images.Id AS Id FROM images)
q.Positive)
q.Negative
fun searchQuery queryString =
if eq queryString "" then
(SELECT images.Id AS Id FROM images)
else
withConditions (parseQuery queryString)
(*
* Web pages
*)
fun template (pageTitle : string) sidebarBody contentBody footerBody =
return <xml>
<head>
<title>{[pageTitle]}</title>
<link rel="stylesheet" type="text/css" href={url (css ())}/>
</head>
<body>
<div class={header}>
<a href={url (main ())}><h1>Gallury</h1></a>
</div>
<div class={wrapper}>
<div class={sidebar}>{sidebarBody}</div>
<div class={content}>{contentBody}</div>
</div>
<div class={pageNumbers}>{footerBody}</div>
</body></xml>
and post () =
let
fun submitPost upload =
(* This procedure does all the necessary checks on uploaded content *)
if blobSize (fileData upload.File) > maximumFileSize then
return <xml>Whoa! That one's too big.</xml>
else
case checkMime (fileMimeType upload.File) of
None => return <xml>Whoa! I'm not touching that.</xml>
| Some _ =>
case Thumbnailer.thumbnail upload.File of
None => return <xml><body>
<h1>At least you tried!</h1>
<p>Could not generate thumbnail (invalid image)</p>
<a href={url (main ())}>home</a>
</body></xml>
| Some thumb => uploadPost upload thumb
and uploadPost upload thumb =
(* This thumbnails and stores the uploaded, verified image into the database *)
let
val title = case fileName upload.File of
None => "Untitled"
| Some t => t
in
id <- nextval s;
dml (INSERT INTO images (Id, Title, MimeType, Image, Thumbnail)
VALUES ({[id]}, {[title]},
{[fileMimeType upload.File]}, {[fileData upload.File]}, {[thumb]}));
return <xml><body>
<h1>Uploaded Successfully!</h1>
<a href={url (page id)}>click here</a>
</body></xml>
end
in
template "Post"
<xml/>
<xml>
<form>
<upload{#File}/>
<submit action={submitPost}/>
</form>
</xml>
<xml/>
end
and page id =
let
fun makeLi row = <xml><li>
<a href={url (search row.Tags.Tag 0)}>{[row.Tags.Tag]}</a>
</li></xml>
fun addTags id queryString =
let
val tagList = parseQuery queryString.Tags
in
mapDml (fn t => (INSERT INTO tags (Id, Tag) VALUES ({[id]}, {[t]})))
tagList.Positive;
mapDml (fn t => (DELETE FROM tags WHERE Id = {[id]} AND Tag = {[t]}))
tagList.Negative;
redirect (url (page id))
end
in
title <- oneRow (SELECT images.Title FROM images WHERE images.Id={[id]});
tagLis <- queryX (SELECT tags.Tag FROM tags WHERE tags.Id={[id]} ORDER BY tags.Tag)
makeLi;
template ("Page: " ^ title.Images.Title)
<xml>
<ul>{tagLis}</ul>
Add tags:
<form>
<textbox{#Tags} size=12/>
<submit action={addTags id} style={STYLE "display: none"}/>
</form>
<hr/>
<a href={url (post ())}>Upload</a>
</xml>
<xml>
<img src={url (image id)} style={STYLE "max-width: 100%"}/>
</xml>
<xml/>
end
and search queryString pageNumber =
let
val query = (SELECT I.Id AS Id
FROM ({{searchQuery queryString}}) AS I
ORDER BY Id DESC
LIMIT {thumbsPerPage} OFFSET {thumbsPerPage * pageNumber})
val countQuery = (SELECT COUNT( * ) AS N
FROM ({{searchQuery queryString}}) AS I)
fun roundUp pages = if pages % thumbsPerPage = 0 then
pages / thumbsPerPage
else pages / thumbsPerPage + 1
fun submitSearch query = redirect (url (search query.Query 0))
fun makeThumbnail row =
<xml><span class={thumb}>
<a href={url (page row.Id)}>
<img src={url (thumbnail row.Id)}/></a>
</span></xml>
fun generatePageNumbers pages =
List.mapX (fn n =>
if eq n pageNumber then
<xml><span>{[pageNumber]}</span></xml>
else
<xml><span><a href={url (search queryString n)}>{[n]}</a></span></xml>)
(List.filter (fn n => n >= 0) (range 0 pages))
in
thumbnails <- queryX query makeThumbnail;
pages <- oneRow countQuery;
template "Search"
<xml>
<form>
<textbox{#Query} size=12 value={queryString}/>
<submit action={submitSearch} style={STYLE "display: none"}/>
</form>
<hr/>
<a href={url (post ())}>Upload</a>
</xml>
thumbnails
(generatePageNumbers (roundUp (pages.N)))
end
and main () = search "" 0