Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using IWshRuntimeLibrary;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace GHFUploader
- {
- public partial class ConfigureForm2 : Form
- {
- private RichTextBox richTextBox1;
- private GroupBox groupBox1;
- public ConfigureForm2(List<string> strings)
- {
- InitializeComponent();
- foreach (string str in strings)
- {
- richTextBox1.Text += str;
- richTextBox1.Text += '\n';
- }
- richTextBox1.AddContextMenu();
- this.BringToFront();
- }
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.richTextBox1 = new System.Windows.Forms.RichTextBox();
- this.groupBox1.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.groupBox1.Controls.Add(this.richTextBox1);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(865, 551);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "results";
- //
- // richTextBox1
- //
- this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.richTextBox1.Location = new System.Drawing.Point(6, 19);
- this.richTextBox1.Name = "richTextBox1";
- this.richTextBox1.Size = new System.Drawing.Size(853, 526);
- this.richTextBox1.TabIndex = 0;
- this.richTextBox1.Text = "";
- //
- // ConfigureForm2
- //
- this.ClientSize = new System.Drawing.Size(889, 575);
- this.Controls.Add(this.groupBox1);
- this.DoubleBuffered = true;
- this.MinimumSize = new System.Drawing.Size(200, 200);
- this.Name = "ConfigureForm2";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "GHFUploader Results";
- this.TopMost = true;
- this.groupBox1.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- }
- public partial class ConfigureForm : Form
- {
- private Label installedLabel;
- private Button uninstallButton;
- private Button installButton;
- private GroupBox groupBox1;
- public ConfigureForm()
- {
- InitializeComponent();
- CheckInstallStatus();
- this.BringToFront();
- }
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.installedLabel = new System.Windows.Forms.Label();
- this.uninstallButton = new System.Windows.Forms.Button();
- this.installButton = new System.Windows.Forms.Button();
- this.groupBox1.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.groupBox1.Controls.Add(this.installedLabel);
- this.groupBox1.Controls.Add(this.uninstallButton);
- this.groupBox1.Controls.Add(this.installButton);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(370, 83);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "install / uninstall";
- //
- // installedLabel
- //
- this.installedLabel.AutoSize = true;
- this.installedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.installedLabel.Location = new System.Drawing.Point(96, 28);
- this.installedLabel.Name = "installedLabel";
- this.installedLabel.Size = new System.Drawing.Size(271, 31);
- this.installedLabel.TabIndex = 2;
- this.installedLabel.Text = "currently not installed";
- //
- // uninstallButton
- //
- this.uninstallButton.Location = new System.Drawing.Point(6, 48);
- this.uninstallButton.Name = "uninstallButton";
- this.uninstallButton.Size = new System.Drawing.Size(84, 23);
- this.uninstallButton.TabIndex = 1;
- this.uninstallButton.Text = "Uninstall";
- this.uninstallButton.UseVisualStyleBackColor = true;
- this.uninstallButton.Click += new System.EventHandler(this.uninstallButton_Click);
- //
- // installButton
- //
- this.installButton.Location = new System.Drawing.Point(6, 19);
- this.installButton.Name = "installButton";
- this.installButton.Size = new System.Drawing.Size(84, 23);
- this.installButton.TabIndex = 0;
- this.installButton.Text = "Install";
- this.installButton.UseVisualStyleBackColor = true;
- this.installButton.Click += new System.EventHandler(this.installButton_Click);
- //
- // ConfigureForm
- //
- this.ClientSize = new System.Drawing.Size(394, 107);
- this.Controls.Add(this.groupBox1);
- this.DoubleBuffered = true;
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.MaximizeBox = false;
- this.MaximumSize = new System.Drawing.Size(410, 146);
- this.MinimumSize = new System.Drawing.Size(410, 146);
- this.Name = "ConfigureForm";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "GHFUploader Installer";
- this.TopMost = true;
- this.groupBox1.ResumeLayout(false);
- this.groupBox1.PerformLayout();
- this.ResumeLayout(false);
- }
- private void installButton_Click(object sender, EventArgs e)
- {
- uninstallButton_Click(null, null);
- string exeDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader");
- if (!Directory.Exists(exeDirectory))
- {
- Directory.CreateDirectory(exeDirectory);
- }
- string exePath = Path.Combine(exeDirectory, "GHFUploader.exe");
- string currentExecutingLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
- System.IO.File.Copy(currentExecutingLocation, exePath);
- string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.lnk");
- WshShell wsh = new WshShell();
- IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(shortcutPath) as IWshRuntimeLibrary.IWshShortcut;
- shortcut.Arguments = "";
- shortcut.TargetPath = exePath;
- shortcut.WindowStyle = 1;
- shortcut.Description = "upload to 666kb.com";
- shortcut.WorkingDirectory = @"";
- shortcut.IconLocation = "specify icon location";
- shortcut.Save();
- CheckInstallStatus();
- }
- private void uninstallButton_Click(object sender, EventArgs e)
- {
- string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.lnk");
- if (System.IO.File.Exists(shortcutPath))
- {
- System.IO.File.Delete(shortcutPath);
- }
- string fileSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader", "GHFUploader.exe");
- if (System.IO.File.Exists(fileSavePath))
- {
- System.IO.File.Delete(fileSavePath);
- }
- CheckInstallStatus();
- }
- private void CheckInstallStatus()
- {
- string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), "uploadToGHF.url");
- string fileSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GHFUploader", "GHFUploader.exe");
- if (System.IO.File.Exists(shortcutPath) || System.IO.File.Exists(fileSavePath))
- {
- installedLabel.Text = "currently installed";
- installedLabel.ForeColor = System.Drawing.Color.Green;
- }
- else
- {
- installedLabel.Text = "currently not installed";
- installedLabel.ForeColor = System.Drawing.Color.Red;
- }
- }
- }
- internal class Program
- {
- [STAThread]
- private static void Main(string[] args)
- {
- Regex urlExtractionRegex = new Regex("n Bilddatei: <a href=\"(.+?)\"", RegexOptions.Compiled);
- if (args.Length == 0)
- {
- // no argument, .exe was executed directly
- ConfigureForm configureForm = new ConfigureForm();
- Application.Run(configureForm);
- }
- else
- {
- List<string> results = new List<string>();
- // arguments, let's upload
- foreach (string fileToBeUploaded in args)
- {
- string uploadResult = Upload(fileToBeUploaded);
- Match match = urlExtractionRegex.Match(uploadResult);
- if (match.Success)
- {
- string resultUrl = match.Groups[1].ToString();
- results.Add(resultUrl);
- }
- else
- {
- results.Add("File<" + fileToBeUploaded + "> could not be uploaded.");
- }
- }
- if (results.Count == 1)
- {
- Clipboard.SetText(results[0]);
- MessageBox.Show("Upload attempt finished. The result is in your clipboard.", "GHUploader", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- else
- {
- Application.Run(new ConfigureForm2(results));
- }
- }
- }
- // uploads file at path, returns url
- private static string Upload(string path)
- {
- string result = UploadFilesToRemoteUrl("http://666kb.com/u.php", path);
- return result;
- }
- // from http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
- public static string UploadFilesToRemoteUrl(string url, string file, NameValueCollection formFields = null)
- {
- string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.ContentType = "multipart/form-data; boundary=" +
- boundary;
- request.Method = "POST";
- request.KeepAlive = true;
- Stream memStream = new System.IO.MemoryStream();
- var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
- boundary + "\r\n");
- var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
- boundary + "--");
- string formdataTemplate = "\r\n--" + boundary +
- "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
- if (formFields != null)
- {
- foreach (string key in formFields.Keys)
- {
- string formitem = string.Format(formdataTemplate, key, formFields[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- memStream.Write(formitembytes, 0, formitembytes.Length);
- }
- }
- string headerTemplate =
- "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
- "Content-Type: application/octet-stream\r\n\r\n";
- memStream.Write(boundarybytes, 0, boundarybytes.Length);
- var header = string.Format(headerTemplate, "f", file);
- var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- memStream.Write(headerbytes, 0, headerbytes.Length);
- using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
- {
- var buffer = new byte[1024];
- var bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- memStream.Write(buffer, 0, bytesRead);
- }
- }
- memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
- request.ContentLength = memStream.Length;
- using (Stream requestStream = request.GetRequestStream())
- {
- memStream.Position = 0;
- byte[] tempBuffer = new byte[memStream.Length];
- memStream.Read(tempBuffer, 0, tempBuffer.Length);
- memStream.Close();
- requestStream.Write(tempBuffer, 0, tempBuffer.Length);
- }
- using (var response = request.GetResponse())
- {
- Stream stream2 = response.GetResponseStream();
- StreamReader reader2 = new StreamReader(stream2);
- return reader2.ReadToEnd();
- }
- }
- }
- internal static class RichTextBoxExtension
- {
- public static void AddContextMenu(this RichTextBox rtb)
- {
- if (rtb.ContextMenuStrip == null)
- {
- ContextMenuStrip cms = new ContextMenuStrip { ShowImageMargin = false };
- ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
- tsmiCut.Click += (sender, e) => rtb.Cut();
- cms.Items.Add(tsmiCut);
- ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
- tsmiCopy.Click += (sender, e) => rtb.Copy();
- cms.Items.Add(tsmiCopy);
- ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
- tsmiPaste.Click += (sender, e) => rtb.Paste();
- cms.Items.Add(tsmiPaste);
- rtb.ContextMenuStrip = cms;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement