/// <summary> /// Merges the attachments from another revision. /// </summary> /// <param name="revision">The revision.</param> /// <param name="sourceRevision">The revision with the attachments to be merged.</param> /// <param name="keepMine">Controls what happens when the source and target have the same attachment (see remarks).</param> /// <remarks> /// <para> /// The <paramref name="keepMine"/> parameter determines what happens when the source and target /// revisions has an attachment with the same name. When <paramref name="keepMine"/>=<c>true</c> /// (the default), then matching attachments from the source revision will be ignored. /// </para> /// <para> /// When <paramref name="keepMine"/>=<c>false</c>, matching attachments from the source /// revision will be copied to the current revision, overwriting the existion attachment. /// </para> /// </remarks> public static void MergeAttachmentsFrom(this UnsavedRevision revision, Revision sourceRevision, bool keepMine = true) { Covenant.Requires <ArgumentNullException>(revision != null); Covenant.Requires <ArgumentNullException>(sourceRevision != null); // Merge the source attachments. foreach (var sourceAttachment in sourceRevision.Attachments) { var existingAttachment = revision.GetAttachment(sourceAttachment.Name); if (existingAttachment != null && keepMine) { continue; } // $todo(jeff.lill): // // I'm a little nervous about this call. [Attachment.Content] is going to // load the attachment into memory, which could be a significant overhead. // It's possible to pass a stream, but looking at the Couchbase Lite source, // it appears that the new attachment would try to take ownership of the // source attachment's stream (if I passed it), probably ending badly. revision.SetAttachment(sourceAttachment.Name, sourceAttachment.ContentType, sourceAttachment.Content); } }
/// <summary>Regression test for https://github.com/couchbase/couchbase-lite-android-core/issues/70 /// </summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> /// <exception cref="System.IO.IOException"></exception> public virtual void TestAttachmentDisappearsAfterSave() { // create a doc with an attachment Document doc = database.CreateDocument(); string content = "This is a test attachment!"; ByteArrayInputStream body = new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString (content)); UnsavedRevision rev = doc.CreateRevision(); rev.SetAttachment("index.html", "text/plain; charset=utf-8", body); rev.Save(); // make sure the doc's latest revision has the attachment IDictionary <string, object> attachments = (IDictionary)doc.GetCurrentRevision().GetProperty ("_attachments"); NUnit.Framework.Assert.IsNotNull(attachments); NUnit.Framework.Assert.AreEqual(1, attachments.Count); // make sure the rev has the attachment attachments = (IDictionary)rev.GetProperty("_attachments"); NUnit.Framework.Assert.IsNotNull(attachments); NUnit.Framework.Assert.AreEqual(1, attachments.Count); // create new properties to add IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("foo", "bar"); // make sure the new rev still has the attachment UnsavedRevision rev2 = doc.CreateRevision(); rev2.GetProperties().PutAll(properties); rev2.Save(); attachments = (IDictionary)rev2.GetProperty("_attachments"); NUnit.Framework.Assert.IsNotNull(attachments); NUnit.Framework.Assert.AreEqual(1, attachments.Count); }
/// <summary> /// Replaces the current revision's attachments with copies of the /// attachments from another revision. /// </summary> /// <param name="revision">The revision.</param> /// <param name="sourceRevision">The revision with the attachments to be copied.</param> public static void ReplaceAttachmentsFrom(this UnsavedRevision revision, Revision sourceRevision) { Covenant.Requires <ArgumentNullException>(revision != null); Covenant.Requires <ArgumentNullException>(sourceRevision != null); // Remove the current attachments. foreach (var name in revision.AttachmentNames.ToArray()) { revision.RemoveAttachment(name); } // Copy the source attachments. foreach (var sourceAttachment in sourceRevision.Attachments) { // $todo(jeff.lill): // // I'm a little nervous about this call. [Attachment.Content] is going to // load the attachment into memory, which could be a significant overhead. // It's possible to pass a stream, but looking at the Couchbase Lite source, // it appears that the new attachment would try to take ownership of the // source attachment's stream (if I passed it), probably ending badly. revision.SetAttachment(sourceAttachment.Name, sourceAttachment.ContentType, sourceAttachment.Content); } }
public static void AddImageAttachment(UnsavedRevision rev, string name, Image img) { if (img == null) { if (rev.AttachmentNames.Contains (name)) { rev.RemoveAttachment (name); } } else { rev.SetAttachment (name, "image/png", img.Serialize()); } }
/// <exception cref="System.Exception"></exception> public static Document CreateDocWithAttachment(Database database, string attachmentName , string content) { IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("foo", "bar"); Document doc = CreateDocumentWithProperties(database, properties); SavedRevision rev = doc.GetCurrentRevision(); NUnit.Framework.Assert.AreEqual(rev.GetAttachments().Count, 0); NUnit.Framework.Assert.AreEqual(rev.GetAttachmentNames().Count, 0); NUnit.Framework.Assert.IsNull(rev.GetAttachment(attachmentName)); ByteArrayInputStream body = new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString (content)); UnsavedRevision rev2 = doc.CreateRevision(); rev2.SetAttachment(attachmentName, "text/plain; charset=utf-8", body); SavedRevision rev3 = rev2.Save(); NUnit.Framework.Assert.IsNotNull(rev3); NUnit.Framework.Assert.AreEqual(rev3.GetAttachments().Count, 1); NUnit.Framework.Assert.AreEqual(rev3.GetAttachmentNames().Count, 1); Attachment attach = rev3.GetAttachment(attachmentName); NUnit.Framework.Assert.IsNotNull(attach); NUnit.Framework.Assert.AreEqual(doc, attach.GetDocument()); NUnit.Framework.Assert.AreEqual(attachmentName, attach.GetName()); IList <string> attNames = new AList <string>(); attNames.AddItem(attachmentName); NUnit.Framework.Assert.AreEqual(rev3.GetAttachmentNames(), attNames); NUnit.Framework.Assert.AreEqual("text/plain; charset=utf-8", attach.GetContentType ()); NUnit.Framework.Assert.AreEqual(IOUtils.ToString(attach.GetContent(), "UTF-8"), content ); NUnit.Framework.Assert.AreEqual(Sharpen.Runtime.GetBytesForString(content).Length , attach.GetLength()); return(doc); }