using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace S3UploadDownload
{
public partial class FormDownload : Form
{
SprightlySoftAWS.S3.Download MyDownload;
System.ComponentModel.BackgroundWorker DownloadBackgroundWorker;
public FormDownload()
{
InitializeComponent();
}
private void FormDownload_Load(object sender, EventArgs e)
{
Properties.Settings MySettings = new Properties.Settings();
TextBoxDownloadBucketName.Text = MySettings.DownloadBucketName;
TextBoxDownloadKeyName.Text = MySettings.DownloadKeyName;
TextBoxDownloadFileName.Text = MySettings.DownloadFileName;
MyDownload = new SprightlySoftAWS.S3.Download();
MyDownload.ProgressChangedEvent += MyDownload_ProgressChangedEvent;
DownloadBackgroundWorker = new System.ComponentModel.BackgroundWorker();
DownloadBackgroundWorker.DoWork += DownloadBackgroundWorker_DoWork;
DownloadBackgroundWorker.RunWorkerCompleted += DownloadBackgroundWorker_RunWorkerCompleted;
ProgressBarTransfered.Value = 0;
LabelBytesTransfered.Text = "0 bytes / 0 bytes";
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
Properties.Settings MySettings = new Properties.Settings();
MySettings.DownloadBucketName = TextBoxDownloadBucketName.Text;
MySettings.DownloadKeyName = TextBoxDownloadKeyName.Text;
MySettings.DownloadFileName = TextBoxDownloadFileName.Text;
MySettings.Save();
}
private void EnableDisableStart()
{
TextBoxDownloadBucketName.Enabled = false;
TextBoxDownloadKeyName.Enabled = false;
TextBoxDownloadFileName.Enabled = false;
ButtonDownloadFile.Enabled = false;
ButtonAbort.Enabled = true;
ProgressBarTransfered.Enabled = true;
LabelBytesTransfered.Enabled = true;
ProgressBarTransfered.Value = 0;
LabelBytesTransfered.Text = "0 bytes / 0 bytes";
}
private void EnableDisableEnd()
{
TextBoxDownloadBucketName.Enabled = true;
TextBoxDownloadKeyName.Enabled = true;
TextBoxDownloadFileName.Enabled = true;
ButtonDownloadFile.Enabled = true;
ButtonAbort.Enabled = false;
ProgressBarTransfered.Enabled = false;
LabelBytesTransfered.Enabled = false;
ProgressBarTransfered.Value = 0;
LabelBytesTransfered.Text = "0 bytes / 0 bytes";
}
private void ButtonDownloadFile_Click(object sender, EventArgs e)
{
EnableDisableStart();
if (TextBoxDownloadFileName.Text.Contains("\\") == true && System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(TextBoxDownloadFileName.Text)) == true)
{
Properties.Settings MySettings = new Properties.Settings();
String RequestURL;
RequestURL = MyDownload.BuildS3RequestURL(true, "s3.amazonaws.com", TextBoxDownloadBucketName.Text, TextBoxDownloadKeyName.Text, "");
String RequestMethod = "GET";
Dictionary<String, String> ExtraRequestHeaders = new Dictionary<String, String>();
ExtraRequestHeaders.Add("x-amz-date", DateTime.UtcNow.ToString("r"));
String AuthorizationValue;
AuthorizationValue = MyDownload.GetS3AuthorizationValue(RequestURL, RequestMethod, ExtraRequestHeaders, MySettings.AWSAccessKeyId, MySettings.AWSSecretAccessKey);
ExtraRequestHeaders.Add("Authorization", AuthorizationValue);
//Use a hash table to pass parameters to the function in the BackgroundWorker.
System.Collections.Hashtable DownloadHashTable = new System.Collections.Hashtable();
DownloadHashTable.Add("RequestURL", RequestURL);
DownloadHashTable.Add("RequestMethod", RequestMethod);
DownloadHashTable.Add("ExtraRequestHeaders", ExtraRequestHeaders);
DownloadHashTable.Add("LocalFileName", TextBoxDownloadFileName.Text);
//Run the DownloadFile function in a BackgroundWorker process. Downloading a large
//file may take a long time. Running the process in a BackgroundWorker will prevent
//the Window from locking up.
DownloadBackgroundWorker.RunWorkerAsync(DownloadHashTable);
}
else
{
EnableDisableEnd();
MessageBox.Show("The local file path does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DownloadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
System.Collections.Hashtable DownloadHashTable = e.Argument as System.Collections.Hashtable;
//Call the DownloadFile function and set the result. When the function is complete
//the RunWorkerCompleted event will fire. Use the parameters that were passed in the
//hash table.
e.Result = MyDownload.DownloadFile(DownloadHashTable["RequestURL"].ToString(), DownloadHashTable["RequestMethod"].ToString(), DownloadHashTable["ExtraRequestHeaders"] as Dictionary<String, String>, DownloadHashTable["LocalFileName"].ToString(), false);
}
private void DownloadBackgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
System.Diagnostics.Debug.Print("");
System.Diagnostics.Debug.Print(MyDownload.LogData);
System.Diagnostics.Debug.Print("");
EnableDisableEnd();
if (Convert.ToBoolean(e.Result) == true)
{
MessageBox.Show("Download complete.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//Delete a partially downloaded file
if (System.IO.File.Exists(TextBoxDownloadFileName.Text) == true)
{
System.IO.File.Delete(TextBoxDownloadFileName.Text);
}
//Show the error message.
String ResponseMessage;
if (MyDownload.ResponseString == "")
{
ResponseMessage = MyDownload.ErrorDescription;
}
else
{
System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
XmlDoc.LoadXml(MyDownload.ResponseString);
System.Xml.XmlNode XmlNode;
XmlNode = XmlDoc.SelectSingleNode("/Error/Message");
ResponseMessage = XmlNode.InnerText;
}
MessageBox.Show(ResponseMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonAbort_Click(object sender, EventArgs e)
{
MyDownload.Abort();
}
private void MyDownload_ProgressChangedEvent()
{
if (this.InvokeRequired == true)
{
this.BeginInvoke(new MethodInvoker(delegate() { MyDownload_ProgressChangedEvent(); }));
}
else
{
//Set the progress bar when the ProgressChangedEvent is fired.
if (MyDownload.BytesTotal > 0)
{
decimal MyDecimal = (Convert.ToDecimal(MyDownload.BytesTransfered) / Convert.ToDecimal(MyDownload.BytesTotal)) * 100;
ProgressBarTransfered.Value = Convert.ToInt32(MyDecimal);
SprightlySoftAWS.S3.Helper MyHelper = new SprightlySoftAWS.S3.Helper();
LabelBytesTransfered.Text = MyHelper.FormatByteSize(MyDownload.BytesTransfered) + " / " + MyHelper.FormatByteSize(MyDownload.BytesTotal);
}
}
}
}
}