Skip to content
Browse files

Merge branch 'master' of git://github.com/soitgoes/narwhal-couchdb

  • Loading branch information...
2 parents 2fac1b3 + 85378f3 commit 86eeab85ccc4f1c55a5e599cf0dfaeb8d928f6a3 @rhino-security rhino-security committed Feb 1, 2010
Showing with 59 additions and 5 deletions.
  1. +59 −5 lib/couchdb/database.js
View
64 lib/couchdb/database.js
@@ -355,7 +355,7 @@ var Database = {
* @param {Array} attachmentData Binary data
*/
addAttachmentToDoc: function(doc, attachmentName, contentType, attachmentData) {
- var response = true;
+ var status;
if (doc._rev === undefined) {
throw new Error("Argument 'doc' must have a '_rev' property in order to add attachments.");
@@ -365,25 +365,26 @@ var Database = {
}
var uri = this.uri + doc._id + "/" + attachmentName + "?rev=" + doc._rev;
+
this.httpClient.put(uri, {
headers: [ { "label" : "Content-Type", "value": contentType }],
data: attachmentData.toArray(),
binary: true,
async: false,
dataType: "json",
success: function(data, textStatus) {
- if (!data.ok) {
+ status = textStatus;
+ if (!data.ok) {
response = false;
- return;
+ return status;
}
- doc._rev = data.rev;
},
error: function(xhr, textStatus, errorThrown) {
throw new DatabaseError(xhr.status, "Failed to add attachment");
}
});
- return response;
+ return status;
},
removeAttachmentFromDoc: function(doc, attachmentName) {
var response = true;
@@ -412,6 +413,59 @@ var Database = {
});
return response;
+ },
+ getAttachment: function(docId, attachmentName, options) {
+ var HttpClient = require("couchdb/http_client").HttpClient;
+ var httpClient = new HttpClient();
+ var url = this.uri + docId + "/" + encodeURIComponent(attachmentName);
+ print(url);
+ var util = require("util");
+ var log = this.log;
+ var attachment = {};
+ var isError = false;
+
+ httpClient.get(url, {
+ dataType: "raw",
+ success: function(data) {
+ attachment.data = data;
+ },
+ complete: function(xhr, textStatus) {
+ var contentType;
+ var etag;
+
+ contentType = xhr.getResponseHeader("Content-Type");
+ if (typeof contentType === "string") {
+ attachment.content_type = contentType;
+ }
+
+ etag = xhr.getResponseHeader("Etag");
+ if (typeof etag === "string") {
+ print(options.if_none_match);
+ print(etag);
+ print("\n");
+ if (options.if_none_match && options.if_none_match === etag) {
+ if (options.not_modified) {
+ options.not_modified();
+ return;
+ }
+ }
+ attachment.etag = etag;
+ }
+
+ if (!isError) {
+ if (options.success)
+ options.success(attachment);
+ }
+ },
+ error: function(client, textStatus, errorThrown) {
+ if (errorThrown) {
+ print("HttpClient error: " + errorThrown);
+ }
+ isError = true;
+ if (options.error)
+ options.error(textStatus);
+ }
+ });
}
};

0 comments on commit 86eeab8

Please sign in to comment.
Something went wrong with that request. Please try again.