On 23.05.2008, at 18:14, Christopher Lenz wrote:
I've started playing with attachment support in Futon. As Futon is a
purely client-side application this is of course tricky (or actually
impossible), and it'll need some kind of hook on the server side.
I have two separate ideas on how this might be enabled, each with
its own advantages and disadvantages:
1. POST /_utils/upload/
This would be a special URL in CouchDB that would only exist to
support Futon. It would take a multipart/form-data POST request,
extra a file upload, and respond with a JSON representation of that
upload, with the content base64 encoded:
handle_upload_request(Req, 'POST') ->
Form = mochiweb_multipart:parse_form(Req),
{Name, {ContentType, _}, Content} = proplists:get_value("file",
Form),
Json = {obj, [
{name, Name},
{type, ContentType},
{content, couch_util:encodeBase64(Content)}
]},
Body = "<textarea>" ++ cjson:encode(Json) ++ "</textarea>",
{ok, Req:ok({"text/html", Body})};
The JSON needs to be wrapped in HTML, because AJAX file uploads need
to go through a separate hidden <iframe> and an impressive number of
hacks. We'd be using the jQuery Form plugin <http://malsup.com/jquery/form/
> to handle that in Futon.
So after submitting the attachment upload form, Futon would get back
a JSON representation of the upload, and add that JSON
representation to the _attachments member of the currently open
document. When the document is saved, the attachment data is part of
the JSON representation of the document, and is thus saved in the
database.
The main drawback of this approach is that the file data is sent
back and forth: the initial upload, the server response, and the
document update request. For large files this will be somewhere
between noticable and a showstopper.
After more playing around, I think this approach is pretty much
infeasible. Uploading even a 1.3MB PDF file causes Firefox3 to grind
to a halt.
Cheers,
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/