ShareX/ShareX.HelpersLib/FileDownloader.cs

198 lines
6.5 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
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 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;
2023-10-28 18:15:24 +13:00
using System.Net.Http;
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
{
2023-10-30 01:05:47 +13:00
public event Action FileSizeReceived;
public event Action ProgressChanged;
2023-10-29 18:50:58 +13:00
public string URL { get; set; }
public string DownloadLocation { get; set; }
public string AcceptHeader { get; set; }
2013-11-03 23:53:49 +13:00
public bool IsDownloading { get; private set; }
public bool IsCanceled { get; private set; }
2023-10-30 01:05:47 +13:00
public long FileSize { get; private set; } = -1;
2013-11-03 23:53:49 +13:00
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;
}
}
2017-05-27 22:05:07 +12:00
private const int bufferSize = 32768;
2013-11-03 23:53:49 +13:00
2023-10-29 18:50:58 +13:00
public FileDownloader()
{
}
2023-10-28 18:15:24 +13:00
public FileDownloader(string url, string downloadLocation)
2013-11-03 23:53:49 +13:00
{
URL = url;
DownloadLocation = downloadLocation;
2013-11-03 23:53:49 +13:00
}
2023-10-30 01:05:47 +13:00
public async Task<bool> StartDownload()
2013-11-03 23:53:49 +13:00
{
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;
2023-10-30 01:05:47 +13:00
FileSize = -1;
2023-10-29 18:50:58 +13:00
DownloadedSize = 0;
DownloadSpeed = 0;
2019-05-08 22:38:12 +12:00
2023-10-30 01:05:47 +13:00
return await DoWork();
2013-11-03 23:53:49 +13:00
}
2023-10-30 01:05:47 +13:00
return false;
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
}
2023-10-30 01:05:47 +13:00
private async Task<bool> DoWork()
2016-12-27 06:23:29 +13:00
{
2013-11-03 23:53:49 +13:00
try
{
2023-10-28 18:15:24 +13:00
HttpClient client = HttpClientFactory.Create();
2023-10-28 18:15:24 +13:00
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, URL))
{
2023-10-28 18:15:24 +13:00
if (!string.IsNullOrEmpty(AcceptHeader))
2013-11-03 23:53:49 +13:00
{
2023-10-28 18:15:24 +13:00
requestMessage.Headers.Accept.ParseAdd(AcceptHeader);
}
2016-12-27 06:23:29 +13:00
2023-10-28 18:15:24 +13:00
using (HttpResponseMessage responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead))
{
responseMessage.EnsureSuccessStatusCode();
2016-12-27 06:23:29 +13:00
2023-10-30 01:05:47 +13:00
FileSize = responseMessage.Content.Headers.ContentLength ?? -1;
2016-12-27 06:23:29 +13:00
2023-10-28 18:15:24 +13:00
FileSizeReceived?.Invoke();
2016-12-27 06:23:29 +13:00
2023-10-28 18:15:24 +13:00
if (FileSize > 0)
{
Stopwatch timer = new Stopwatch();
Stopwatch progressEventTimer = new Stopwatch();
long speedTest = 0;
2016-12-27 06:23:29 +13:00
2023-10-28 18:15:24 +13:00
byte[] buffer = new byte[(int)Math.Min(bufferSize, FileSize)];
int bytesRead;
2016-12-27 06:23:29 +13:00
2023-10-28 18:15:24 +13:00
using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
using (FileStream fileStream = new FileStream(DownloadLocation, FileMode.Create, FileAccess.Write, FileShare.Read))
{
while (DownloadedSize < FileSize && !IsCanceled)
2016-12-27 06:23:29 +13:00
{
2023-10-28 18:15:24 +13:00
if (!timer.IsRunning)
{
timer.Start();
}
if (!progressEventTimer.IsRunning)
{
progressEventTimer.Start();
}
bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);
await fileStream.WriteAsync(buffer, 0, bytesRead);
DownloadedSize += bytesRead;
speedTest += bytesRead;
if (timer.ElapsedMilliseconds > 500)
{
DownloadSpeed = (double)speedTest / timer.ElapsedMilliseconds * 1000;
speedTest = 0;
timer.Reset();
}
if (progressEventTimer.ElapsedMilliseconds > 100)
{
ProgressChanged?.Invoke();
progressEventTimer.Reset();
}
2016-12-27 06:23:29 +13:00
}
2023-10-30 01:05:47 +13:00
ProgressChanged?.Invoke();
2016-12-27 06:23:29 +13:00
}
2021-01-08 23:26:33 +13:00
2023-10-30 01:05:47 +13:00
return true;
2023-10-28 18:15:24 +13:00
}
2013-11-03 23:53:49 +13:00
}
}
}
2023-10-28 18:15:24 +13:00
catch (Exception e)
2013-11-03 23:53:49 +13:00
{
if (!IsCanceled)
{
2023-10-29 18:50:58 +13:00
throw e;
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
}
2023-10-30 01:05:47 +13:00
return false;
2013-11-03 23:53:49 +13:00
}
}
}