Database Reference
In-Depth Information
and return it; if the same thumbnail is requested a second time, the API serves it from
the database rather than regenerating it.
Consider the following example, where we use cURL to make a request to a RESTXQ
resource function to return an image thumbnail:
curl http://localhost:8080/exist/restxq/image/thumbnail/
24a85a52-5031-4bac-8843-4c7e7701905b.jpg
Observe the
thumbnail/
before the identifier of the image, in com‐
parison to the URI used in
“Retrieve a stored image from the data‐
base” on page 356
.
The code in our
image-api.xqm
file for handling this request is actually very similar to
that for retrieving an image, except for a few minor changes. Therefore, we will only
really examine the differences here:
declare
%rest:GET
%rest:path
(
"/image/thumbnail/{$image-name}"
)
%
rest:produces
(
"image/jpeg"
)
%output:method(
"binary"
)
function
ii:get
-
or-create-thumbnail
(
$
image-name
)
{
let
$
thumbnail-image-name
:=
"thumbnail-"
||
$
image-name
,
$
thumbnail-db-path
:=
$
ii:image-collection
||
"/"
||
$
thumbnail-image-name
return
(: does the thumbnail already exist in the database? :)
if
(
util:binary-doc-available
(
$
thumbnail-db-path
))
then
(: yes, return the thumbnail :)
ii:get-image
(
$
thumbnail-image-name
)
else
(: no, does the original image of which we want a
thumbnail exist in the database? :)
let
$
image
:=
ii:get-image
(
$
image-name
)
return
if
(
not
(
empty
(
$
image
)))
then
(: yes, create the thumbnail :)
let
$
thumbnail
:=
image:scale
(
$
image
,
(
400
,
200
),
"image/jpeg"
),
$
thumbnail-db-path
:=
xmldb:store
(
$
ii:image-collection
,
$
thumbnail-image-name
,
$
thumbnail
,
"image/jpeg"
)
return
$
thumbnail
Search WWH ::
Custom Search