CouchDB patch for attachment uploading in Futon

30 August 2008
12:49

By cmlenz as Diff

1 Index: src/couchdb/couch_httpd.erl
2 ===================================================================
3 --- src/couchdb/couch_httpd.erl (revision 690489)
4 +++ src/couchdb/couch_httpd.erl (working copy)
5 @@ -609,6 +609,30 @@
6 end_json_response(Resp)
7 end;
8
9 +handle_doc_request(Req, 'POST', _DbName, Db, DocId) ->
10 + Form = mochiweb_multipart:parse_form(Req),
11 + Rev = proplists:get_value("_rev", Form),
12 + NewAttachments = [{Name, {ContentType, Content}} ||
13 + {Name, {ContentType, _}, Content} <-
14 + proplists:get_all_values("_attachments", Form)],
15 +
16 + Doc = case couch_db:open_doc_revs(Db, DocId, [Rev], []) of
17 + {ok, [{ok, Doc0}]} -> Doc0#doc{revs=[Rev]};
18 + {ok, [Error]} -> throw(Error)
19 + end,
20 +
21 + #doc{attachments=Attachments} = Doc,
22 + NewDoc = Doc#doc{
23 + attachments = Attachments ++ NewAttachments
24 + },
25 + {ok, NewRev} = couch_db:update_doc(Db, NewDoc, []),
26 +
27 + send_json(Req, 201, [{"Etag", "\"" ++ NewRev ++ "\""}], {obj, [
28 + {ok, true},
29 + {id, DocId},
30 + {rev, NewRev}
31 + ]});
32 +
33 handle_doc_request(Req, 'PUT', _DbName, Db, DocId) ->
34 Json = {obj, DocProps} = cjson:decode(Req:recv_body(?MAX_DOC_SIZE)),
35 DocRev = proplists:get_value("_rev", DocProps),
36 @@ -694,7 +718,7 @@
37 ]});
38
39 handle_doc_request(_Req, _Method, _DbName, _Db, _DocId) ->
40 - throw({method_not_allowed, "DELETE,GET,HEAD,PUT,COPY,MOVE"}).
41 + throw({method_not_allowed, "DELETE,GET,HEAD,POST,PUT,COPY,MOVE"}).
42
43 % Useful for debugging
44 % couch_doc_open(Db, DocId) ->
45 Index: share/www/style/layout.css
46 ===================================================================
47 --- share/www/style/layout.css (revision 690489)
48 +++ share/www/style/layout.css (working copy)
49 @@ -198,7 +198,7 @@
50 width: expression(document.body.clientWidth + 'px');
51 height: expression(document.body.clientHeight + 'px');
52 }
53 -#dialog { background: #333 url(../image/progress.gif) 50% 50% no-repeat;
54 +#dialog { background: #333 url(../image/progress.gif) 50% 50% no-repeat;
55 color: #f4f4f4; overflow: hidden; opacity: .95; max-width: 33em;
56 padding: 1em 1em 0; -moz-border-radius: 7px; -webkit-border-radius: 7px;
57 -webkit-box-shadow: 4px 4px 6px #333;
58 @@ -237,6 +237,11 @@
59 #dialog .buttons button:hover { background: #555; }
60 #dialog .buttons button:active { background: #333; color: #fff; }
61
62 +#dialog fieldset td#progress {
63 + background: url(../image/progress.gif) 50% 50% no-repeat;
64 + visibility: hidden;
65 +}
66 +
67 /* View selector */
68
69 #switch { color: #666; float: right; font-size: 90%; font-weight: bold;
70 Index: share/www/browse/document.html
71 ===================================================================
72 --- share/www/browse/document.html (revision 690489)
73 +++ share/www/browse/document.html (working copy)
74 @@ -23,6 +23,7 @@
75 <script src="../script/jquery.cookies.js?0.8.0"></script>
76 <script src="../script/jquery.couch.js?0.8.0"></script>
77 <script src="../script/jquery.dialog.js?0.8.0"></script>
78 + <script src="../script/jquery.form.js?0.8.0"></script>
79 <script src="../script/jquery.resizer.js?0.8.0"></script>
80 <script src="../script/browse.js?0.8.0"></script>
81 <script src="../script/pprint.js?0.8.0"></script>
82 @@ -61,6 +62,7 @@
83
84 $("#toolbar button.save").click(page.saveDocument);
85 $("#toolbar button.add").click(page.addField);
86 + $("#toolbar button.load").click(page.uploadAttachment);
87 $("#toolbar button.delete").click(page.deleteDocument);
88 });
89 </script>
90 @@ -76,6 +78,7 @@
91 <ul id="toolbar">
92 <li><button class="save">Save Document</button></li>
93 <li><button class="add">Add Field</button></li>
94 + <li><button class="load">Upload Attachment</button></li>
95 <li><button class="delete">Delete Document</button></li>
96 </ul>
97
98 Index: share/www/browse/_upload_attachment.html
99 ===================================================================
100 --- share/www/browse/_upload_attachment.html (revision 0)
101 +++ share/www/browse/_upload_attachment.html (revision 0)
102 @@ -0,0 +1,37 @@
103 +<!--
104 +
105 +Licensed under the Apache License, Version 2.0 (the "License"); you may not use
106 +this file except in compliance with the License. You may obtain a copy of the
107 +License at
108 +
109 + http://www.apache.org/licenses/LICENSE-2.0
110 +
111 +Unless required by applicable law or agreed to in writing, software distributed
112 +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
113 +CONDITIONS OF ANY KIND, either express or implied. See the License for the
114 +specific language governing permissions and limitations under the License.
115 +
116 +-->
117 +<form action="" method="post" id="upload-form">
118 + <h2>Upload Attachment</h2>
119 + <fieldset>
120 + <p class="help">
121 + Please select the file you want to upload as an attachment to this
122 + document.
123 + </p>
124 + <table summary=""><tbody><tr>
125 + <th><label>File 1:</label></th>
126 + <td><input type="file" name="_attachments"></td>
127 + </tr><tr>
128 + <th><label>File 2:</label></th>
129 + <td><input type="file" name="_attachments"></td>
130 + </tr><tr>
131 + <td id="progress" colspan="2">&nbsp;</td>
132 + </tr></table>
133 + </fieldset>
134 + <div class="buttons">
135 + <input type="hidden" name="_rev" value="">
136 + <button type="submit">Upload</button>
137 + <button type="button" class="cancel">Cancel</button>
138 + </div>
139 +</form>
140
141 Property changes on: share/www/browse/_upload_attachment.html
142 ___________________________________________________________________
143 Name: svn:eol-style
144 + native
145
146 Index: share/www/script/browse.js
147 ===================================================================
148 --- share/www/script/browse.js (revision 690489)
149 +++ share/www/script/browse.js (working copy)
150 @@ -648,6 +648,31 @@
151 });
152 }
153
154 + this.uploadAttachment = function() {
155 + if (page.isDirty) {
156 + alert("You need to save or revert any changes you have made to the " +
157 + "document before you can attach a new file.");
158 + return false;
159 + }
160 + $.showDialog("_upload_attachment.html", {
161 + submit: function(data, callback) {
162 + var form = $("#upload-form");
163 + form.find("#progress").css("visibility", "visible");
164 + form
165 + .find("input[name='_rev']").val(page.doc._rev).end()
166 + .ajaxSubmit({
167 + url: db.uri + encodeURIComponent(page.docId),
168 + success: function(resp) {
169 + form.find("#progress").css("visibility", "hidden");
170 + page.isDirty = false;
171 + location.href = "?" + encodeURIComponent(dbName) +
172 + "/" + encodeURIComponent(docId);
173 + }
174 + });
175 + }
176 + });
177 + }
178 +
179 window.onbeforeunload = function() {
180 if (page.isDirty) {
181 return "You've made changes to this document that have not been " +

Download Raw Source

Comments

No comments so far.