示例#1
0
        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);
                }
            }
        }