Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 03, 2012
@deadok22 deadok22 added AddAttachment() method that takes Stream e1950fb
@soitgoes Merge pull request #30 from deadok22/master
Added method AddAttachment() that takes Stream as an argument
a2ab29f
Showing with 41 additions and 0 deletions.
  1. +26 −0 LoveSeat/CouchDatabase.cs
  2. +15 −0 LoveSeat/Support/CouchRequest.cs
View
26 LoveSeat/CouchDatabase.cs
@@ -167,6 +167,7 @@ public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing)
/// </summary>
/// <param name="id">id of the couch Document</param>
/// <param name="attachment">byte[] of of the attachment. Use File.ReadAllBytes()</param>
+ /// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
public CouchResponse AddAttachment(string id, byte[] attachment, string filename, string contentType)
{
@@ -187,6 +188,31 @@ public CouchResponse AddAttachment(string id, string rev, byte[] attachment, str
return
GetRequest(databaseBaseUri + "/" + id + "/" + filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachment).GetResponse().GetJObject();
}
+ /// <summary>
+ /// Adds an attachment to a document. If revision is not specified then the most recent will be fetched and used. Warning: if you need document update conflicts to occur please use the method that specifies the revision
+ /// </summary>
+ /// <param name="id">id of the couch Document</param>
+ /// <param name="attachmentStream">Stream of the attachment.</param>
+ /// <param name="contentType">Content Type must be specifed</param>
+ public CouchResponse AddAttachment(string id, Stream attachmentStream, string filename, string contentType)
+ {
+ var doc = GetDocument(id);
+ return AddAttachment(id, doc.Rev, attachmentStream, filename, contentType);
+ }
+ /// <summary>
+ /// Adds an attachment to the documnet. Rev must be specified on this signature. If you want to attach no matter what then use the method without the rev param
+ /// </summary>
+ /// <param name="id">id of the couch Document</param>
+ /// <param name="rev">revision _rev of the Couch Document</param>
+ /// <param name="attachmentStream">Stream of of the attachment. Use File.ReadAllBytes()</param>
+ /// <param name="filename">filename of the attachment</param>
+ /// <param name="contentType">Content Type must be specifed</param>
+ /// <returns></returns>
+ public CouchResponse AddAttachment(string id, string rev, Stream attachmentStream, string filename, string contentType)
+ {
+ return
+ GetRequest(databaseBaseUri + "/" + id + "/" + filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachmentStream).GetResponse().GetJObject();
+ }
public Stream GetAttachmentStream(Document doc, string attachmentName)
{
View
15 LoveSeat/Support/CouchRequest.cs
@@ -1,3 +1,4 @@
+using System.IO;
using System.Net;
using System.Text;
using System.Web;
@@ -7,6 +8,7 @@ namespace LoveSeat.Support {
public class CouchRequest {
private const string INVALID_USERNAME_OR_PASSWORD = "reason=Name or password is incorrect";
private const string NOT_AUTHORIZED = "reason=You are not authorized to access this db.";
+ private const int STREAM_BUFFER_SIZE = 4096;
private readonly HttpWebRequest request;
public CouchRequest(string uri)
@@ -82,6 +84,19 @@ public CouchRequest(string uri)
request.Method = "DELETE";
return this;
}
+ public CouchRequest Data(Stream data)
+ {
+ using (var body = request.GetRequestStream())
+ {
+ var buffer = new byte[STREAM_BUFFER_SIZE];
+ var bytesRead = 0;
+ while (0 != (bytesRead = data.Read(buffer, 0, buffer.Length)))
+ {
+ body.Write(buffer, 0, bytesRead);
+ }
+ }
+ return this;
+ }
public CouchRequest Data(string data) {
using (var body = request.GetRequestStream()) {
var encodedData = Encoding.UTF8.GetBytes(data);

No commit comments for this range

Something went wrong with that request. Please try again.