Permalink
Browse files

Added getAttachemnt to Database

  • Loading branch information...
1 parent 54dd281 commit 30b658f2122c1a6eadfbbd348e09ed34f6c98a25 @soitgoes soitgoes committed Jan 28, 2010
Showing with 53 additions and 0 deletions.
  1. +53 −0 lib/couchdb/database.js
View
53 lib/couchdb/database.js
@@ -415,6 +415,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 30b658f

Please sign in to comment.