2022-07-09 00:33:55 -04:00
|
|
|
|
using System;
|
2022-05-30 13:04:54 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net.Http;
|
2022-05-30 00:49:58 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-07 20:34:09 +01:00
|
|
|
|
using HttpClientProgress;
|
2022-07-09 00:33:55 -04:00
|
|
|
|
using SPT_AKI_Installer.Aki.Core.Model;
|
2022-05-30 00:49:58 +01:00
|
|
|
|
|
|
|
|
|
namespace SPT_AKI_Installer.Aki.Helper
|
|
|
|
|
{
|
2022-05-30 13:04:54 +01:00
|
|
|
|
public static class DownloadHelper
|
2022-05-30 00:49:58 +01:00
|
|
|
|
{
|
2022-07-09 00:33:55 -04:00
|
|
|
|
private static HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromHours(1) };
|
2022-05-30 13:04:54 +01:00
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
public static async Task<GenericResult> DownloadFile(FileInfo outputFile, string targetLink, IProgress<double> progress)
|
2022-05-30 13:04:54 +01:00
|
|
|
|
{
|
2022-06-06 15:55:46 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-07-09 00:33:55 -04:00
|
|
|
|
outputFile.Refresh();
|
2022-05-30 13:04:54 +01:00
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
if (outputFile.Exists) outputFile.Delete();
|
2022-05-30 13:04:54 +01:00
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
// Use the provided extension method
|
|
|
|
|
using (var file = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
|
|
|
await _httpClient.DownloadDataAsync(targetLink, file, progress);
|
2022-06-06 15:55:46 +01:00
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
outputFile.Refresh();
|
2022-06-06 15:55:46 +01:00
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
if(!outputFile.Exists)
|
2022-06-06 15:55:46 +01:00
|
|
|
|
{
|
2022-07-09 00:33:55 -04:00
|
|
|
|
return GenericResult.FromError($"Failed to download {outputFile.Name}");
|
2022-06-06 15:55:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-09 00:33:55 -04:00
|
|
|
|
return GenericResult.FromSuccess();
|
2022-06-06 15:55:46 +01:00
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
catch(Exception ex)
|
2022-05-30 13:04:54 +01:00
|
|
|
|
{
|
2022-07-09 00:33:55 -04:00
|
|
|
|
return GenericResult.FromError(ex.Message);
|
2022-05-30 13:04:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-30 00:49:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|