예제 #1
0
 public static SafeModeResult DeleteMessage(this boSessionState _sessionState, Message value)
 {
     var result = _sessionState.db.GetCollection<Message>("Message").Remove(Query.EQ("_id", value.Id));
     if (result.Ok)
         AppCache.MessageList.Remove(value);
     return result;
 }
예제 #2
0
 public static Message CreatePublicMessage(Message value)
 {
     //PublicMessage msg = new PublicMessage();
     //msg.Id = value.Id;
     //msg.Type = value.Type;
     //msg.Values = AppCache.AESProvider.DecryptString(value.Value);
     //msg.CreatedDateTime = value.CreatedDateTime;
     return value;
 }
예제 #3
0
 public static void AddSafeName(this List<Message> l, Message value)
 {
     if (l.Find(c => c.Id == value.Id) == null)
         l.Add(Functions.CreatePublicMessage(value));
 }
예제 #4
0
 public static SafeModeResult SaveMessage(this boSessionState _sessionState, Message value)
 {
     var result = _sessionState.db.GetCollection<Message>("Message").Save(value, SafeMode.True);
     if (result.Ok)
         AppCache.MessageList.AddSafeName(Functions.CreatePublicMessage(value));
     return result;
 }
예제 #5
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 List<dynamic>();
                    _restoreSession.LastChannel.Add(chan);
                    _restoreSession.LastChannel.Add(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 List<dynamic>();
                    _restoreSession.LastChannel.Add(chan);
                    _restoreSession.LastChannel.Add(AppCache.MessageList.Where(m => m.ChannelId == chan.Id).Take(10).ToList());
                }
            }

            chan = _restoreSession.LastChannel[0];
            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.PrivateDateTime = DateTime.Now;
                            msg.PublicData = new MessageResource() { 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.PublicData.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);
                                uploadToCloud(filename, uploadFileName, context, msg, chan, file);
                            }
                            else // Other Browsers
                            {
                                // Save file to server
                                System.IO.FileStream fileStream = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate);
                                uploadToCloud(filename, uploadFileName, context, msg, chan, file);
                                context.Request.InputStream.CopyTo(fileStream);
                                fileStream.Close();
                            }

                            //save in mongodb
                            msg.PrivateData = AppCache.AESProvider.EncryptToString(JsonConvert.SerializeObject(msg.PublicData));
                            _sessionState.SaveMessage(msg);

                            //send notification in pubnub

                            if (msg.PublicData.Type == MessageTypes.File)
                            {
                                _sessionState.AuthClient();
                                msg.PublicData.Value = _sessionState.Client.GetMediaLinkAsync(msg.PublicData.Value).Result.Url;
                            }

                            PublishToPubNub(chan.Name, 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);
                }
            }
        }
예제 #6
0
        private void uploadToCloud(string filename, string uploadFileName, HttpContext context, Message msg, Channel chan, string file)
        {
            if (filename.EndsWith("png") || filename.EndsWith("jpeg") || filename.EndsWith("jpg") || filename.EndsWith("gif") || filename.EndsWith("bmp"))
            {
                try
                {
                    msg.PublicData.Type = MessageTypes.Image;
                    var configuration = new AccountConfiguration("saykor", "277334748579534", "mUjzZ-X3jOuNKGswrAjocB-D-Rc");

                    var uploader = new Uploader(configuration);
                    string publicId = Path.GetFileNameWithoutExtension(filename);
                    Stream stream;
                    if (string.IsNullOrEmpty(uploadFileName) == true) // IE Browsers
                        stream = context.Request.Files[0].InputStream;
                    else // Other Browsers
                        stream = context.Request.InputStream;

                    var uploadResult = uploader.Upload(new UploadInformation(filename, stream)
                    {
                        PublicId = publicId,
                        Format = filename.Substring(filename.Length - 3),
                    });
                    msg.PublicData.Value = filename;
                }
                catch (Exception ex)
                {
                    context.Response.Write("{ 'success': " + ex.Message + " }");
                    return;
                }
            }
            else
            {
                //upload to dropbox
                string cloudPath = "/" + chan.Name + "/" + filename;
                _sessionState.AuthClient();
                var result = _sessionState.Client.UploadFileAsync(new FileResource(file), cloudPath).Result;
                msg.PublicData.Value = cloudPath;
            }
        }
예제 #7
0
 private void PublishToPubNub(string name, Message msg)
 {
     if (msg != null)
     {
         List<dynamic> pubnubMessage = new List<dynamic>();
         Dictionary<string, string> actionDict = new Dictionary<string, string>();
         actionDict.Add("Action", "add");
         actionDict.Add("Channel", name);
         pubnubMessage.Add(actionDict);
         msg.PrivateData = null;
         if (msg.PublicData.Type == MessageTypes.File)
         {
             _sessionState.AuthClient();
             msg.PublicData.Value = _sessionState.Client.GetMediaLinkAsync(msg.PublicData.Value).Result.Url;
         }
         pubnubMessage.Add(msg);
         List<object> publishResult = pubnub.Publish("NewMsgIn" + name, pubnubMessage);
     }
 }
예제 #8
0
 private Message AddMessage(Channel chan, string message, MessageTypes type)
 {
     Message msg = new Message();
     msg.ChannelId = chan.Id;
     msg.PrivateDateTime = DateTime.Now;
     msg.CreatedDateTime = msg.PrivateDateTime.ToString();
     msg.PublicData = new MessageResource() { Type = type, Value = message };
     msg.PrivateData = AppCache.AESProvider.EncryptToString(JsonConvert.SerializeObject(msg.PublicData));
     _sessionState.SaveMessage(msg);
     AppCache.MessageList.AddSafeName(msg);
     SaveSession();
     return msg;
 }