Permalink
Browse files

Added support for writing attachments using an arbitrary stream as a …

…source.
  • Loading branch information...
Sebastian Negomireanu
Sebastian Negomireanu committed Nov 30, 2009
1 parent f30baa9 commit a83e8b69eac23299f4923dc0705d87d8bd2f7b1a
Showing with 27 additions and 0 deletions.
  1. +27 −0 src/CouchDatabase.cs
View
@@ -6,6 +6,7 @@
using System;
using Divan.Lucene;
using Newtonsoft.Json.Linq;
+using System.IO;
namespace Divan
{
@@ -287,6 +288,32 @@ public ICouchDocument WriteAttachment(ICouchDocument document, string attachment
return document;
}
+ /// <summary>
+ /// Writes the attachment.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="attachmentName">Name of the attachment.</param>
+ /// <param name="attachmentData">The attachment data.</param>
+ /// <param name="mimeType">Type of the MIME.</param>
+ /// <returns>The document.</returns>
+ /// <remarks>This relies on the document to already have an id.</remarks>
+ public ICouchDocument WriteAttachment(ICouchDocument document, string attachmentName, Stream attachmentData, string mimeType)
+ {
+ if (document.Id == null)
+ {
+ throw CouchException.Create(
+ "Failed to add attachment to document using PUT because it lacks an id");
+ }
+
+ JObject result =
+ Request(document.Id + "/" + attachmentName).Query("?rev=" + document.Rev).Data(attachmentData).MimeType(mimeType).Put().Check("Failed to write attachment")
+ .Result();
+ document.Id = result["id"].Value<string>(); // Not really neeed
+ document.Rev = result["rev"].Value<string>();
+
+ return document;
+ }
+
/// <summary>
/// Read a ICouchDocument with an id even if it has not changed revision.
/// </summary>

0 comments on commit a83e8b6

Please sign in to comment.