couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cml...@apache.org
Subject svn commit: r644977 - /incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl
Date Fri, 04 Apr 2008 22:42:39 GMT
Author: cmlenz
Date: Fri Apr  4 15:42:36 2008
New Revision: 644977

URL: http://svn.apache.org/viewvc?rev=644977&view=rev
Log:
mochiweb branch: add attachment support, fix db_info.

Modified:
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl?rev=644977&r1=644976&r2=644977&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl Fri Apr  4 15:42:36 2008
@@ -156,11 +156,7 @@
 
 handle_db_request(Req, 'GET', {DbName, Db, []}) ->
     {ok, DbInfo} = couch_db:get_db_info(Db),
-    send_json(Req, {obj, [
-        {"db_name", DbName},
-        {"doc_count", proplists:get_value(doc_count, DbInfo)},
-        {"update_seq", proplists:get_value(last_update_seq, DbInfo)}
-    ]});
+    send_json(Req, {obj, [{db_name, DbName} | DbInfo]});
 
 handle_db_request(Req, 'POST', {_DbName, Db, []}) ->
     % TODO: Etag handling
@@ -371,9 +367,15 @@
 
 % Document request handlers
 
-handle_db_request(Req, Method, {DbName, Db, [Resource]}) ->
-    DocId = mochiweb_util:unquote(Resource),
-    handle_doc_request(Req, Method, DbName, Db, DocId).
+handle_db_request(Req, Method, {DbName, Db, [DocId]}) ->
+    UnquotedDocId = mochiweb_util:unquote(DocId),
+    handle_doc_request(Req, Method, DbName, Db, UnquotedDocId);
+
+handle_db_request(Req, Method, {DbName, Db, [DocId, FileName]}) ->
+    UnquotedDocId = mochiweb_util:unquote(DocId),
+    UnquotedFileName = mochiweb_util:unquote(FileName),
+    handle_attachment_request(Req, Method, DbName, Db, UnquotedDocId,
+                              UnquotedFileName).
 
 handle_doc_request(Req, 'DELETE', _DbName, Db, DocId) ->
     % TODO: Etag handling
@@ -460,6 +462,34 @@
 
 handle_doc_request(_Req, _Method, _DbName, _Db, _DocId) ->
     throw({method_not_allowed, "DELETE,GET,HEAD,PUT"}).
+
+% Attachment request handlers
+
+handle_attachment_request(Req, 'GET', _DbName, Db, DocId, FileName) ->
+    case couch_db:open_doc(Db, DocId, []) of
+    {ok, #doc{attachments=Attachments}} ->
+        case proplists:get_value(FileName, Attachments) of
+        undefined ->
+            throw({not_found, missing});
+        {Type, Bin} ->
+            Resp = Req:respond({200, [
+                {"content-type", Type},
+                {"content-length", integer_to_list(couch_doc:bin_size(Bin))}
+            ], chunked}),
+            couch_doc:bin_foldl(Bin,
+                fun(BinSegment, []) ->
+                    Resp:write_chunk(BinSegment)
+                end,
+                []
+            ),
+            Resp:write_chunk("")
+        end;
+    Error ->
+        throw(Error)
+    end;
+
+handle_attachment_request(_Req, _Method, _DbName, _Db, _DocId, _FileName) ->
+    throw({method_not_allowed, "GET,HEAD"}).
 
 % View request handling internals
 



Mime
View raw message