Commit fbc5a699 authored by mrodzis's avatar mrodzis
Browse files

Merge branch 'feature/tble-image-api' into 'develop'

TBLE API via RESTXQ

See merge request !6
parents a1d3d5a1 3764cebf
xquery version "3.1";
(:~
: This module provides an API to retreive images based on a TextGrid URI and a
: corresponding xml:id used in compliance with the Text-Image-Link-Editor.
: It queries the database for the latest link based on the xml:id and returns
: the image.
:
: @author Mathias Göbel
: @version 1.0
: @since 2.5.1
: :)
module namespace tbleapi="https://fontane-nb.dariah.eu/tble";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace svg="http://www.w3.org/2000/svg";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace xlink="http://www.w3.org/1999/xlink";
import module namespace rest="http://exquery.org/ns/restxq";
import module namespace fontaneTransfo="http://fontane-nb.dariah.eu/Transfo" at "transform.xqm";
(:~
: retreives parameters via REST and returns the corresponding
: image. Resolution is always 1000px in height.
:
: @param $uri – A valid TextGrid Base-URI without prefix
: @param $xmlid – Any xml:id used inside the document specified by $uri
: @param $format – the return format (IIIF: http://iiif.io/api/image/2.0/#format)
: @return response object with the binary data
: :)
declare
%rest:GET
%rest:path("/tble/{$uri}-{$xmlid}.{$format}")
%output:method("binary")
function tbleapi:stream-image($uri as xs:string, $xmlid as xs:string, $format as xs:string)
{
let $url := tbleapi:get-url($uri, $xmlid, $format)
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
$binary-data
};
(:~
: retreives the URL to a clipping generated with Text-Image-Link-Editor
:
: @param $uri – A valid TextGrid Base-URI without prefix
: @param $xmlid – Any xml:id used inside the document specified by $uri
: @param $format – the return format (IIIF: http://iiif.io/api/image/2.0/#format)
: @return the URL to the image as xs:string
:)
declate function tbleapi:get-url($uri as xs:string, $xmlid as xs:string, $format as xs:string)
as xs:string {
let $link := fontaneTransfo:newestTBLELink($uri, $xmlid) (: TODO: move this function here :)
let $shape := $link/@targets => substring-after('#') => substring-before(' ')
let $svgg := $link/root()//svg:g[@id = $link/parent::tei:linkGrp/substring-after(@facs, '#')]
let $image := substring-before($svgg//svg:image/@xlink:href, ".")
let $rect := $svgg//svg:rect[@id = $shape]
let $x := $rect/@x => substring-before('%')
let $y := $rect/@y => substring-before('%')
let $w := $rect/@width => substring-before('%')
let $h := $rect/@height => substring-before('%')
let $rotation := 0
let $url := "https://textgridlab.org/1.0/digilib/rest/IIIF/"
|| $image || '/pct:' || $x || ',' || $y || ',' || $w || ',' || $h
|| "/,1000/" || $rotation || "/default." || $format
return
$url
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment