ShareX/ShareX.HelpersLib/FileDownloader.cs

219 lines
7.2 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public class FileDownloader
{
public event EventHandler FileSizeReceived, DownloadStarted, ProgressChanged, DownloadCompleted, ExceptionThrown;
2013-11-03 23:53:49 +13:00
public string URL { get; private set; }
public string DownloadLocation { get; private set; }
2013-11-03 23:53:49 +13:00
public bool IsDownloading { get; private set; }
public bool IsCanceled { get; private set; }
2019-05-08 22:38:12 +12:00
public bool IsPaused { get; private set; }
2013-11-03 23:53:49 +13:00
public long FileSize { get; private set; }
public long DownloadedSize { get; private set; }
public double DownloadSpeed { get; private set; }
public double DownloadPercentage
{
get
{
if (FileSize > 0)
{
return (double)DownloadedSize / FileSize * 100;
}
return 0;
}
}
public IWebProxy Proxy { get; set; }
public string AcceptHeader { get; set; }
2019-05-08 22:38:12 +12:00
public Exception LastException { get; private set; }
2013-11-03 23:53:49 +13:00
2017-05-27 22:05:07 +12:00
private const int bufferSize = 32768;
2013-11-03 23:53:49 +13:00
public FileDownloader(string url, string downloadLocation, IWebProxy proxy = null, string acceptHeader = null)
2013-11-03 23:53:49 +13:00
{
URL = url;
DownloadLocation = downloadLocation;
2013-11-03 23:53:49 +13:00
Proxy = proxy;
AcceptHeader = acceptHeader;
2013-11-03 23:53:49 +13:00
}
public void StartDownload()
{
if (!IsDownloading && !string.IsNullOrEmpty(URL))
2013-11-03 23:53:49 +13:00
{
IsDownloading = true;
2019-05-08 22:38:12 +12:00
IsCanceled = false;
2013-11-03 23:53:49 +13:00
IsPaused = false;
2019-05-08 22:38:12 +12:00
Progress<EventHandler> progress = new Progress<EventHandler>(OnProgressChanged);
Task.Run(() => DoWork(progress));
2013-11-03 23:53:49 +13:00
}
}
2019-05-08 22:38:12 +12:00
public void StopDownload()
2013-11-03 23:53:49 +13:00
{
2019-05-08 22:38:12 +12:00
IsCanceled = true;
2013-11-03 23:53:49 +13:00
}
public void PauseDownload()
{
2019-05-08 22:38:12 +12:00
IsPaused = true;
2013-11-03 23:53:49 +13:00
}
2019-05-08 22:38:12 +12:00
public void ResumeDownload()
2013-11-03 23:53:49 +13:00
{
2019-05-08 22:38:12 +12:00
IsPaused = false;
2013-11-03 23:53:49 +13:00
}
private void OnProgressChanged(EventHandler eventHandler)
2013-11-03 23:53:49 +13:00
{
eventHandler?.Invoke(this, EventArgs.Empty);
2016-12-27 06:23:29 +13:00
}
2013-11-03 23:53:49 +13:00
private void DoWork(IProgress<EventHandler> progress)
2016-12-27 06:23:29 +13:00
{
2013-11-03 23:53:49 +13:00
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = ShareXResources.UserAgent;
2013-11-03 23:53:49 +13:00
request.Proxy = Proxy;
if (!string.IsNullOrEmpty(AcceptHeader))
{
request.Accept = AcceptHeader;
}
2016-12-27 06:23:29 +13:00
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
2013-11-03 23:53:49 +13:00
{
2016-12-27 06:23:29 +13:00
FileSize = response.ContentLength;
2013-11-03 23:53:49 +13:00
progress.Report(FileSizeReceived);
2013-11-03 23:53:49 +13:00
2016-12-27 06:23:29 +13:00
if (FileSize > 0)
2013-11-03 23:53:49 +13:00
{
2016-12-27 06:23:29 +13:00
Stopwatch timer = new Stopwatch();
Stopwatch progressEventTimer = new Stopwatch();
long speedTest = 0;
2013-11-03 23:53:49 +13:00
2016-12-27 06:23:29 +13:00
byte[] buffer = new byte[(int)Math.Min(bufferSize, FileSize)];
int bytesRead;
2013-11-03 23:53:49 +13:00
using (FileStream fs = new FileStream(DownloadLocation, FileMode.Create, FileAccess.Write, FileShare.Read))
2016-12-27 06:23:29 +13:00
using (Stream responseStream = response.GetResponseStream())
2013-11-03 23:53:49 +13:00
{
progress.Report(DownloadStarted);
2016-12-27 06:23:29 +13:00
while (DownloadedSize < FileSize && !IsCanceled)
2016-12-27 06:23:29 +13:00
{
while (IsPaused && !IsCanceled)
{
timer.Reset();
Thread.Sleep(10);
}
if (IsCanceled)
{
return;
}
if (!timer.IsRunning)
{
timer.Start();
}
if (!progressEventTimer.IsRunning)
{
progressEventTimer.Start();
}
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
2016-12-27 06:23:29 +13:00
DownloadedSize += bytesRead;
speedTest += bytesRead;
if (timer.ElapsedMilliseconds > 500)
{
DownloadSpeed = (double)speedTest / timer.ElapsedMilliseconds * 1000;
speedTest = 0;
timer.Reset();
}
if (progressEventTimer.ElapsedMilliseconds > 100)
{
progress.Report(ProgressChanged);
2016-12-27 06:23:29 +13:00
progressEventTimer.Reset();
}
}
2013-11-03 23:53:49 +13:00
}
2021-01-08 23:26:33 +13:00
progress.Report(ProgressChanged);
progress.Report(DownloadCompleted);
2013-11-03 23:53:49 +13:00
}
}
}
catch (Exception ex)
{
if (!IsCanceled)
{
LastException = ex;
2016-12-27 06:23:29 +13:00
progress.Report(ExceptionThrown);
2013-11-03 23:53:49 +13:00
}
}
finally
{
if (IsCanceled)
{
try
{
if (File.Exists(DownloadLocation))
{
File.Delete(DownloadLocation);
}
}
catch
{
}
}
2013-11-03 23:53:49 +13:00
IsDownloading = false;
2013-11-03 23:53:49 +13:00
}
}
}
}