Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

COUCHDB-259 allow content_type of attachments to be changed via stub #51

Open
wants to merge 1 commit into from

4 participants

Andreas Pieper Jan Lehnardt Dave Cottlehuber Daniel Gruno
Andreas Pieper
argl commented

Patch to allow the content type of attachments to be changed via a document PUT and stub structure. Partially solves COUCHDB-259, but useful anyway. Test included.

Jan Lehnardt
janl commented

Woha, excellent Pull Request! <3

Dave Cottlehuber
dch commented

Wow, best PR ever. Many thanks @argl!

+1 on a quick inspection.

Andreas Pieper
argl commented

Thanks for the encouragement. I am really moved since its my first pull request ever and Erlang is really not my primary language (wipes tear off cheek). I'll try my best to completely solve the rest of couchdb-259.

Jan Lehnardt
janl commented

This looks good. I ran into https://issues.apache.org/jira/browse/COUCHDB-1711 while testing this, so I can’t vouch for this just yet, but once 1711 is out of the way and this still passes, I am happy to get this merged. Sorry for the delay!

Andreas Pieper
argl commented

Thanks Jan

Daniel Gruno
Owner

Should this be closed? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 13, 2013
Andreas Pieper argl COUCHDB-259 allow content_type of attachments to be changed via stub 35ad22d
This page is out of date. Refresh to see the latest.
1  CHANGES
@@ -9,6 +9,7 @@ Apache CouchDB CHANGES
9 9 # HTTP Interface:
10 10 #
11 11 # * Fixed bug with CORS and replication (COUCHDB-1689).
  12 +# * Allow update of content_type in attachments (COUCHDB-259).
12 13 #
13 14 # Test Suite:
14 15 #
49 share/doc/src/api/documents.rst
Source Rendered
@@ -650,6 +650,55 @@ The JSON returned will include the updated revision number:
650 650 For information on batched writes, which can provide improved
651 651 performance, see :ref:`api-batch-writes`.
652 652
  653 +Inline Attachments
  654 +------------------
  655 +
  656 +It is possible to store attachments inline, along with the main
  657 +JSON structure. Attachments go into a special _attachments attribute
  658 +of the document. They are encoded in a JSON structure that holds
  659 +the name, the content_type and the base64 encoded data of an
  660 +attachment.
  661 +
  662 +Creating a document with an attachment:
  663 +
  664 +.. code-block:: javascript
  665 +
  666 + {
  667 + "_id":"attachment_doc",
  668 + "_attachments":
  669 + {
  670 + "foo.txt":
  671 + {
  672 + "content_type":"text/plain",
  673 + "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  674 + }
  675 + }
  676 + }
  677 +
  678 +While metatdata for the attachment (length, MD5-sum, etc.) is
  679 +maintained by the server, it is possible to change the content_type
  680 +field without sending the attachment data. This is useful for cases
  681 +when the supplied content type cannot be trusted (i.e. direct uploads
  682 +via browser).
  683 +
  684 +To change the content type field, PUT the document along with a stub
  685 +attachment structure and the updated content type field:
  686 +
  687 +.. code-block:: javascript
  688 +
  689 + {
  690 + "_id":"attachment_doc",
  691 + "_attachments":
  692 + {
  693 + "foo.txt":
  694 + {
  695 + "stub": true,
  696 + "content_type":"text/x-markdown"
  697 + }
  698 + }
  699 + }
  700 +
  701 +
653 702 .. _api-del-doc:
654 703
655 704 ``DELETE /db/doc``
33 share/www/script/test/attachments.js
@@ -298,4 +298,37 @@ couchTests.attachments= function(debug) {
298 298 var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc7/attachment.txt");
299 299 TEquals('MntvB0NYESObxH4VRDUycw==', xhr.getResponseHeader("Content-MD5"));
300 300
  301 + // test COUCHDB-259 - allow update of content type via stubs
  302 + var bin_doc8 = {
  303 + _id: "bin_doc8",
  304 + _attachments:{
  305 + "foo.txt": {
  306 + content_type:"application/octet-stream",
  307 + data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  308 + }
  309 + }
  310 + };
  311 +
  312 + TEquals(true, db.save(bin_doc8).ok);
  313 +
  314 + // reopen the document, change the content type and update
  315 + bin_doc8 = db.open("bin_doc8");
  316 + bin_doc8._attachments["foo.txt"] = {stub: true, content_type: "text/plain"};
  317 + TEquals(true, db.save(bin_doc8).ok);
  318 + // reopen the docuemnt, check if the new content type persisted
  319 + bin_doc8 = db.open("bin_doc8");
  320 + TEquals("text/plain", bin_doc8._attachments["foo.txt"].content_type);
  321 +
  322 + // try setting to null, this should be ignored by the server
  323 + bin_doc8._attachments["foo.txt"] = {stub: true, content_type: null};
  324 + TEquals(true, db.save(bin_doc8).ok);
  325 + bin_doc8 = db.open("bin_doc8");
  326 + TEquals("text/plain", bin_doc8._attachments["foo.txt"].content_type);
  327 +
  328 + // try setting to undefined, this should be ignored by the server
  329 + bin_doc8._attachments["foo.txt"] = {stub: true, content_type: undefined};
  330 + TEquals(true, db.save(bin_doc8).ok);
  331 + bin_doc8 = db.open("bin_doc8");
  332 + TEquals("text/plain", bin_doc8._attachments["foo.txt"].content_type);
  333 +
301 334 };
7 src/couchdb/couch_doc.erl
@@ -423,11 +423,14 @@ merge_stubs(#doc{id = Id}, nil) ->
423 423 merge_stubs(#doc{id=Id,atts=MemBins}=StubsDoc, #doc{atts=DiskBins}) ->
424 424 BinDict = dict:from_list([{Name, Att} || #att{name=Name}=Att <- DiskBins]),
425 425 MergedBins = lists:map(
426   - fun(#att{name=Name, data=stub, revpos=StubRevPos}) ->
  426 + fun(#att{name=Name, data=stub, revpos=StubRevPos, type=Type}) ->
427 427 case dict:find(Name, BinDict) of
428 428 {ok, #att{revpos=DiskRevPos}=DiskAtt}
429 429 when DiskRevPos == StubRevPos orelse StubRevPos == nil ->
430   - DiskAtt;
  430 + case Type of
  431 + _Binary when is_binary(Type) -> DiskAtt#att{type=Type};
  432 + _Else -> DiskAtt
  433 + end;
431 434 _ ->
432 435 throw({missing_stub,
433 436 <<"id:", Id/binary, ", name:", Name/binary>>})

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.