Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.68 KB | None | 0 0
  1. using IWshRuntimeLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace GHFUploader
  14. {
  15.     public partial class ConfigureForm2 : Form
  16.     {
  17.         private RichTextBox richTextBox1;
  18.         private GroupBox groupBox1;
  19.  
  20.         public ConfigureForm2(List<string> strings)
  21.         {
  22.             InitializeComponent();
  23.             foreach (string str in strings)
  24.             {
  25.                 richTextBox1.Text += str;
  26.                 richTextBox1.Text += '\n';
  27.             }
  28.             richTextBox1.AddContextMenu();
  29.             this.BringToFront();
  30.         }
  31.  
  32.         private void InitializeComponent()
  33.         {
  34.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  35.             this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  36.             this.groupBox1.SuspendLayout();
  37.             this.SuspendLayout();
  38.             //
  39.             // groupBox1
  40.             //
  41.             this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  42.             | System.Windows.Forms.AnchorStyles.Left)
  43.             | System.Windows.Forms.AnchorStyles.Right)));
  44.             this.groupBox1.Controls.Add(this.richTextBox1);
  45.             this.groupBox1.Location = new System.Drawing.Point(12, 12);
  46.             this.groupBox1.Name = "groupBox1";
  47.             this.groupBox1.Size = new System.Drawing.Size(865, 551);
  48.             this.groupBox1.TabIndex = 0;
  49.             this.groupBox1.TabStop = false;
  50.             this.groupBox1.Text = "results";
  51.             //
  52.             // richTextBox1
  53.             //
  54.             this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  55.             | System.Windows.Forms.AnchorStyles.Left)
  56.             | System.Windows.Forms.AnchorStyles.Right)));
  57.             this.richTextBox1.Location = new System.Drawing.Point(6, 19);
  58.             this.richTextBox1.Name = "richTextBox1";
  59.             this.richTextBox1.Size = new System.Drawing.Size(853, 526);
  60.             this.richTextBox1.TabIndex = 0;
  61.             this.richTextBox1.Text = "";
  62.             //
  63.             // ConfigureForm2
  64.             //
  65.             this.ClientSize = new System.Drawing.Size(889, 575);
  66.             this.Controls.Add(this.groupBox1);
  67.             this.DoubleBuffered = true;
  68.             this.MinimumSize = new System.Drawing.Size(200, 200);
  69.             this.Name = "ConfigureForm2";
  70.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  71.             this.Text = "GHFUploader Results";
  72.             this.TopMost = true;
  73.             this.groupBox1.ResumeLayout(false);
  74.             this.ResumeLayout(false);
  75.         }
  76.     }
  77.  
  78.     public partial class ConfigureForm : Form
  79.     {
  80.         private Label installedLabel;
  81.         private Button uninstallButton;
  82.         private Button installButton;
  83.         private GroupBox groupBox1;
  84.  
  85.         public ConfigureForm()
  86.         {
  87.             InitializeComponent();
  88.             CheckInstallStatus();
  89.             this.BringToFront();
  90.         }
  91.  
  92.         private void InitializeComponent()
  93.         {
  94.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  95.             this.installedLabel = new System.Windows.Forms.Label();
  96.             this.uninstallButton = new System.Windows.Forms.Button();
  97.             this.installButton = new System.Windows.Forms.Button();
  98.             this.groupBox1.SuspendLayout();
  99.             this.SuspendLayout();
  100.             //
  101.             // groupBox1
  102.             //
  103.             this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  104.             | System.Windows.Forms.AnchorStyles.Left)
  105.             | System.Windows.Forms.AnchorStyles.Right)));
  106.             this.groupBox1.Controls.Add(this.installedLabel);
  107.             this.groupBox1.Controls.Add(this.uninstallButton);
  108.             this.groupBox1.Controls.Add(this.installButton);
  109.             this.groupBox1.Location = new System.Drawing.Point(12, 12);
  110.             this.groupBox1.Name = "groupBox1";
  111.             this.groupBox1.Size = new System.Drawing.Size(370, 83);
  112.             this.groupBox1.TabIndex = 0;
  113.             this.groupBox1.TabStop = false;
  114.             this.groupBox1.Text = "install / uninstall";
  115.             //
  116.             // installedLabel
  117.             //
  118.             this.installedLabel.AutoSize = true;
  119.             this.installedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  120.             this.installedLabel.Location = new System.Drawing.Point(96, 28);
  121.             this.installedLabel.Name = "installedLabel";
  122.             this.installedLabel.Size = new System.Drawing.Size(271, 31);
  123.             this.installedLabel.TabIndex = 2;
  124.             this.installedLabel.Text = "currently not installed";
  125.             //
  126.             // uninstallButton
  127.             //
  128.             this.uninstallButton.Location = new System.Drawing.Point(6, 48);
  129.             this.uninstallButton.Name = "uninstallButton";
  130.             this.uninstallButton.Size = new System.Drawing.Size(84, 23);
  131.             this.uninstallButton.TabIndex = 1;
  132.             this.uninstallButton.Text = "Uninstall";
  133.             this.uninstallButton.UseVisualStyleBackColor = true;
  134.             this.uninstallButton.Click += new System.EventHandler(this.uninstallButton_Click);
  135.             //
  136.             // installButton
  137.             //
  138.             this.installButton.Location = new System.Drawing.Point(6, 19);
  139.             this.installButton.Name = "installButton";
  140.             this.installButton.Size = new System.Drawing.Size(84, 23);
  141.             this.installButton.TabIndex = 0;
  142.             this.installButton.Text = "Install";
  143.             this.installButton.UseVisualStyleBackColor = true;
  144.             this.installButton.Click += new System.EventHandler(this.installButton_Click);
  145.             //
  146.             // ConfigureForm
  147.             //
  148.             this.ClientSize = new System.Drawing.Size(394, 107);
  149.             this.Controls.Add(this.groupBox1);
  150.             this.DoubleBuffered = true;
  151.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  152.             this.MaximizeBox = false;
  153.             this.MaximumSize = new System.Drawing.Size(410, 146);
  154.             this.MinimumSize = new System.Drawing.Size(410, 146);
  155.             this.Name = "ConfigureForm";
  156.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  157.             this.Text = "GHFUploader Installer";
  158.             this.TopMost = true;
  159.             this.groupBox1.ResumeLayout(false);
  160.             this.groupBox1.PerformLayout();
  161.             this.ResumeLayout(false);
  162.         }
  163.  
  164.         private void installButton_Click(object sender, EventArgs e)
  165.         {
  166.             uninstallButton_Click(null, null);
  167.  
  168.             string exeDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader");
  169.             if (!Directory.Exists(exeDirectory))
  170.             {
  171.                 Directory.CreateDirectory(exeDirectory);
  172.             }
  173.             string exePath = Path.Combine(exeDirectory, "GHFUploader.exe");
  174.             string currentExecutingLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
  175.             System.IO.File.Copy(currentExecutingLocation, exePath);
  176.  
  177.             string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.lnk");
  178.  
  179.             WshShell wsh = new WshShell();
  180.             IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(shortcutPath) as IWshRuntimeLibrary.IWshShortcut;
  181.             shortcut.Arguments = "";
  182.             shortcut.TargetPath = exePath;
  183.             shortcut.WindowStyle = 1;
  184.             shortcut.Description = "upload to 666kb.com";
  185.             shortcut.WorkingDirectory = @"";
  186.             shortcut.IconLocation = "specify icon location";
  187.             shortcut.Save();
  188.  
  189.             CheckInstallStatus();
  190.         }
  191.  
  192.         private void uninstallButton_Click(object sender, EventArgs e)
  193.         {
  194.             string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.lnk");
  195.             if (System.IO.File.Exists(shortcutPath))
  196.             {
  197.                 System.IO.File.Delete(shortcutPath);
  198.             }
  199.             string fileSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader", "GHFUploader.exe");
  200.             if (System.IO.File.Exists(fileSavePath))
  201.             {
  202.                 System.IO.File.Delete(fileSavePath);
  203.             }
  204.             CheckInstallStatus();
  205.         }
  206.  
  207.         private void CheckInstallStatus()
  208.         {
  209.             string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.url");
  210.             string fileSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader", "GHFUploader.exe");
  211.             if (System.IO.File.Exists(shortcutPath) || System.IO.File.Exists(fileSavePath))
  212.             {
  213.                 installedLabel.Text = "currently installed";
  214.                 installedLabel.ForeColor = System.Drawing.Color.Green;
  215.             }
  216.             else
  217.             {
  218.                 installedLabel.Text = "currently not installed";
  219.                 installedLabel.ForeColor = System.Drawing.Color.Red;
  220.             }
  221.         }
  222.     }
  223.  
  224.     internal class Program
  225.     {
  226.         [STAThread]
  227.         private static void Main(string[] args)
  228.         {
  229.             Regex urlExtractionRegex = new Regex("n Bilddatei: <a href=\"(.+?)\"", RegexOptions.Compiled);
  230.  
  231.             if (args.Length == 0)
  232.             {
  233.                 // no argument, .exe was executed directly
  234.                 ConfigureForm configureForm = new ConfigureForm();
  235.                 Application.Run(configureForm);
  236.             }
  237.             else
  238.             {
  239.                 List<string> results = new List<string>();
  240.                 // arguments, let's upload
  241.                 foreach (string fileToBeUploaded in args)
  242.                 {
  243.                     string uploadResult = Upload(fileToBeUploaded);
  244.                     Match match = urlExtractionRegex.Match(uploadResult);
  245.                     if (match.Success)
  246.                     {
  247.                         string resultUrl = match.Groups[1].ToString();
  248.                         results.Add(resultUrl);
  249.                     }
  250.                     else
  251.                     {
  252.                         results.Add("File<" + fileToBeUploaded + "> could not be uploaded.");
  253.                     }
  254.                 }
  255.  
  256.                 if (results.Count == 1)
  257.                 {
  258.                     Clipboard.SetText(results[0]);
  259.                     MessageBox.Show("Upload attempt finished. The result is in your clipboard.", "GHUploader", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  260.                 }
  261.                 else
  262.                 {
  263.                     Application.Run(new ConfigureForm2(results));
  264.                 }
  265.             }
  266.         }
  267.  
  268.         // uploads file at path, returns url
  269.         private static string Upload(string path)
  270.         {
  271.             string result = UploadFilesToRemoteUrl("http://666kb.com/u.php", path);
  272.             return result;
  273.         }
  274.  
  275.         // from http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
  276.         public static string UploadFilesToRemoteUrl(string url, string file, NameValueCollection formFields = null)
  277.         {
  278.             string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
  279.  
  280.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  281.             request.ContentType = "multipart/form-data; boundary=" +
  282.                                     boundary;
  283.             request.Method = "POST";
  284.             request.KeepAlive = true;
  285.  
  286.             Stream memStream = new System.IO.MemoryStream();
  287.  
  288.             var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
  289.                                                                     boundary + "\r\n");
  290.             var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
  291.                                                                         boundary + "--");
  292.  
  293.             string formdataTemplate = "\r\n--" + boundary +
  294.                                         "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
  295.  
  296.             if (formFields != null)
  297.             {
  298.                 foreach (string key in formFields.Keys)
  299.                 {
  300.                     string formitem = string.Format(formdataTemplate, key, formFields[key]);
  301.                     byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  302.                     memStream.Write(formitembytes, 0, formitembytes.Length);
  303.                 }
  304.             }
  305.  
  306.             string headerTemplate =
  307.                 "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
  308.                 "Content-Type: application/octet-stream\r\n\r\n";
  309.  
  310.             memStream.Write(boundarybytes, 0, boundarybytes.Length);
  311.             var header = string.Format(headerTemplate, "f", file);
  312.             var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  313.  
  314.             memStream.Write(headerbytes, 0, headerbytes.Length);
  315.  
  316.             using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
  317.             {
  318.                 var buffer = new byte[1024];
  319.                 var bytesRead = 0;
  320.                 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  321.                 {
  322.                     memStream.Write(buffer, 0, bytesRead);
  323.                 }
  324.             }
  325.  
  326.             memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  327.             request.ContentLength = memStream.Length;
  328.  
  329.             using (Stream requestStream = request.GetRequestStream())
  330.             {
  331.                 memStream.Position = 0;
  332.                 byte[] tempBuffer = new byte[memStream.Length];
  333.                 memStream.Read(tempBuffer, 0, tempBuffer.Length);
  334.                 memStream.Close();
  335.                 requestStream.Write(tempBuffer, 0, tempBuffer.Length);
  336.             }
  337.  
  338.             using (var response = request.GetResponse())
  339.             {
  340.                 Stream stream2 = response.GetResponseStream();
  341.                 StreamReader reader2 = new StreamReader(stream2);
  342.                 return reader2.ReadToEnd();
  343.             }
  344.         }
  345.     }
  346.  
  347.     internal static class RichTextBoxExtension
  348.     {
  349.         public static void AddContextMenu(this RichTextBox rtb)
  350.         {
  351.             if (rtb.ContextMenuStrip == null)
  352.             {
  353.                 ContextMenuStrip cms = new ContextMenuStrip { ShowImageMargin = false };
  354.                 ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
  355.                 tsmiCut.Click += (sender, e) => rtb.Cut();
  356.                 cms.Items.Add(tsmiCut);
  357.                 ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
  358.                 tsmiCopy.Click += (sender, e) => rtb.Copy();
  359.                 cms.Items.Add(tsmiCopy);
  360.                 ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
  361.                 tsmiPaste.Click += (sender, e) => rtb.Paste();
  362.                 cms.Items.Add(tsmiPaste);
  363.                 rtb.ContextMenuStrip = cms;
  364.             }
  365.         }
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement