private void SendPicture() { if (Uploading) { MessageBox.Show("Please wait until the uploading is complete!"); return; } if (CurrentUser == null || RecipientUser == null) { return; } string message = ChatMessage.Text.Trim(); if (message == PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName) { message = ""; } SocketMessage msg = new SocketMessage( new object[] { "ConversationID", ConversationID }, new object[] { "SenderID", CurrentUser.ID }, new object[] { "RecipientID", RecipientUser.ID }, new object[] { "Sender", CurrentUser.UserName }, new object[] { "Message", message }, new object[] { "TimeStamp", DateTime.UtcNow.ToString("MM-dd-yyyy HH:mm:ss") }, new object[] { "DataType", ChatType }, new object[] { "Links", pictureURLs } ); ClientSocket.Send("message", msg); ChatMessage.Text = PreDefinedMessage["TypeRecipient"] + RecipientUser.UserName; ChatMessage.SelectionLength = ChatMessage.SelectionStart = 0; ChatMessage.ForeColor = Color.FromArgb(150, 150, 150); ChatType = Reply.ContentType.String; pictureURLs = null; SendIcon.Image = Properties.Resources.send_disabled; foreach (var upPic in UploadContainer.Controls.OfType <UploadPictureBox>()) { upPic?.Dispose(); } UploadHiddenItems.Text = ""; UploadProgressPercent.Text = "0%"; UploadContainer.Hide(); ClientSocket.Send("typing", new SocketMessage( new object[] { "typing", false }, new object[] { "RecipientID", RecipientUser.ID } )); }
private void InnerChatContainer_DragDrop(object sender, DragEventArgs e) { UploadDropHere.Hide(); string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; if (files == null) { return; } if (files.Length <= 0) { return; } string[] serverNames = new string[files.Length]; foreach (var upPic in UploadContainer.Controls.OfType <UploadPictureBox>()) { upPic?.Dispose(); } UploadTotalProgressBar.Size = new Size(0, UploadTotalProgressBar.Height); ChatType = Reply.ContentType.Picture; Uploading = true; uploadTotalSize = uploadProgressSize = 0; UploadContainer.Show(); int x, y; int h = UploadContainer.Height - 10; int cc = 1; x = y = 10; if (files.Length > 5) { UploadHiddenItems.Text = "+" + (files.Length - 5).ToString(); } UploadHiddenItems.Visible = files.Length > 5; foreach (string file in files) { uploadTotalSize += new FileInfo(file).Length; if (cc > 5) { continue; } UploadPictureBox upPicture = new UploadPictureBox(file, new Size(h, h)); upPicture.Location = new Point(x, y); UploadContainer.Controls.Add(upPicture); x += upPicture.Width + 5; cc++; } Task.Run(async() => { UploadPictureBox[] upBoxes = UploadContainer.Controls.OfType <UploadPictureBox>().ToArray(); for (int c = 0; c < upBoxes.Length; c++) { await upBoxes[c].LoadImage(upBoxes[c].Name); } for (int c = 0; c < upBoxes.Length; c++) { string dest = REMOTE_IMAGE_LOCATION + Guid.NewGuid() + Path.GetExtension(upBoxes[c].Name); serverNames[c] = dest; await upBoxes[c].Upload(upBoxes[c].Name, dest, new NetworkCredential("2159860_user", "Qub1cR3dSt0rag3"), UploadProgress); } if (files.Length > 5) // Upload all the files that aren't actually shown in the preview bar { for (int i = 5; i < files.Length; i++) { const int CHUNK_SIZE = (1024 * 1024) / 2; string source = files[i]; string destination = REMOTE_IMAGE_LOCATION + Guid.NewGuid() + Path.GetExtension(source); int chunk = CHUNK_SIZE; serverNames[i] = destination; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destination); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("2159860_user", "Qub1cR3dSt0rag3"); using (Stream inputStream = File.Open(source, FileMode.Open, FileAccess.Read)) using (Stream stream = request.GetRequestStream()) { request.ContentLength = inputStream.Length; if (request.ContentLength <= chunk) { byte[] buffer = new byte[inputStream.Length]; inputStream.Read(buffer, 0, buffer.Length); await stream.WriteAsync(buffer, 0, buffer.Length); UploadProgress(buffer.Length); } else { chunk = Math.Min((int)inputStream.Length / 100, CHUNK_SIZE); byte[] buffer = new byte[chunk]; int totalReadBytesCount = 0; int readBytesCount; while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0) { await stream.WriteAsync(buffer, 0, readBytesCount); totalReadBytesCount += readBytesCount; UploadProgress(readBytesCount); } } } } } SendIcon.Image = Properties.Resources.send; pictureURLs = serverNames; Uploading = false; }); }