ShareX/ShareX.HelpersLib/FileDownloader.cs

198 lines
6.5 KiB
C#
Raw Normal View History

2013-11-03 12:53:49 +02:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2025-01-08 03:46:27 +03:00
Copyright (c) 2007-2025 ShareX Team
2013-11-03 12:53:49 +02: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 08:15:24 +03:00
using System.Net.Http;
using System.Threading.Tasks;
2013-11-03 12:53:49 +02:00
2014-12-10 22:25:20 +02:00
namespace ShareX.HelpersLib
2013-11-03 12:53:49 +02:00
{
public class FileDownloader
{
2023-10-29 15:05:47 +03:00
public event Action FileSizeReceived;
public event Action ProgressChanged;
2023-10-29 08:50:58 +03:00
public string URL { get; set; }
public string DownloadLocation { get; set; }
public string AcceptHeader { get; set; }
2013-11-03 12:53:49 +02:00
public bool IsDownloading { get; private set; }
public bool IsCanceled { get; private set; }
2023-10-29 15:05:47 +03:00
public long FileSize { get; private set; } = -1;
2013-11-03 12:53:49 +02: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 13:05:07 +03:00
private const int bufferSize = 32768;
2013-11-03 12:53:49 +02:00
2023-10-29 08:50:58 +03:00
public FileDownloader()
{
}
2023-10-28 08:15:24 +03:00
public FileDownloader(string url, string downloadLocation)
2013-11-03 12:53:49 +02:00
{
URL = url;
DownloadLocation = downloadLocation;
2013-11-03 12:53:49 +02:00
}
2023-10-29 15:05:47 +03:00
public async Task<bool> StartDownload()
2013-11-03 12:53:49 +02:00
{
if (!IsDownloading && !string.IsNullOrEmpty(URL))
2013-11-03 12:53:49 +02:00
{
IsDownloading = true;
2019-05-08 13:38:12 +03:00
IsCanceled = false;
2023-10-29 15:05:47 +03:00
FileSize = -1;
2023-10-29 08:50:58 +03:00
DownloadedSize = 0;
DownloadSpeed = 0;
2019-05-08 13:38:12 +03:00
2023-10-29 15:05:47 +03:00
return await DoWork();
2013-11-03 12:53:49 +02:00
}
2023-10-29 15:05:47 +03:00
return false;
2013-11-03 12:53:49 +02:00
}
2019-05-08 13:38:12 +03:00
public void StopDownload()
2013-11-03 12:53:49 +02:00
{
2019-05-08 13:38:12 +03:00
IsCanceled = true;
2013-11-03 12:53:49 +02:00
}
2023-10-29 15:05:47 +03:00
private async Task<bool> DoWork()
2016-12-26 20:23:29 +03:00
{
2013-11-03 12:53:49 +02:00
try
{
2023-10-28 08:15:24 +03:00
HttpClient client = HttpClientFactory.Create();
2023-10-28 08:15:24 +03:00
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, URL))
{
2023-10-28 08:15:24 +03:00
if (!string.IsNullOrEmpty(AcceptHeader))
2013-11-03 12:53:49 +02:00
{
2023-10-28 08:15:24 +03:00
requestMessage.Headers.Accept.ParseAdd(AcceptHeader);
}
2016-12-26 20:23:29 +03:00
2023-10-28 08:15:24 +03:00
using (HttpResponseMessage responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead))
{
responseMessage.EnsureSuccessStatusCode();
2016-12-26 20:23:29 +03:00
2023-10-29 15:05:47 +03:00
FileSize = responseMessage.Content.Headers.ContentLength ?? -1;
2016-12-26 20:23:29 +03:00
2023-10-28 08:15:24 +03:00
FileSizeReceived?.Invoke();
2016-12-26 20:23:29 +03:00
2023-10-28 08:15:24 +03:00
if (FileSize > 0)
{
Stopwatch timer = new Stopwatch();
Stopwatch progressEventTimer = new Stopwatch();
long speedTest = 0;
2016-12-26 20:23:29 +03:00
2023-10-28 08:15:24 +03:00
byte[] buffer = new byte[(int)Math.Min(bufferSize, FileSize)];
int bytesRead;
2016-12-26 20:23:29 +03:00
2023-10-28 08:15:24 +03: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-26 20:23:29 +03:00
{
2023-10-28 08:15:24 +03: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-26 20:23:29 +03:00
}
2023-10-29 15:05:47 +03:00
ProgressChanged?.Invoke();
2016-12-26 20:23:29 +03:00
}
2021-01-08 13:26:33 +03:00
2023-10-29 15:05:47 +03:00
return true;
2023-10-28 08:15:24 +03:00
}
2013-11-03 12:53:49 +02:00
}
}
}
2023-10-28 08:15:24 +03:00
catch (Exception e)
2013-11-03 12:53:49 +02:00
{
if (!IsCanceled)
{
2023-10-29 08:50:58 +03:00
throw e;
2013-11-03 12:53:49 +02:00
}
}
finally
{
if (IsCanceled)
{
try
{
if (File.Exists(DownloadLocation))
{
File.Delete(DownloadLocation);
}
}
catch
{
}
}
2013-11-03 12:53:49 +02:00
IsDownloading = false;
2013-11-03 12:53:49 +02:00
}
2023-10-29 15:05:47 +03:00
return false;
2013-11-03 12:53:49 +02:00
}
}
}