public void TestObjectUploadHandler_ResponseCompleted() { using (HttpServer cloud = new HttpServer(8080)) using (HttpServer server = new HttpServer(80)) { List <UserGroup> groups = new List <UserGroup>(); groups.Add(new UserGroup { creator_id = "id1", group_id = "gid1", name = "group1", description = "none" }); FileStorage fileStore = new FileStorage(new Driver { email = "*****@*****.**", folder = @"resource\group1", groups = groups, session_token = "session_token1", user_id = "id1" }); AttachmentUploadHandler handler = new AttachmentUploadHandler(); DummyRequestCompletedHandler evtHandler = new DummyRequestCompletedHandler(); handler.ImageAttachmentCompleted += evtHandler.Handle; server.AddHandler("/test/", handler); server.Start(); cloud.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/upload/", new DummyImageUploadHandler()); cloud.Start(); ObjectUploadResponse res = Wammer.Model.Attachment.UploadImage( "http://localhost:80/test/", new ArraySegment <byte>(imageRawData), "group1", null, "orig_name.jpeg", "image/jpeg", ImageMeta.Origin, "apiKey1", "token1"); Assert.IsTrue(evtHandler.EventReceived()); } }
public void TestStationRecv_NewThumbnailImage() { using (HttpServer cloud = new HttpServer(8080)) using (HttpServer server = new HttpServer(80)) { List <UserGroup> groups = new List <UserGroup>(); groups.Add(new UserGroup { creator_id = "id1", group_id = "gid1", name = "group1", description = "none" }); FileStorage fileStore = new FileStorage(new Driver { email = "*****@*****.**", folder = @"resource\group1", groups = groups, session_token = "session_token1", user_id = "id1" }); AttachmentUploadHandler handler = new AttachmentUploadHandler(); server.AddHandler("/test/", handler); server.Start(); cloud.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/upload/", new DummyImageUploadHandler()); cloud.Start(); string oid = Guid.NewGuid().ToString(); ObjectUploadResponse res = Wammer.Model.Attachment.UploadImage( "http://localhost:80/test/", new ArraySegment <byte>(imageRawData), "group1", oid, "orig_name2.jpeg", "image/jpeg", ImageMeta.Large, "apikey1", "token1"); // verify Assert.AreEqual(oid, res.object_id); using (FileStream f = fileStore.Load(oid + "_large.jpeg")) { Assert.AreEqual((long)imageRawData.Length, f.Length); for (int i = 0; i < imageRawData.Length; i++) { Assert.AreEqual((int)imageRawData[i], f.ReadByte()); } } BsonDocument saveData = mongodb.GetDatabase("wammer"). GetCollection("attachments").FindOne( new QueryDocument("_id", oid)); Assert.IsNotNull(saveData); Assert.IsFalse(saveData.Contains("title")); Assert.IsFalse(saveData.Contains("description")); Assert.IsFalse(saveData.Contains("mime_type")); Assert.IsFalse(saveData.Contains("url")); Assert.IsFalse(saveData.Contains("file_size")); Assert.AreEqual((int)AttachmentType.image, saveData["type"].AsInt32); BsonDocument meta = saveData["image_meta"].AsBsonDocument; Assert.AreEqual( string.Format("/v2/attachments/view/?object_id={0}&image_meta=large", res.object_id), meta["large"].AsBsonDocument["url"].AsString); Assert.AreEqual(res.object_id + "_large.jpeg", meta["large"].AsBsonDocument["file_name"]); Assert.AreEqual(imageRawData.Length, meta["large"].AsBsonDocument["file_size"].ToInt32()); Assert.AreEqual("image/jpeg", meta["large"].AsBsonDocument["mime_type"].AsString); } }
public void TestStationRecv_OldOriginalImage() { using (HttpServer cloud = new HttpServer(8080)) using (HttpServer server = new HttpServer(80)) { List <UserGroup> groups = new List <UserGroup>(); groups.Add(new UserGroup { creator_id = "id1", group_id = "gid1", name = "group1", description = "none" }); FileStorage fileStore = new FileStorage(new Driver { email = "*****@*****.**", folder = @"resource\group1", groups = groups, session_token = "session_token1", user_id = "id1" }); AttachmentUploadHandler handler = new AttachmentUploadHandler(); server.AddHandler("/test/", handler); server.Start(); cloud.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/upload/", new DummyImageUploadHandler()); cloud.Start(); ObjectUploadResponse res = Wammer.Model.Attachment.UploadImage( "http://localhost:80/test/", new ArraySegment <byte>(imageRawData), "group1", object_id1, "orig_name2.png", "image/png", ImageMeta.Origin, "apikey1", "token1"); // verify saved file using (FileStream f = fileStore.Load(object_id1 + ".png")) { byte[] imageData = new byte[f.Length]; Assert.AreEqual(imageData.Length, f.Read(imageData, 0, imageData.Length)); for (int i = 0; i < f.Length; i++) { Assert.AreEqual(imageData[i], imageRawData[i]); } } // verify db MongoCursor <Attachment> cursor = mongodb.GetDatabase("wammer").GetCollection <Attachment>("attachments") .Find(new QueryDocument("_id", object_id1)); Assert.AreEqual <long>(1, cursor.Count()); foreach (Attachment doc in cursor) { Assert.AreEqual(object_id1, doc.object_id); Assert.AreEqual("orig_desc", doc.description); Assert.AreEqual("orig_title", doc.title); Assert.AreEqual(AttachmentType.image, doc.type); Assert.AreEqual(imageRawData.Length, doc.file_size); Assert.AreEqual("image/png", doc.mime_type); } } }
protected override void OnStart(string[] args) { try { logger.Info("============== Starting Waveface Station ================="); ConfigThreadPool(); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Environment.CurrentDirectory = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location); logger.Debug("Initialize Waveface Service"); InitStationId(); InitResourceBasePath(); fastJSON.JSON.Instance.UseUTCDateTime = true; functionServer = new HttpServer(9981); // TODO: remove hard code stationTimer = new StationTimer(functionServer); logger.Debug("Add cloud forwarders to function server"); BypassHttpHandler cloudForwarder = new BypassHttpHandler(CloudServer.BaseUrl); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/auth/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/users/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/groups/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/stations/"); functionServer.AddDefaultHandler(cloudForwarder); logger.Debug("Add handlers to function server"); functionServer.AddHandler("/", new DummyHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/view/", new AttachmentViewHandler(stationId)); AttachmentUploadHandler attachmentHandler = new AttachmentUploadHandler(); AttachmentUploadMonitor attachmentMonitor = new AttachmentUploadMonitor(); ImagePostProcessing imgProc = new ImagePostProcessing(); imgProc.ThumbnailUpstreamed += attachmentMonitor.OnThumbnailUpstreamed; attachmentHandler.ImageAttachmentSaved += imgProc.HandleImageAttachmentSaved; attachmentHandler.ImageAttachmentCompleted += imgProc.HandleImageAttachmentCompleted; attachmentHandler.ThumbnailUpstreamed += attachmentMonitor.OnThumbnailUpstreamed; CloudStorageSync cloudSync = new CloudStorageSync(); attachmentHandler.AttachmentSaved += cloudSync.HandleAttachmentSaved; attachmentHandler.ProcessSucceeded += attachmentMonitor.OnProcessSucceeded; functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/upload/", attachmentHandler); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/resourceDir/get/", new ResouceDirGetHandler(resourceBasePath)); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/resourceDir/set/", new ResouceDirSetHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/get/", new AttachmentGetHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/availability/ping/", new PingHandler()); //if (Wammer.Utility.AutoRun.Exists("WavefaceStation")) //{ logger.Debug("Start function server"); functionServer.Start(); stationTimer.Start(); //} logger.Debug("Add handlers to management server"); managementServer = new HttpServer(9989); AddDriverHandler addDriverHandler = new AddDriverHandler(stationId, resourceBasePath); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/online/", new StationOnlineHandler(functionServer, stationTimer)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/offline/", new StationOfflineHandler(functionServer, stationTimer)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/add/", addDriverHandler); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/list/", new ListDriverHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/remove/", new RemoveOwnerHandler(stationId)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/status/get/", new StatusGetHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/list", new ListCloudStorageHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/oauth/", new DropBoxOAuthHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/connect/", new DropBoxConnectHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/update/", new DropBoxUpdateHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/disconnect/", new DropboxDisconnectHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/availability/ping/", new PingHandler()); addDriverHandler.DriverAdded += new EventHandler <DriverAddedEvtArgs>(addDriverHandler_DriverAdded); logger.Debug("Start management server"); managementServer.Start(); logger.Info("Waveface station is started"); } catch (Exception ex) { logger.Error("Unknown exception", ex); throw; } }