-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission.ur
298 lines (264 loc) · 12.7 KB
/
submission.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
open Bootstrap3
functor Make(M : sig
con key1 :: Name
con keyT :: Type
con keyR :: {Type}
constraint [key1] ~ keyR
con key = [key1 = keyT] ++ keyR
constraint [Filename, Content, MimeType, When, Channel] ~ key
con other :: {Type}
constraint other ~ key
con keyName :: Name
con otherConstraints :: {{Unit}}
constraint [keyName] ~ otherConstraints
val tab : sql_table (key ++ other) ([keyName = map (fn _ => ()) key] ++ otherConstraints)
val keyInj : $(map sql_injectable_prim key)
val keyFl : folder key
val keyShow : show $key
con ukey :: Name
con uother :: {Type}
con ukeyName :: Name
con uotherConstraints :: {{Unit}}
constraint [ukeyName] ~ uotherConstraints
constraint [ukey] ~ [Filename, Content, MimeType, When, Channel]
constraint [ukey] ~ uother
constraint [ukey] ~ key
val user : sql_table ([ukey = string] ++ uother) ([ukeyName = [ukey]] ++ uotherConstraints)
val whoami : transaction (option string)
con fs :: {(Type * Type * Type)}
constraint [ukey, Filename, Content, MimeType, When] ~ fs
constraint key ~ fs
val widgets : $(map Widget.t' fs)
val fl : folder fs
val injs : $(map sql_injectable (map fst3 fs))
val labels : $(map (fn _ => string) fs)
val makeFilename : $key -> string (* username *) -> string
val mayInspect : option string (* username *) -> transaction bool
end) = struct
open M
con others = [Filename = option string, Content = blob, MimeType = string, When = time]
constraint others ~ (key ++ [ukey = string] ++ map fst3 fs)
con submission_hidden_constraints = [Pkey = [ukey, When] ++ map (fn _ => ()) key,
Key = [],
User = []]
con lame :: {{Unit}} = []
constraint lame ~ submission_hidden_constraints
table submission : (key ++ [ukey = string] ++ map fst3 fs
++ [Filename = option string, Content = blob, MimeType = string, When = time])
PRIMARY KEY {{@primary_key [ukey] [key ++ [When = time]] ! ! (_ ++ keyInj)}},
{{one_constraint [#Key] (@Sql.easy_foreign ! ! ! ! ! ! keyFl tab)}},
{{one_constraint [#User] (@Sql.easy_foreign ! ! ! ! ! ! (_ : folder [ukey = _]) user)}}
datatype status = Idle | Uploading | Uploaded of AjaxUpload.handle | Error
val keyInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim keyFl keyInj
table listeners : (key ++ [ukey = option string, Channel = channel {Filename : option string, When : time, ukey : string}])
fun upload k h vs =
cl <- AjaxUpload.claim h;
case cl of
AjaxUpload.NotFound => error <xml>Submission: upload not found</xml>
| AjaxUpload.Found up =>
u <- whoami;
case u of
None => error <xml>Submission: not logged in</xml>
| Some u =>
tm <- now;
@@Sql.easy_insert
[key ++ map fst3 fs ++ [Filename = _, Content = _, MimeType = _, When = _, ukey = _]] [_]
(_ ++ injs ++ keyInj')
(@Folder.concat ! _ (@Folder.concat ! keyFl (@Folder.mp fl))) submission
(k ++ up ++ {When = tm, ukey = u} ++ vs);
queryI1 (SELECT listeners.Channel
FROM listeners
WHERE {@@Sql.easy_where [#Listeners] [key] [_] [_] [_] [_] ! ! keyInj' keyFl k}
AND (listeners.{ukey} IS NULL
OR listeners.{ukey} = {[Some u]}))
(fn {Channel = ch} => send ch {Filename = up.Filename, When = tm, ukey = u})
fun newUpload k =
id <- fresh;
st <- source Idle;
ws <- @Monad.mapR _ [Widget.t'] [fn p => id * p.2]
(fn [nm ::_] [p ::_] (w : Widget.t' p) =>
id <- fresh;
cfg <- @Widget.configure w;
w <- @Widget.create w cfg;
return (id, w))
fl widgets;
up <- AjaxUpload.render {SubmitLabel = None,
OnBegin = set st Uploading,
OnSuccess = fn h => set st (Uploaded h),
OnError = set st Error};
return (Ui.modal
(st <- get st;
case st of
Uploaded h =>
vs <- @Monad.mapR2 _ [Widget.t'] [fn p => id * p.2] [fst3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (_, x) => current (@Widget.value w x))
fl widgets ws;
rpc (upload k h vs)
| _ => alert "Please select a file first.")
<xml>New submission for {[k]}</xml>
<xml>
<div class="form-group">
<label class="form-group" for={id}>File</label>
<span id={id}>
<span dynClass={st <- signal st;
return (case st of
Idle => CLASS ""
| Uploading => CLASS "glyphicon glyphicon-cloud-upload"
| Uploaded _ => CLASS "glyphicon glyphicon-ok"
| Error => CLASS "glyphicon glyphicon-fire")}/>
{up}
</span>
</div>
{@mapX3 [fn _ => string] [Widget.t'] [fn p => id * p.2] [body]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] lab (w : Widget.t' p) (id, x) => <xml>
<div class="form-group">
<label class="control-label" for={id}>{[lab]}</label>
{@Widget.asWidget w x (Some id)}
</div>
</xml>)
fl labels widgets ws}
</xml>
<xml>Submit</xml>)
fun latests f k =
let
fun retrieveLatest u =
b <- mayInspect (Some u);
if not b then
error <xml>Submission: not authorized to retrieve submissions</xml>
else
r <- oneRow1 (SELECT submission.Content, submission.MimeType
FROM submission
WHERE {@@Sql.easy_where [#Submission] [[ukey = _] ++ key] [_] [_] [_] [_] ! !
({ukey = _} ++ keyInj')
(@Folder.cons [ukey] [_] ! keyFl) (k ++ {ukey = u})}
ORDER BY submission.When DESC
LIMIT 1);
case checkMime r.MimeType of
None => error <xml>Submission: bad MIME type on retrieve</xml>
| Some mt =>
setHeader (blessResponseHeader "Content-Disposition")
("attachment; filename=" ^ makeFilename k u);
returnBlob r.Content mt
val listLatest =
b <- mayInspect None;
if not b then
error <xml>Submission: not authorized to list submissions</xml>
else
xm <- queryX1 (SELECT DISTINCT submission.{ukey}
FROM submission
WHERE {@@Sql.easy_where [#Submission] [key] [_] [_] [_] [_] ! ! keyInj' keyFl k}
ORDER BY submission.{ukey})
(fn r => <xml>
<li><a link={retrieveLatest r.ukey}>{[r.ukey]}</a> {f r.ukey}</li>
</xml>);
return <xml>
<ul>
{xm}
</ul>
</xml>
in
rpc listLatest
end
style file
fun retrieve k u wh =
b <- mayInspect (Some u);
if not b then
error <xml>Submission: not authorized to retrieve submissions</xml>
else
r <- oneRow1 (SELECT submission.Content, submission.MimeType
FROM submission
WHERE {@@Sql.easy_where [#Submission] [[ukey = _] ++ key] [_] [_] [_] [_] ! !
({ukey = _} ++ keyInj')
(@Folder.cons [ukey] [_] ! keyFl) (k ++ {ukey = u})}
AND submission.When = {[wh]}
LIMIT 1);
case checkMime r.MimeType of
None => error <xml>Submission: bad MIME type on retrieve</xml>
| Some mt =>
setHeader (blessResponseHeader "Content-Disposition")
("attachment; filename=" ^ makeFilename k u);
returnBlob r.Content mt
structure AllFiles = struct
type a = _
type input = _
fun create {Key = k, User = u} =
ls <- queryL1 (SELECT submission.Filename, submission.When
FROM submission
WHERE {@@Sql.easy_where [#Submission] [key] [_] [_] [_] [_] ! ! keyInj' keyFl k}
AND submission.{ukey} = {[u]}
ORDER BY submission.When);
ls <- source ls;
ch <- channel;
@@Sql.easy_insert [[Channel = _, ukey = _] ++ key] [_]
({Channel = _, ukey = _} ++ keyInj')
(@Folder.concat ! _ keyFl)
listeners ({Channel = ch, ukey = Some u} ++ k);
return {Key = k,
User = u,
Files = ls,
Channel = ch}
fun onload me =
let
fun loop () =
msg <- recv me.Channel;
fs <- get me.Files;
set me.Files ((msg -- ukey) :: fs);
loop ()
in
spawn (loop ())
end
fun render _ me = <xml><div class="file">
<h2>Submissions</h2>
<dyn signal={fs <- signal me.Files;
return <xml><table class="bs3-table table-striped">
{List.mapX (fn r => <xml><tr>
<td>{[r.When]}</td>
<td><a link={retrieve me.Key me.User r.When}><tt>{[r.Filename]}</tt></a></td>
</tr></xml>) fs}
</table></xml>}/>
</div></xml>
fun ui x = {Create = create x,
Onload = onload,
Render = render}
end
structure AllFilesAllUsers = struct
type a = _
type input = _
fun create k =
ls <- queryL1 (SELECT submission.Filename, submission.When, submission.{ukey}
FROM submission
WHERE {@@Sql.easy_where [#Submission] [key] [_] [_] [_] [_] ! ! keyInj' keyFl k}
ORDER BY submission.When);
ls <- source ls;
ch <- channel;
@@Sql.easy_insert [[Channel = _, ukey = _] ++ key] [_]
({Channel = _, ukey = _} ++ keyInj')
(@Folder.concat ! _ keyFl)
listeners ({Channel = ch, ukey = None} ++ k);
return {Key = k,
Files = ls,
Channel = ch}
fun onload me =
let
fun loop () =
msg <- recv me.Channel;
fs <- get me.Files;
set me.Files (msg :: fs);
loop ()
in
spawn (loop ())
end
fun render _ me = <xml><div class="file">
<dyn signal={fs <- signal me.Files;
return <xml><table class="bs3-table table-striped">
{List.mapX (fn r => <xml><tr>
<td>{[r.When]}</td>
<td><a link={retrieve me.Key r.ukey r.When}><tt>{[r.Filename]}</tt></a></td>
</tr></xml>) fs}
</table></xml>}/>
</div></xml>
fun ui x = {Create = create x,
Onload = onload,
Render = render}
end
end