dba/files
library moduleR
Summary
Upload files.
- Tags
Author: Christian Grün, BaseX Team 2005-21, BSD License
__source : files/file-upload.xqm
Imports
This module is imported by 0 modules. It imports 2 modules.
Variables
Functions
4.1 dba:file-upload
Arities: dba:file-upload#1R
dba:file-upload
(
$files
as
map(xs:string, xs:base64Binary)
)
as
element(rest:response)
- files
as
map(xs:string, xs:base64Binary)
map with uploaded files
element(rest:response)
redirection
Invokes 12 functions from 9 modules
- config:directory#0
- util:info#3
- {http://basex.org/modules/convert}binary-to-string#1
- {http://basex.org/modules/prof}void#1
- {http://basex.org/modules/web}redirect#2
- {http://basex.org/modules/xquery}parse#2
- {http://expath.org/ns/file}write-binary#2
- {http://www.w3.org/2005/xpath-functions/map}for-each#2
- {http://www.w3.org/2005/xpath-functions/map}keys#1
- {http://www.w3.org/2005/xpath-functions}false#0
- {http://www.w3.org/2005/xpath-functions}matches#2
- {http://www.w3.org/2005/xpath-functions}true#0
Invoked by 0 functions from 0 modules
Annotations
%rest:POST | () |
%rest:path | ('/dba/file-upload') |
%rest:form-param | ('files','{$files}') |
Source ( 23 lines)
function dba:file-upload(
$files as map(xs:string, xs:base64Binary)
) as element(rest:response) {
(: save files :)
let $dir := config:directory()
return try {
(: Parse all XQuery files; reject files that cannot be parsed :)
map:for-each($files, function($file, $content) {
if(matches($file, '\.xqm?$')) then (
prof:void(xquery:parse(
convert:binary-to-string($content),
map { 'plan': false(), 'pass': true(), 'base-uri': $dir || $file }
))
) else ()
}),
map:for-each($files, function($file, $content) {
file:write-binary($dir || $file, $content)
}),
web:redirect($dba:CAT, map { 'info': util:info(map:keys($files), 'file', 'uploaded') })
} catch * {
web:redirect($dba:CAT, map { 'error': 'Upload failed: ' || $err:description })
}
}
Namespaces
The following namespaces are defined:
Prefix | Uri |
---|---|
config | dba/config |
dba | dba/files 2 3 4 5 6 7 8 |
rest | http://exquery.org/ns/restxq |
util | dba/util |
Source Code
(:~
: Upload files.
:
: @author Christian Grün, BaseX Team 2005-21, BSD License
:)
module namespace dba = 'dba/files';
import module namespace config = 'dba/config' at '../lib/config.xqm';
import module namespace util = 'dba/util' at '../lib/util.xqm';
(:~ Top category :)
declare variable $dba:CAT := 'files';
(:~
: Upploads files.
: @param $files map with uploaded files
: @return redirection
:)
declare
%rest:POST
%rest:path('/dba/file-upload')
%rest:form-param('files', '{$files}')
function dba:file-upload(
$files as map(xs:string, xs:base64Binary)
) as element(rest:response) {
(: save files :)
let $dir := config:directory()
return try {
(: Parse all XQuery files; reject files that cannot be parsed :)
map:for-each($files, function($file, $content) {
if(matches($file, '\.xqm?$')) then (
prof:void(xquery:parse(
convert:binary-to-string($content),
map { 'plan': false(), 'pass': true(), 'base-uri': $dir || $file }
))
) else ()
}),
map:for-each($files, function($file, $content) {
file:write-binary($dir || $file, $content)
}),
web:redirect($dba:CAT, map { 'info': util:info(map:keys($files), 'file', 'uploaded') })
} catch * {
web:redirect($dba:CAT, map { 'error': 'Upload failed: ' || $err:description })
}
};