Click here to Skip to main content
15,891,725 members
Articles / Programming Languages / C# 4.0

Gett.NET - A library for Ge.tt Web Services

Rate me:
Please Sign up or sign in to vote.
4.98/5 (14 votes)
18 Jan 2016CPOL7 min read 54.3K   1.3K   45  
Use Ge.tt Web Services for real-time file publishing and sharing.
/*

  Copyright (c) 2012 Togocoder
 
  This program is to illustrate how to use Gett.NET library that uses the Ge.tt API Web Service, http://ge.tt/developers
  Please see copyright text in the source code for Gett.NET.

*/
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace GettTest
{
  public partial class UploadDownloadProgress : Form
  {
    private bool upload;
    private string fileName;
    private long fileSize = 0;
    private Gett.Sharing.GettFile file;
    private DateTime started, ended;

    public UploadDownloadProgress(bool upload, string fileName, Gett.Sharing.GettFile file)
    {
      this.upload = upload;
      this.fileName = fileName;
      this.file = file;

      InitializeComponent();
    }

    private void UploadDownloadProgress_Load(object sender, EventArgs e)
    {
      this.Text = (this.upload ? "Upload file " : "Download file ") + this.file.Info.FileName;
      this.label_FileName.Text = this.file.Info.FileName;

      if (this.upload)
      {
        this.button_OpenFolder.Visible = false;
      }
    }

    private void UploadDownloadProgress_Shown(object sender, EventArgs e)
    {
      if (this.upload)
      {
        try
        {
          this.file.UploadProgressChanged += new System.Net.UploadProgressChangedEventHandler(this.File_UploadProgressChanged);
          this.file.UploadFileCompleted += new System.Net.UploadFileCompletedEventHandler(this.File_UploadFileCompleted);

          this.started = DateTime.Now;
          this.fileSize = new System.IO.FileInfo(this.fileName).Length;
          this.file.UploadFileAsync(this.fileName);
        }
        catch (Exception ex)
        {
          this.file.UploadProgressChanged -= this.File_UploadProgressChanged;
          this.file.UploadFileCompleted -= this.File_UploadFileCompleted;

          MessageBox.Show("Failed to upload new file '" + this.fileName + "' to Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          Close();
        }
      }
      else
      {
        try
        {
          this.file.DownloadAsyncProgressChanged += new System.Net.DownloadProgressChangedEventHandler(this.File_DownloadAsyncProgressChanged);
          this.file.DownloadAsyncFileCompleted += new AsyncCompletedEventHandler(this.File_DownloadAsyncFileCompleted);

          this.started = DateTime.Now;
          this.fileSize = this.file.Info.Size;
          this.file.DownloadFileAsync(this.fileName);
        }
        catch (Exception ex)
        {
          this.file.DownloadAsyncProgressChanged -= this.File_DownloadAsyncProgressChanged;
          this.file.DownloadAsyncFileCompleted -= this.File_DownloadAsyncFileCompleted;

          MessageBox.Show("Failed to download '" + this.file.Info.FileName + "' from Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          Close();
        }
      }
    }

    private void button_Cancel_Click(object sender, EventArgs e)
    {
      try
      {
        this.file.CancelAsync();

        this.button_Cancel.Text = "Close";
        this.button_Cancel.Click -= this.button_Cancel_Click;
        this.button_Cancel.Click += (obj, evt) => Close();
      }
      catch { }
    }

    private void UploadDownloadProgress_FormClosing(object sender, FormClosingEventArgs e)
    {
      try
      {
        this.file.CancelAsync();
      }
      catch { }

      if (this.upload)
      {
        this.file.UploadProgressChanged -= this.File_UploadProgressChanged;
        this.file.UploadFileCompleted -= this.File_UploadFileCompleted;

      }
      else
      {
        this.file.DownloadAsyncProgressChanged -= this.File_DownloadAsyncProgressChanged;
        this.file.DownloadAsyncFileCompleted -= this.File_DownloadAsyncFileCompleted;
      }
    }

    #region Upload
    private void File_UploadFileCompleted(object sender, System.Net.UploadFileCompletedEventArgs e)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new Action(() => File_UploadFileCompleted(sender, e)));
        return;
      }

      this.file.UploadProgressChanged -= this.File_UploadProgressChanged;
      this.file.UploadFileCompleted -= this.File_UploadFileCompleted;

      this.ended = DateTime.Now;
      TimeSpan totalTime = this.ended - this.started;
      long speed = (long)totalTime.TotalSeconds > 0 ? this.fileSize / (long)totalTime.TotalSeconds : 0;
      this.progressBar_UploadDownload.Value = this.progressBar_UploadDownload.Maximum;

      this.label_Speed.Text = string.Format("Total time: {0}, speed was {1}/s", (DateTime.Now - this.started).ToString(@"hh\:mm\:ss"), FormatSizeDisplay(speed));

      this.button_Cancel.Text = "Close";
      this.button_Cancel.Click -= this.button_Cancel_Click;
      this.button_Cancel.Click += (obj, evt) => Close();

      if (e.Error != null)
      {
        MessageBox.Show("Failed to upload new file '" + this.fileName + "' to Ge.tt" + Environment.NewLine + "Exception of type " + e.Error.GetType().ToString() + Environment.NewLine + e.Error.Message);
      }
    }

    private void File_UploadProgressChanged(object sender, System.Net.UploadProgressChangedEventArgs e)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new Action(() => File_UploadProgressChanged(sender, e)));
        return;
      }

      long percentage = (100 * e.BytesSent) / e.TotalBytesToSend;
      this.label_Size.Text = string.Format("{2}% - Total: {1} - Uploaded: {0}", FormatSizeDisplay(e.BytesSent), FormatSizeDisplay(e.TotalBytesToSend), percentage);

      TimeSpan elapsedTime = DateTime.Now - started;
      TimeSpan estimatedTime = TimeSpan.FromSeconds((e.TotalBytesToSend - e.BytesSent) / ((double)e.BytesSent / elapsedTime.TotalSeconds));
      if (estimatedTime.Seconds > 0 && estimatedTime.Seconds % 2 == 0)
      {
        this.label_Speed.Text = string.Format("Estimated time:{0}", estimatedTime.ToString(@"hh\:mm\:ss"));
      }

      try
      {
        this.progressBar_UploadDownload.Value = (int)percentage;
      }
      catch { }
    }
    #endregion

    #region Download
    private void File_DownloadAsyncFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new Action(() => File_DownloadAsyncFileCompleted(sender, e)));
        return;
      }

      this.file.DownloadAsyncProgressChanged -= this.File_DownloadAsyncProgressChanged;
      this.file.DownloadAsyncFileCompleted -= this.File_DownloadAsyncFileCompleted;

      this.ended = DateTime.Now;
      TimeSpan totalTime = this.ended - this.started;
      long speed = (long)totalTime.TotalSeconds > 0 ? this.fileSize / (long)totalTime.TotalSeconds : 0;
      this.progressBar_UploadDownload.Value = this.progressBar_UploadDownload.Maximum;

      this.label_Speed.Text = string.Format("Total time: {0}, speed was {1}/s", (DateTime.Now - this.started).ToString(@"hh\:mm\:ss"), FormatSizeDisplay(speed));

      this.button_Cancel.Text = "Close";
      this.button_Cancel.Click -= this.button_Cancel_Click;
      this.button_Cancel.Click += (obj, evt) => Close();

      if (e.Error != null)
      {
        MessageBox.Show("Failed to download '" + this.file.Info.FileName + "' from Ge.tt" + Environment.NewLine + "Exception of type " + e.Error.GetType().ToString() + Environment.NewLine + e.Error.Message);
      }
    }

    private void File_DownloadAsyncProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new Action(() => File_DownloadAsyncProgressChanged(sender, e)));
        return;
      }

      long percentage = (100 * e.BytesReceived) / e.TotalBytesToReceive;
      this.label_Size.Text = string.Format("{2}% - Total: {1} - Download: {0}", FormatSizeDisplay(e.BytesReceived), FormatSizeDisplay(e.TotalBytesToReceive), percentage);

      TimeSpan elapsedTime = DateTime.Now - started;
      TimeSpan estimatedTime = TimeSpan.FromSeconds((e.TotalBytesToReceive - e.BytesReceived) / ((double)e.BytesReceived / elapsedTime.TotalSeconds));
      if (estimatedTime.Seconds > 0 && estimatedTime.Seconds % 2 == 0)
      {
        this.label_Speed.Text = string.Format("Estimated time:{0}", estimatedTime.ToString(@"hh\:mm\:ss"));
      }

      try
      {
        this.progressBar_UploadDownload.Value = (int)percentage;
      }
      catch {}
    }

    private void button_OpenFolder_Click(object sender, EventArgs e)
    {
      try
      {
        System.Diagnostics.Process.Start("explorer.exe", "/select, " + this.fileName);
      }
      catch {}
    }
    #endregion

    public static string FormatSizeDisplay(long bytes)
    {
      const int scale = 1024;
      string[] orders = new string[] { "PB", "TB", "GB", "MB", "KB", "B" };
      long max = (long)Math.Pow(scale, orders.Length - 1);
      foreach (string order in orders)
      {
        if (bytes > max)
          return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
        max /= scale;
      }
      return "0 B";
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Miralix
Denmark Denmark
Has worked as a programmer since 1999, starting with C++/MFC, Java, PHP and MySQL. Now it is all about C# (my favorite programming language), MS SQL, Azure and Xamarin (iOS/Android/WP8)

My primary work is to create applications that interacts with PABC/PBX, Microsoft Lync / UCMA.

Comments and Discussions