Monday, August 3, 2009

Binary Content


Formats

The usual approach to using rdbhost is to request records, consisting of multiple fields, and that record data is incorporated into a structured JSON or XML page and delivered to requesting client. The client can then disassemble the structure to extract the data for use. Those JSON or XML formatted pages are text, and non-text data gets encoded/escaped to make it fit a textual structure.

Sometimes, though, you just want the raw data, without textual escaping. This would be useful for an image database, for example. You would like to just send the binary image data to the browser or other client, and have it processed without any text-binary decoding.


Binary Format

The 'binary' format does just this. If your request calls for a binary format, using the format parameter, all the fields of all the records of the output are just catenated together and the resulting blob is sent to the client. I would expect the typical use is querying for one field of one record, but if you have need to aggregate multiple fields together, it will do that. The data to be queried this way should generally be stored in bytea fields; bytea fields are delivered as-is, others are utf-8 decoded and non-decodable characters are escaped.

When you request binary format, the format parameter value can have content-type appended to it, delimited by a colon; for example "format=binary:image/jpeg" would result in the query data being sent as a binary blob, and with a 'content-type: image/jpeg' header line. If you are delivering the binary content directly to a browser, the content-type is very helpful to the browser in rendering the object correctly.

Inserting Binary Data into Database

Getting the binary data into the database is its own problem. Our sql_form supports http file uploads of binary data. Put the binary data in a file, and upload it through the form, and it will be processed by the server as binary. Unless you put it into a bytea field, it will be utf-8 encoded going into the table, and will be utf-8 decoded when delivered in response to a SELECT. The Python DB API module does not, today, support binary uploads, but that is planned for the next version. In the meantime, the PostgreSQL decode(...,'base64') function can be used to put binary data into a field, but you would need a client side base64 encoder to pre-encode the data.

Sample Page

A sample page can be viewed here. The full url is below, and it incorporates the account and the query all into the query string. The page itself is delivered in response to a database query, and the embedded image comes from another database query.

http://dev.rdbhost.com/db/r0000000763?q=select+data+from+d+where+tag%3d%27page%27&format=binary:text/html

That page was created using the sql_form form linked from the profile page, with SQL INSERTs and the file upload feature.


David

17 May 2010
Updated to correct url.

No comments:

Post a Comment