public void ProcessRequest(HttpContext context) { _sessionState.AuthClient(); Channel chan; GetSession(context); if (_sessionState.CurrentUser == null) { chan = getChannel(context); if (chan != null) { _sessionState.CurrentUser = new User() { ChannelName = new List <string>() }; _sessionState.CurrentUser.ChannelName.Add(chan.Name); _sessionState.GetChanMessages(chan, 10); _restoreSession.LastChannel = new Channel(); _restoreSession.LastChannel.Messages = AppCache.MessageList.Where(m => m.ChannelId == chan.Id).Take(10).ToList(); } else { context.Response.Write("{ 'success': login to channel pls }"); return; } } else if (_restoreSession.LastChannel == null) { chan = getChannel(context); if (chan != null) { _sessionState.GetChanMessages(chan, 10); _restoreSession.LastChannel = new Channel(); _restoreSession.LastChannel.Messages = AppCache.MessageList.Where(m => m.ChannelId == chan.Id).Take(10).ToList(); } } chan = _restoreSession.LastChannel; if (chan != null) { // Set the response return data type context.Response.ContentType = "text/html"; try { // First check this header (cross browser support) string uploadFileName = context.Request.Headers["X-File-Name"]; if (string.IsNullOrEmpty(uploadFileName) == false || context.Request.Files.Count > 0) { // Get the uploads physical directory on the server string directory = context.Server.MapPath("~/UploadFiles/"); if (Functions.DirectoryExist(directory)) { //create new message Message msg = new Message(); msg.ChannelId = chan.Id; msg.CreatedDateTime = DateTime.Now.ToString(); msg.Type = MessageTypes.File; string filename = string.Empty; if (uploadFileName == null) { // get just the original filename filename = System.IO.Path.GetFileName(context.Request.Files[0].FileName); } else { filename = uploadFileName; } msg.Type = MessageTypes.File; // create full server path string file = string.Format("{0}\\{1}", directory, filename); // If file exists already, delete it (optional) //if (System.IO.File.Exists(file) == true) System.IO.File.Delete(file); if (string.IsNullOrEmpty(uploadFileName) == true) // IE Browsers { // Save file to server context.Request.Files[0].SaveAs(file); } else // Other Browsers { // Save file to server System.IO.FileStream fileStream = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate); context.Request.InputStream.CopyTo(fileStream); fileStream.Close(); } using (FileStream sr = new FileStream(file, FileMode.Open)) { uploadToCloud(filename, uploadFileName, sr, msg, chan, file); } //save in mongodb _sessionState.SaveMessage(msg); //send notification in pubnub if (msg.Type == MessageTypes.File) { _sessionState.AuthClient(); msg.Content = _sessionState.DropboxClient.GetMediaLinkAsync(msg.Content).Result.Url; } PublishToPubNub(msg); // return the json object as successful context.Response.Write("{ 'success': true }"); return; } } // return the json object as unsuccessful context.Response.Write("{ 'success': false }"); } catch (Exception) { // return the json object as unsuccessful context.Response.Write("{ 'success': false }"); } finally { SaveSession(context); } } }
public override HttpResponseMessage Post(Channel value) { try { GetSession(); var channel = new Channel(); if (_sessionState.CurrentUser != null && _sessionState.CurrentUser.ChannelName.Contains(value.Name)) { channel = _sessionState.GetChannel(value.Name); if (channel != null) { _sessionState.GetChanMessages(channel, 10); channel.Messages = AppCache.MessageList.Where(m => m.ChannelId == channel.Id).Take(10).ToList(); } } else { channel = _sessionState.GetChannel(value.Name, value.Password, _sessionState.Pubnub); if (channel != null) { if (_sessionState.CurrentUser == null) { _sessionState.CurrentUser = new User() { Id = Guid.NewGuid(), ChannelName = new List <string>() } } ; if (!_sessionState.CurrentUser.ChannelName.Contains(value.Name)) { _sessionState.CurrentUser.ChannelName.Add(value.Name); } _sessionState.GetChanMessages(channel, value.NumberOfMessages); channel.Messages = AppCache.MessageList.Where(m => m.ChannelId == channel.Id) .OrderBy(m => m.CreatedDateTime) .Take(value.NumberOfMessages) .ToList(); if (_restoreSession.LastChannel == null) { _restoreSession.LastChannel = new Channel(); } _restoreSession.LastChannel = channel; } else { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "If create a new channel: channel exist." + Environment.NewLine + "If login to a channel: password not match."); throw new HttpResponseException(errResponse); } } var response = Request.CreateResponse(HttpStatusCode.Created, channel); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = value.Id })); return(response); } catch (Exception ex) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errResponse); } finally { SaveSession(); } }
private void RestoreChannel(Channel channel) { _sessionState.GetChanMessages(channel, 10); _restoreSession.LastChannel = new Channel(); _restoreSession.LastChannel.Messages = AppCache.MessageList.Where(m => m.ChannelId == channel.Id).Take(10).ToList(); }