forked from eXist-db/eXide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.xql
76 lines (73 loc) · 3.18 KB
/
load.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
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
(:
: eXide - web-based XQuery IDE
:
: Copyright (C) 2011 Wolfgang Meier
:
: This program is free software: you can redistribute it and/or modify
: it under the terms of the GNU General Public License as published by
: the Free Software Foundation, either version 3 of the License, or
: (at your option) any later version.
:
: This program is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
: GNU General Public License for more details.
:
: You should have received a copy of the GNU General Public License
: along with this program. If not, see <http://www.gnu.org/licenses/>.
:)
xquery version "3.0";
import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";
declare function local:get-run-path($path) {
let $appRoot := repo:get-root()
return
replace(
if (starts-with($path, $appRoot)) then
request:get-context-path() || "/" || request:get-attribute("$exist:prefix") || "/" ||
substring-after($path, $appRoot)
else
request:get-context-path() || "/rest" || $path,
"/{2,}", "/"
)
};
let $path := request:get-parameter("path", ())
let $download := request:get-parameter("download", ())
let $indent := request:get-parameter("indent", true()) cast as xs:boolean
let $expand-xincludes := request:get-parameter("expand-xincludes", false()) cast as xs:boolean
let $omit-xml-declaration := request:get-parameter("omit-xml-decl", true()) cast as xs:boolean
let $mime := xmldb:get-mime-type($path)
let $isBinary := util:is-binary-doc($path)
(: Disable betterFORM filter :)
let $attribute := request:set-attribute("betterform.filter.ignoreResponseBody", "true")
let $header := response:set-header("Content-Type", if ($mime) then $mime else "application/binary")
let $header := response:set-header("X-Link", local:get-run-path($path))
let $header2 :=
if ($download) then
response:set-header("Content-Disposition", concat("attachment; filename=", replace($path, "^.*/([^/]+)$", "$1")))
else
()
return
if (config:access-allowed($path, sm:id()//sm:real/sm:username/string())) then
if ($isBinary) then
let $data := util:binary-doc($path)
return
response:stream-binary($data, $mime, ())
else
if (doc-available($path)) then
(
(: workaround until https://github.com/eXist-db/exist/issues/2394 is resolved :)
util:declare-option(
"exist:serialize",
"indent="
|| (if ($indent) then "yes" else "no")
|| " expand-xincludes="
|| (if ($expand-xincludes) then "yes" else "no")
|| " omit-xml-declaration="
|| (if ($omit-xml-declaration) then "yes" else "no")
),
doc($path)
)
else
response:set-status-code(404)
else
response:set-status-code(404)