From 796a66de5f10b219c5ed196a8c97ed4bd3a227a2 Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Sat, 30 Aug 2008 21:09:31 +0000 Subject: Implement attachment uploading in Futon. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@690590 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_httpd.erl | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 1e5551e6..22adb7dd 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -609,6 +609,30 @@ handle_doc_request(Req, 'GET', _DbName, Db, DocId) -> end_json_response(Resp) end; +handle_doc_request(Req, 'POST', _DbName, Db, DocId) -> + Form = mochiweb_multipart:parse_form(Req), + Rev = proplists:get_value("_rev", Form), + NewAttachments = [{Name, {ContentType, Content}} || + {Name, {ContentType, _}, Content} <- + proplists:get_all_values("_attachments", Form)], + + Doc = case couch_db:open_doc_revs(Db, DocId, [Rev], []) of + {ok, [{ok, Doc0}]} -> Doc0#doc{revs=[Rev]}; + {ok, [Error]} -> throw(Error) + end, + + #doc{attachments=Attachments} = Doc, + NewDoc = Doc#doc{ + attachments = Attachments ++ NewAttachments + }, + {ok, NewRev} = couch_db:update_doc(Db, NewDoc, []), + + send_json(Req, 201, [{"Etag", "\"" ++ NewRev ++ "\""}], {obj, [ + {ok, true}, + {id, DocId}, + {rev, NewRev} + ]}); + handle_doc_request(Req, 'PUT', _DbName, Db, DocId) -> Json = {obj, DocProps} = cjson:decode(Req:recv_body(?MAX_DOC_SIZE)), DocRev = proplists:get_value("_rev", DocProps), @@ -694,7 +718,7 @@ handle_doc_request(Req, 'MOVE', _DbName, Db, SourceDocId) -> ]}); handle_doc_request(_Req, _Method, _DbName, _Db, _DocId) -> - throw({method_not_allowed, "DELETE,GET,HEAD,PUT,COPY,MOVE"}). + throw({method_not_allowed, "DELETE,GET,HEAD,POST,PUT,COPY,MOVE"}). % Useful for debugging % couch_doc_open(Db, DocId) -> -- cgit v1.2.3