/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary) { using (Stream memStream = new MemoryStream()) { byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); // tamplates string imageType = AccessoryFuncs.GetMimeType(file); string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; memStream.Write(boundarybytes, 0, boundarybytes.Length); string header = string.Format(headerFileTemplate, "attached", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[10240]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } memStream.Write(boundarybytes, 0, boundarybytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary) { string boundaryInContnet = "\r\n--" + boundary + "\r\n"; using (Stream memStream = new MemoryStream()) { // tamplates string imageType = AccessoryFuncs.GetMimeType(file); const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}"; string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; byte[] contentBytes; StringBuilder builder = new StringBuilder(); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "MAX_FILE_SIZE", "15000000")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "APC_UPLOAD_PROGRESS", getProgressKey())); builder.Append(boundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); // file contains the path of the file. string header = string.Format(headerFileTemplate, "file", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } builder.Length = 0; // truncate stringBuilder. builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "title", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "description", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "tags", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "category", "general")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "private", "false")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "submit", "")); builder.Append(boundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// the delegate for the event of preesing on "Upload button" /// </summary> /// <param name="sender">button</param> /// <param name="e"> Useful parameters. </param> private void Upload_Click(object sender, EventArgs e) { try { #region --- Check for user errors. --- if (radioButton_Image.Checked == true) { // Checks if user entered file. if (textBox_FilePathToUpload.Text == string.Empty) { throw new Exception("No file was selected"); } // Checks that all files exist. if (!File.Exists(textBox_FilePathToUpload.Text)) { throw new Exception("File does not exist"); } } else if (radioButton_Url.Checked == true) { // Checks if user entered link. if (textBox_linkToUpload.Text == string.Empty) { throw new Exception("No URL was given"); } // Checks that link is valid. if (!AccessoryFuncs.IsURL(textBox_linkToUpload.Text)) { throw new Exception("URL is malformed"); } } #endregion // Change GUI. UploadingModeFormView(); if (radioButton_Image.Checked) { backgroundWorker.RunWorkerAsync(textBox_FilePathToUpload.Text); } else { backgroundWorker.RunWorkerAsync(textBox_linkToUpload.Text); } } catch (Exception e1) { MessageBoxOptions options = MessageBoxOptions.RtlReading; MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, options); } }
/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary) { string boundaryInContnet = "\r\n--" + boundary + "\r\n"; using (Stream memStream = new MemoryStream()) { // tamplates string imageType = AccessoryFuncs.GetMimeType(file); const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}"; string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; const string headerEmptyFileTemplate = "Content-Disposition: form-data; name=\"image{0}\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n"; StringBuilder builder = new StringBuilder(); builder.Append(boundaryInContnet); for (int i = 2; i <= 6; i++) { builder.Append(string.Format(headerEmptyFileTemplate, i.ToString())); builder.Append(boundaryInContnet); } byte[] contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); // file contains the path of the file. string header = string.Format(headerFileTemplate, "image1", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } builder.Length = 0; builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "x", "50")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "y", "10")); builder.Append(boundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// Start uploading. /// </summary> /// <param name="sender"> BackgroundWorker. </param> /// <param name="e"> Useful parameters. </param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { if (!AccessoryFuncs.CheckForInternetConnection( ListOfServerProperties.Instance.GetURL((int)_siteToUpload))) { throw new Exception("Could not access: " + ListOfServerProperties.Instance.GetURL((int)_siteToUpload) + "\nYou might be offline"); } e.Result = UploaderFactory.StartUpload(_siteToUpload, e.Argument as string); } catch (Exception ex) { // Cancel thread. e.Cancel = true; backgroundWorker.CancelAsync(); // Show error. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading); } }
/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary) { string boundaryInContnet = "\r\n--" + boundary + "\r\n"; using (Stream memStream = new MemoryStream()) { // tamplates string imageType = AccessoryFuncs.GetMimeType(file); const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}"; string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; const string headerEmptyFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n"; byte[] contentBytes; StringBuilder builder = new StringBuilder(); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "SID", Sid)); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "HOST_WWW", HostWww)); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "HOST_END", HostEnd)); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "UPLOAD_IDENTIFIER", UploadIdentifier)); builder.Append(boundaryInContnet); if (File.Exists(file)) // Upload file. { builder.Append(string.Format(headerEmptyFileTemplate, "file_1")); builder.Append(boundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); builder.Length = 0; // Truncate StringBuilder // file contains the path of the file. string header = string.Format(headerFileTemplate, "file_0", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "urlimage", "")); builder.Append(boundaryInContnet); } else // Upload URL. { builder.Append(string.Format(headerEmptyFileTemplate, "file_0")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "urlimage", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "urluploadfile_0", file)); builder.Append(boundaryInContnet); } builder.Append(string.Format(headerTemplate, "x", "30")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "y", "26")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "resize_x", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "resize_y", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerEmptyFileTemplate, "branding_image")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "branding_urlimage", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "branding_text", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "thumbbar", "1")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "rotate", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "mirror", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "colormode_do", "1")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "camerashakestep", "1")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "folder", "0")); builder.Append(boundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary) { // tamplates string imageType = AccessoryFuncs.GetMimeType(file); const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}"; string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; string startBoundaryInContnet = "--" + boundary + "\r\n"; string boundaryInContnet = "\r\n--" + boundary + "\r\n"; string endBoundaryInContnet = "\r\n--" + boundary + "--\r\n"; bool isUrl = !File.Exists(file); using (Stream memStream = new MemoryStream()) { byte[] contentBytes = Encoding.UTF8.GetBytes(startBoundaryInContnet); memStream.Write(contentBytes, 0, contentBytes.Length); StringBuilder builder = new StringBuilder(); if (!isUrl) { // the file. string header = string.Format(headerFileTemplate, "source", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[10240]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "type", "file")); } else { builder.Append(string.Format(headerTemplate, "source", file)); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "type", "url")); } builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "action", "upload")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "timestamp", AccessoryFuncs.GetEpochTime())); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "auth_token", _authToken)); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "nsfw", "0")); builder.Append(endBoundaryInContnet); contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// Generates the buffer which will be sended to send the server via http post-multipart request /// </summary> /// <param name="file"> Image path. </param> /// <param name="boundary"> The boundary to seperate the fields in the multipart form. </param> /// <returns> Byte array with all the request content. </returns> private byte[] generateHttpMultipartRequestBuffer(string file, string boundary, out bool isUrl) { // tamplates string imageType = AccessoryFuncs.GetMimeType(file); const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}"; string headerFileTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: " + imageType + "\r\n\r\n"; string boundaryInContnet = "\r\n--" + boundary + "\r\n"; using (Stream memStream = new MemoryStream()) { StringBuilder builder = new StringBuilder(); byte[] contentBytes = Encoding.UTF8.GetBytes(boundaryInContnet); memStream.Write(contentBytes, 0, contentBytes.Length); isUrl = !File.Exists(file); if (!isUrl) { string header = string.Format(headerFileTemplate, "Filedata", file.Substring(file.LastIndexOf("\\") + 1)); byte[] headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } } builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "token", getToken())); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "upload_session", rand_string(32))); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "numfiles", "1")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "gallery", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "ui", "24__1920__1080__true__?__?__" + DateTime.Now.ToString("MM/dd/yyyy, h:mm:ss tt") + "__Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36__")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "optsize", "0")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "expire", "0")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "upload_referer", "")); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "session_upload", AccessoryFuncs.GetEpochTime().ToString())); builder.Append(boundaryInContnet); builder.Append(string.Format(headerTemplate, "forum", "")); builder.Append(boundaryInContnet); } else // URL { // For some reason stopped working. also in browser... isUrl = true; string date = DateTime.Now.ToString("MM/dd/yyyy, h%3Amm%3Ass+tt").Replace("/", "%2F7").Replace(",", "%2C").Replace(" ", "+"); var str = "token={0}&upload_session={1}&url={2}&numfiles=1&gallery=&ui=24__1920__1080__true__%3F__%3F__{3}__Mozilla%2F5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML%2C+like+Gecko)+Chrome%2F59.0.3071.115+Safari%2F537.36__&optsize=0&expire=0&session_upload={4}"; builder.Append(string.Format(str, getToken(), rand_string(32), file, date, ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString())); } contentBytes = Encoding.UTF8.GetBytes(builder.ToString()); memStream.Write(contentBytes, 0, contentBytes.Length); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); return(tempBuffer); } }
/// <summary> /// take the snapshot. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void butSnapShot_Click(object sender, EventArgs e) { SaveFileDialog savefiledialog = null; try { // Take a picture of screen. this.Opacity = 0; pictureBox1.Image = AccessoryFuncs.TakeScreenPicture(); this.Opacity = 100; // Open save file dialog, for saving the snapshot. savefiledialog = new SaveFileDialog(); savefiledialog.Filter = "JPEG|*.jpeg|PNG|*.png|GIF|*.gif"; // If Ok was pressed. if (savefiledialog.ShowDialog() == DialogResult.OK) { string imageFormat = Path.GetExtension(savefiledialog.FileName); System.Drawing.Imaging.ImageFormat formatToSave; // Check file extension. switch (imageFormat) { case ".jpeg": case ".JPEG": { formatToSave = System.Drawing.Imaging.ImageFormat.Jpeg; break; } case ".jpg": case ".JPG": { formatToSave = System.Drawing.Imaging.ImageFormat.Jpeg; break; } case ".png": case ".PNG": { formatToSave = System.Drawing.Imaging.ImageFormat.Png; break; } case ".gif": case ".GIF": { formatToSave = System.Drawing.Imaging.ImageFormat.Gif; break; } default: throw new Exception("Bad input"); } textBox_FilePathToUpload.Text = savefiledialog.FileName; pictureBox1.Image.Save(savefiledialog.FileName, formatToSave); // check if the "mspaint.exe" exist, if not button "edit" wil be disabled. if (File.Exists(MSPAINT_PATH)) { button_EditPicture.Enabled = true; } } } catch (Exception e1) { MessageBox.Show(e1.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading); } finally { savefiledialog.Dispose(); } }
/// <summary> /// Check if the application is up to date. /// </summary> private void CheckForUpdates() { // My special message box. MyMessageBox msg; try { if (AccessoryFuncs.CheckForInternetConnection("https://github.com")) { using (WebClient client = new WebClient()) { string htmlCode = client.DownloadString( "https://raw.githubusercontent.com/supermathbond/FastImageUpload/master/ImageUploader/Versions.txt#"); if (htmlCode == "") { msg = new MyMessageBox("Message", "Connection with server aborted"); this.Invoke(new Action <MyMessageBox>(ShowSpecialMessegeBox), msg); return; } // Searchs for the version number. int start = htmlCode.IndexOf("[version]") + "[version]".Length; int end = htmlCode.IndexOf("[/version]", start); string version = htmlCode.Substring(start, end - start); // Searchs for the Last improvements list. start = htmlCode.IndexOf("[Improvements]") + "[Improvements]".Length; end = htmlCode.IndexOf("[/Improvements]", start); string improvements = htmlCode.Substring(start, end - start); // Checks if the application is up to date. if (version == System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()) { msg = new MyMessageBox("Message", "Version is up to date.", string.Empty, improvements); this.Invoke(new Action <MyMessageBox>(ShowSpecialMessegeBox), msg); } else { // Searchs for the new version link. start = htmlCode.IndexOf("[Download]") + "[Download]".Length; end = htmlCode.IndexOf("[/Download]", start); string link = htmlCode.Substring(start, end - start); msg = new MyMessageBox("Message", "There is a new version.\nFor download:", link, improvements); this.Invoke(new Action <MyMessageBox>(ShowSpecialMessegeBox), msg); } } } else { // No connection. msg = new MyMessageBox("Message", "Can't reach server,\ncheck your internet connection."); this.Invoke(new Action <MyMessageBox>(ShowSpecialMessegeBox), msg); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }