How to upload and save a picture with eXist-db? - xquery

I am tryng to upload a picture and store it in exist-db but i get the next error when opening the stored picture:
Cannot open specified file: Could not recognize image encoding.
I have tryed the next code with a small adjustment for normal txt files and it works fine but not with pictures.
picture.xhtml
<?xml-model href="http://www.oxygenxml.com/1999/xhtml/xhtml-xforms.nvdl" schematypens="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<title/>
<xf:model>
<xf:instance xmlns="">
<data>
<image xsi:type="xs:base64Binary"/>
</data>
</xf:instance>
<xf:submission id="save" action="save.xquery" method="post"/>
</xf:model>
</head>
<body>
<xf:upload ref="image">
<xf:label>Upload Photo:</xf:label>
</xf:upload>
<br/>
<xf:submit submission="save">
<xf:label>Save</xf:label>
</xf:submit>
</body>
</html>
save.xquery
xquery version "3.1";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $login:=xmldb:login('xmldb:exist:///db/apps/places','admin','admin')
(: The small adjusment i refer is just to change file extension from .jpeg to .txt :)
return xmldb:store("/db/apps/places/",concat("pic",".jpeg"), util:base64-decode(request:get-data()//image))

If you want to store images to the eXist-db you should probably replace xmldb:store() with xmldb:store-as-binary().

Related

HERE GET batch geocoding response 403 but job is complete

I successfully submitted a batch geocoding request...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1">
<Response>
<MetaInfo>
<RequestId>CAOxHo4SFaj17sSB2UyTxeoDKHZR77f9</RequestId>
</MetaInfo>
<Status>accepted</Status>
<TotalCount>0</TotalCount>
<ValidCount>0</ValidCount>
<InvalidCount>0</InvalidCount>
<ProcessedCount>0</ProcessedCount>
<PendingCount>0</PendingCount>
<SuccessCount>0</SuccessCount>
<ErrorCount>0</ErrorCount>
</Response>
</ns2:SearchBatch>
And when I GET status, the job is completed....
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1">
<Response>
<MetaInfo>
<RequestId>CAOxHo4SFaj17sSB2UyTxeoDKHZR77f9</RequestId>
</MetaInfo>
<Status>completed</Status>
<JobStarted>2019-10-31T00:11:58.000Z</JobStarted>
<JobFinished>2019-10-31T00:12:26.000Z</JobFinished>
<TotalCount>13291</TotalCount>
<ValidCount>13291</ValidCount>
<InvalidCount>0</InvalidCount>
<ProcessedCount>13291</ProcessedCount>
<PendingCount>0</PendingCount>
<SuccessCount>13291</SuccessCount>
<ErrorCount>0</ErrorCount>
</Response>
However, when I then go to get the response, I get 403 forbidden
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.</p>
</body>
</html>
I am using the endpoint outlined here: https://developer.here.com/documentation/batch-geocoder/topics/example-get-results.html
In this case you need to pass correct apikey to download the result.
https://batch.geocoder.ls.hereapi.com/6.2/jobs/CAOxHo4SFaj17sSB2UyTxeoDKHZR77f9/result?apiKey={}
Also refer https://developer.here.com/documentation/batch-geocoder/dev_guide/topics/example-get-results.html

Handle frontend login and authentication via LDAP

I've created a MPGW policy to add a front page to authenticate before go to backside :
Requests Rules:
Rule1 : Matchrule: /favicon.ico
Rule2: Matchrule: /loginpage.html Transform: xsl stylesheet to dispaly a html loginpage and add the login & password at the query params in the URL
Rule3: Matchrule: /wps/portal/Home Transform: xsl stylesheet to extract login/passwrd from URL and authenticate user with a LDAP (dp:ldap-authen()) and write result in a context variable
and here is my problem:
Response Rules:
Rule4: Matchrule: "/" Transform : xsl stylesheet " i want to do: if context variable is OK nothing to do else display an error page html " !!!??
i've written in the response stylesheet something like :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions" xmlns:re="http://exslt.org/regular-expressions"
extension-element-prefixes="dp re"
exclude-result-prefixes="dp re">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:if test="string-length(dp:variable('var://context/LDAP_Auth_Status')) = 0">
<!--ERROR-->
<html>
<head>
<title> Error Page</title>
</head>
<body>
<h2> Error USER VALIDITY </h2>
<strong>doom!!</strong>
</body>
</html>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
First, Rule4: Matchrule: "*" should be an asterisk if you want it to match anything.
Secondly, is there a backside response?
Else you have to add a var://service/skip-backside=1 if the Response rule should be run. It wil go to Error rule otherwise.
Also it is good practice to not add values into the context. You should use context variables, e.g.:
var://context/LDAPInfo/LDAP_Auth_Status
You can use <xsl:message pd:priority="debug"> to add information into the logs for easier way to find values. Also use the Probe to find Context variable values and see how the flow runs.

How to parse/download stylesheet from HTML

I am downloading part of an HTML page by:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('https://example.com/index.html'))
wiki = doc./('//*[#id="wiki"]/div[1]')
and I need the stylesheets in order to display it correctly. They are included in the header like so:
<!DOCTYPE html>
<html lang="en" class="">
<head>
...
<link href="https://example.com/9f40a.css" media="all" rel="stylesheet" />
<link href="https://example.com/4e5fb.css" media="all" rel="stylesheet" />
...
</head>
...
and their naming can be changed. How do I parse/download local copies of the stylesheets?
Something like this:
require 'open-uri'
doc.css("head link").each do |tag|
link = tag["href"]
next unless link && link.end_with?("css")
File.open("/tmp/#{File.basename(link)}", "w") do |f|
content = open(link) { |g| g.read }
f.write(content)
end
end
I'm not a ruby expert but you can go over following steps
You can use .scan(...) method provided with String type to parse and get the .css file names. The scan method will return you an array stylesheet file names. Find more info on scan here
Then download and store the files with Net::HTTP.get(...) an example is here

XSLT to format Wordpress WXR XML for importing in to Drupal via Feeds

I'm trying to format a Wordpress WXR file using XSLT so I can import it into Drupal.
I'm aware of modules for Drupal that will import WXR files but I need the flexibility that the Feeds module can give as the imported data will be imported against different content types and I'll be pulling images and other attachments into the newly created Drupal pages. With this in mind the standard WordPress Migrate just won't cut it.
So, the WXR format has Wordpress posts and attachments as separate items within the feed and links the posts an attachments using an id. Attachments can be images, files (pdf,doc etc) and are found at the xpath wp:postmeta/wp:meta_key and have values of _thumbnail_id, _wp_attached_file
What I'd like to do is take various nodes from items of type attachment and put them within the cooresponding post item, where the id links them together
A fragment of the xml to be transformed... First item is post second is attachment. The
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<wp:postmeta>
<wp:meta_key>_thumbnail_id</wp:meta_key>
<wp:meta_value>566</wp:meta_value>
</wp:postmeta>
</item>
...
...
...
<item>
<title>My fantastic attachment</title>
<link>http://www.example.com/fantastic-attachment</link>
<wp:post_id>566</wp:post_id>
<wp:post_type>attachment</wp:post_type>
...
...
...
<wp:attachment_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url>
<wp:postmeta>
<wp:meta_key>_wp_attached_file</wp:meta_key>
<wp:meta_value>2012/12/fantastic.jpg</wp:meta_value>
</wp:postmeta>
</item>
After the transform I would like
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<wp:postmeta>
<wp:meta_key>_thumbnail_id</wp:meta_key>
<wp:meta_value>566</wp:meta_value>
<wp:meta_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url>
</wp:postmeta>
</item>
Maybe, there is a better approach? Maybe merge post and attachment where the id create a link between the nodes?
I'm new to XSLT and have read a few posts on identity transforms and I think thats the correct direction but I just don't have the experience to pull of what i need, assistance would be appreciated.
It looks like I've managed to sort out a solution.
I used a number of indexes to organise the attachments. My requirements changed a little on further inspection of the XML, as there was
I changed my resulting output to be in the format of...
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<thumbnail>
<title>Spaner</title>
<url>http://www.example.com/wp-content/uploads/2012/03/spanner.jpg</url>
</thumbnail>
<attachments>
<attachment>
<title>Fixing your widgets: An idiots guide</title>
<url>http://www.example.com/wp-content/uploads/2012/12/fixiing-widgets.pdf</url>
</attachment>
<attachment>
<title>Do It Yourself Trepanning</title>
<url>http://www.example.com/wp-content/uploads/2013/04/trepanning.pdf</url>
</attachment>
</attachments>
</item>
So using the following xsl gave me the desired result. The conditions on the indexes ensured I was selecting the correct files.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wp="http://wordpress.org/export/1.2/">
<xsl:output indent="yes" cdata-section-elements="content"/>
<!-- Setup indexes -->
<!-- Index all main posts -->
<xsl:key
name="mainposts"
match="*/item[wp:post_type[text()='post']]"
use="wp:post_id" />
<!-- Index all sub posts (posts within posts)-->
<xsl:key
name="subposts"
match="*/item[wp:post_type[text()='post'] and category[#nicename = 'documents']]"
use="category[#domain = 'post_tag']" />
<!-- Index all image thumbs -->
<xsl:key
name="images"
match="*/item[wp:post_type[text()='attachment'] and wp:postmeta/wp:meta_key[text()='_wp_attachment_metadata']]"
use="wp:post_parent" />
<!-- Index all files (unable to sort members file at the moment)-->
<xsl:key
name="attachments"
match="*/item[wp:post_type[text()='attachment'] and not(wp:postmeta/wp:meta_key = '_wp_attachment_metadata')]"
use="wp:post_parent" />
<xsl:key
name="thumbnails"
match="*/item[wp:post_type[text()='attachment']]"
use="wp:post_id" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/item/wp:post_parent[text()= 0]">
<wp:post_parent>
<xsl:value-of select="." />
</wp:post_parent>
<xsl:for-each select="key('thumbnails', ../wp:postmeta[wp:meta_key[text()='_thumbnail_id']]/wp:meta_value)">
<thumbnail>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</thumbnail>
</xsl:for-each>
<xsl:for-each select="key('subposts', ../category[#domain = 'post_tag'])">
<attachments>
<xsl:for-each select="key('images', wp:post_id)">
<file>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</file>
</xsl:for-each>
<xsl:for-each select="key('attachments', wp:post_id)">
<file>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</file>
</xsl:for-each>
</attachments>
</xsl:for-each>
</xsl:template>

App in development hangs every second time it is run

This is a fairly straightforward question for such odd behaviour, but that is exactly what my app is doing on the Playbook. I open the app the first time and it runs perfectly. I close the app, and then open it again and it hangs/freezes after the first action I perform with it. I then close the app, it seems to reset itself, and then opens and runs perfectly the next time.
I am using the latest WebWorks and am debugging on the PB with a debug token. My previous app (written/tested on pre OS2.0) worked, and continues to work, just fine.
I am pleased to post code if you would like, but it may be a waste of space. In an effort to root out the problem, I wrote a quick Hello World app that writes one item to an HTML5 database on the playbook (my main app also uses WebDB), and that app, as simple as it is, has exactly the same behavior. All my apps function fine in Chrome.
If anyone has any thoughts about what could be causing this behaviour, please post a reply.
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="helloworld.js"></script>
</head>
<body>
<input type="text" id="testDBInput"/>
<button type="button" onclick="testDB()">go</button>
</body>
</html>
JavaScript Document:
var taskdb=openDatabase ("helloworldDB", "1.0", "test database", 10*1024*1024);
taskdb.transaction(function(tx)
{
tx.executeSql("CREATE TABLE IF NOT EXISTS maintable (id integer primary key autoincrement, nametitle TEXT)");
});
function testDB()
{
var testTitle=document.getElementById("testDBInput").value;
taskdb.transaction(function(tx)
{
tx.executeSql("INSERT INTO maintable (nametitle) VALUES (?)", [testTitle], function (tx, results)
{
});
});
}
blackberry-tablet.xml:
<?xml version="1.0" encoding="utf-8"?>
<qnx>
<icon>
<image></image>
</icon>
<author>***edited for privacy***</author>
<authorId>***edited for privacy***</authorId>
<platformVersion>1.0.0.0</platformVersion>
</qnx>
config.xml:
<?xml version="1.0" encoding="utf-8"?>
<widget xmlns=" http://www.w3.org/ns/widgets"
xmlns:rim="http://www.blackberry.com/ns/widgets"
version="1.0.0.0">
<name>Tester</name>
<description>
PB Tester
</description>
<rim:orientation mode="landscape"/>
<rim:loadingScreen onFirstLaunch="true" >
</rim:loadingScreen>
<author>***edited for privacy***</author>
<icon src="bdicon.png"/>
<content src="index.html"/>
<feature id="blackberry.app" required="true" version="1.0.0.0"/>
<feature id="blackberry.ui.dialog" required="true" version="1.0.0.0"/>
</widget>
It sounds like the javascript crashed when the appli hangs. testDB() has few lines, you can insert alert() to see which line is not reached

Resources