public void TestMerge_NestedComplex() { BsonDocument a = new BsonDocument { { "a", 1 }, { "b", new BsonDocument { { "a", 10 }, { "b", 20 } } }, }; BsonDocument b = new BsonDocument { { "b", new BsonDocument { { "aa", 10 }, { "bb", 20 } } }, }; BsonDocument res = a.DeepMerge(b); Assert.IsTrue(res["a"] == 1); Assert.IsTrue(res["b"].Equals(new BsonDocument { { "a", 10 }, { "b", 20 }, { "aa", 10 }, { "bb", 20 }, }) ); Assert.AreEqual(2, res.ElementCount); }
public void Execute(Attachment file, ImageMeta meta, NameValueCollection Parameters, Driver driver, string savedName, AttachmentUploadHandler handler) { file.file_size = file.RawData.Count; file.modify_time = DateTime.UtcNow; file.url = "/v2/attachments/view/?object_id=" + file.object_id; file.saved_file_name = savedName; file.Upload(meta, Parameters["apikey"], Parameters["session_token"]); new FileStorage(driver).SaveAttachment(file); BsonDocument dbDoc = CreateDbDocument(file, meta, savedName); BsonDocument existDoc = AttachmentCollection.Instance.FindOneAs <BsonDocument>( new QueryDocument("_id", file.object_id)); if (existDoc != null) { existDoc.DeepMerge(dbDoc); AttachmentCollection.Instance.Save(existDoc); } else { AttachmentCollection.Instance.Save(dbDoc); } AttachmentEventArgs aEvtArgs = new AttachmentEventArgs { Attachment = file, Driver = driver }; handler.OnAttachmentSaved(aEvtArgs); HttpHelper.RespondSuccess(handler.Response, ObjectUploadResponse.CreateSuccess(file.object_id)); }
private void SaveAttachmentInfoToDB(Attachment file, ImageMeta meta, string savedName) { BsonDocument dbDoc = CreateDbDocument(file, meta, savedName); BsonDocument existDoc = AttachmentCollection.Instance.FindOneAs <BsonDocument>( new QueryDocument("_id", file.object_id)); if (existDoc != null) { existDoc.DeepMerge(dbDoc); AttachmentCollection.Instance.Save(existDoc); } else { AttachmentCollection.Instance.Save(dbDoc); } }
public void testMixedMerge() { Wammer.Model.Attachment a = new Wammer.Model.Attachment { type = Wammer.Model.AttachmentType.image, object_id = "1234567890", image_meta = new Wammer.Model.ImageProperty { small = new Wammer.Model.ThumbnailInfo { url = "http://localhost/" } } }; Wammer.Model.Attachment b = new Wammer.Model.Attachment { type = Wammer.Model.AttachmentType.image, title = "this is title", description = "this is description" }; BsonDocument doc = b.ToBsonDocument(); BsonDocument update = a.ToBsonDocument(); doc.DeepMerge(update); string bb = update.ToString(); Assert.AreEqual("this is title", doc["title"].AsString); Assert.AreEqual("this is description", doc["description"].AsString); Assert.AreEqual("http://localhost/", doc["image_meta"] .AsBsonDocument["small"].AsBsonDocument["url"].AsString); Assert.AreEqual("1234567890", doc["_id"].AsString); }
public void HandleImageAttachmentSaved(object sender, ImageAttachmentEventArgs evt) { System.Diagnostics.Debug.Assert(evt.Attachment.type == AttachmentType.image); if (evt.Meta != ImageMeta.Origin) { return; } if (evt.Attachment.image_meta != null && evt.Attachment.image_meta.medium != null) { return; } try { ThumbnailInfo medium; Attachment update; using (Bitmap origImage = BuildBitmap(evt.Attachment.RawData)) { // release raw data immediately evt.Attachment.RawData = new ArraySegment <byte>(); medium = MakeThumbnail(origImage, ImageMeta.Medium, evt.Attachment.Orientation, evt.Attachment.object_id, evt.Driver, evt.Attachment.file_name); update = new Attachment { object_id = evt.Attachment.object_id, image_meta = new ImageProperty { width = origImage.Width, height = origImage.Height, medium = medium } }; } BsonDocument exist = AttachmentCollection.Instance.FindOneAs <BsonDocument>( Query.EQ("_id", evt.Attachment.object_id)); exist.DeepMerge(update.ToBsonDocument()); AttachmentCollection.Instance.Save(exist); TaskQueue.EnqueueMedium(this.UpstreamThumbnail, new UpstreamArgs { FullImageId = evt.Attachment.object_id, GroupId = evt.Attachment.group_id, ImageMeta = ImageMeta.Medium, Thumbnail = medium, UserApiKey = evt.UserApiKey, UserSessionToken = evt.UserSessionToken }); } catch (Exception e) { logger.Warn("Unabel to make thumbnail and upstream", e); } }
public void HandleImageAttachmentCompletedSync(object args) { ImageAttachmentEventArgs evt = (ImageAttachmentEventArgs)args; if (evt.Attachment.type != AttachmentType.image || evt.Meta != ImageMeta.Origin) { return; } try { ThumbnailInfo small; ThumbnailInfo large; ThumbnailInfo square; string origImgObjectId = evt.Attachment.object_id; using (Bitmap origImage = BuildBitmap(evt.Attachment.RawData)) { // release raw data immediately evt.Attachment.RawData = new ArraySegment <byte>(); small = MakeThumbnail(origImage, ImageMeta.Small, evt.Attachment.Orientation, origImgObjectId, evt.Driver, evt.Attachment.file_name); large = MakeThumbnail(origImage, ImageMeta.Large, evt.Attachment.Orientation, origImgObjectId, evt.Driver, evt.Attachment.file_name); square = MakeThumbnail(origImage, ImageMeta.Square, evt.Attachment.Orientation, origImgObjectId, evt.Driver, evt.Attachment.file_name); } Attachment update = new Attachment { object_id = evt.Attachment.object_id, image_meta = new ImageProperty { small = small, large = large, square = square } }; BsonDocument doc = AttachmentCollection.Instance.FindOneAs <BsonDocument>( Query.EQ("_id", origImgObjectId)); doc.DeepMerge(update.ToBsonDocument()); AttachmentCollection.Instance.Save(doc); doc.Clear(); doc = null; UpstreamThumbnail(small, evt.Attachment.group_id, evt.Attachment.object_id, ImageMeta.Small, evt.UserApiKey, evt.UserSessionToken); UpstreamThumbnail(large, evt.Attachment.group_id, evt.Attachment.object_id, ImageMeta.Large, evt.UserApiKey, evt.UserSessionToken); UpstreamThumbnail(square, evt.Attachment.group_id, evt.Attachment.object_id, ImageMeta.Square, evt.UserApiKey, evt.UserSessionToken); long newValue = Interlocked.Add(ref g_counter, 1L); if (newValue % 5 == 0) { GC.Collect(); } } catch (Exception e) { logger.Warn("Image attachment post processing unsuccess", e); } }