Browse files

Merge branch 'sebastian'

  • Loading branch information...
2 parents dfd86ac + 8329f9f commit 2bbe608f35c771551b7a429ddd6055902fff61fb @gokr gokr committed Dec 1, 2009
Showing with 59 additions and 13 deletions.
  1. +28 −1 src/CouchDatabase.cs
  2. +31 −12 src/CouchRequest.cs
View
29 src/CouchDatabase.cs
@@ -6,6 +6,7 @@
using System;
using Divan.Lucene;
using Newtonsoft.Json.Linq;
+using System.IO;
namespace Divan
{
@@ -318,6 +319,32 @@ public ICouchDocument WriteAttachment(ICouchDocument document, string attachment
}
/// <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>
/// <param name="document">Document to fill.</param>
@@ -857,7 +884,7 @@ public void Copy(string sourceDocumentId, string destinationDocumentId, string d
/// <param name="sourceDocument">The source document.</param>
/// <param name="destinationDocument">The destination document.</param>
/// <remarks>This method does not update the destinationDocument object.</remarks>
- public void Copy(CouchDocument sourceDocument, CouchDocument destinationDocument)
+ public void Copy(ICouchDocument sourceDocument, ICouchDocument destinationDocument)
{
Copy(sourceDocument.Id, destinationDocument.Id, destinationDocument.Rev);
}
View
43 src/CouchRequest.cs
@@ -16,7 +16,9 @@ namespace Divan
/// </summary>
public class CouchRequest
{
+ private const int UploadBufferSize = 100000;
private readonly CouchDatabase db;
+ private Stream postStream;
private readonly CouchServer server;
private string etag, etagToCheck;
public Dictionary<string, string> headers = new Dictionary<string, string>();
@@ -25,7 +27,7 @@ public class CouchRequest
public string method = "GET"; // PUT, DELETE, POST, HEAD
public string mimeType;
public string path;
- public byte[] postData;
+ //public byte[] postData;
public string query;
public JToken result;
@@ -132,13 +134,22 @@ public CouchRequest Delete()
public CouchRequest Data(string data)
{
- postData = Encoding.UTF8.GetBytes(data);
- return this;
+ var postData = Encoding.UTF8.GetBytes(data);
+ return Data(postData);
+ //this.postStream = new MemoryStream(postData);
+ //return this;
}
public CouchRequest Data(byte[] data)
{
- postData = data;
+ this.postStream = new MemoryStream(data);
+ //postData = data;
+ return this;
+ }
+
+ public CouchRequest Data(Stream dataStream)
+ {
+ this.postStream = dataStream;
return this;
}
@@ -210,21 +221,29 @@ private HttpWebRequest GetRequest()
request.Headers.Add(header.Key, header.Value);
}
- if (postData != null)
+ if (postStream != null)
{
- //byte[] bytes = Encoding.UTF8.GetBytes(postData);
- request.ContentLength = postData.Length;
- using (Stream ps = request.GetRequestStream())
- {
- ps.Write(postData, 0, postData.Length);
- ps.Close();
- }
+ WriteData(request);
}
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Request: {0} Method: {1}", requestUri, method));
return request;
}
+ private void WriteData(HttpWebRequest request)
+ {
+ request.ContentLength = postStream.Length;
+ using (Stream ps = request.GetRequestStream())
+ {
+ var buffer = new byte[UploadBufferSize];
+ int bytesRead;
+ while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
+ {
+ ps.Write(buffer, 0, bytesRead);
+ }
+ }
+ }
+
public JObject Parse()
{
return Parse<JObject>();

0 comments on commit 2bbe608

Please sign in to comment.