-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.xql
47 lines (40 loc) · 1.92 KB
/
controller.xql
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
xquery version "3.1";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $config := doc("config.xml")/config;
declare variable $sid := $config/sid/string();
declare variable $iiif := $config/textgrid/iiif/string();
declare variable $this-url := $config/url/string();
declare variable $path := tokenize($exist:path, "/")[.!=""];
declare variable $response-header := response:set-header("access-control-allow-origin", "*");
try {
switch ($path[1])
case "image" return
let $url := $iiif||$path[2]||";sid="|| $sid ||"/"||string-join($path[position() gt 2], "/")
let $request := httpclient:get($url, false(), ())
let $data := $request/httpclient:body/text()
let $binary-data := try { $data => xs:base64Binary() }
catch * {
error(QName("err", "load1"), string-join((" Could not load data from TextGrid: ",$data,$err:code,$err:description), " "))
}
let $mime := string($request/httpclient:body/@mimetype)
return
if($exist:resource = "info.json")
then
util:base64-decode($binary-data)
=> replace($iiif, $this-url || "/image/")
=> util:base64-encode()
=> response:stream-binary($mime)
else
response:stream-binary($binary-data, $mime)
case "manifests" return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/manifest.xql">
<add-parameter name="resource" value="{$path[2]}"/>
</forward>
</dispatch>
default return <error>You sent something unexpected.</error>
} catch * {
<error>an error occured.</error>
}