|
|
@ -3,6 +3,7 @@ using System.Diagnostics; |
|
|
|
using System.IO; |
|
|
|
using System.IO; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
|
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Refit; |
|
|
|
using Refit; |
|
|
|
using StabilityMatrix.Api; |
|
|
|
using StabilityMatrix.Api; |
|
|
@ -30,8 +31,19 @@ public class A3WebUI: BasePackage |
|
|
|
using var client = new HttpClient {Timeout = TimeSpan.FromMinutes(5)}; |
|
|
|
using var client = new HttpClient {Timeout = TimeSpan.FromMinutes(5)}; |
|
|
|
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("StabilityMatrix", "1.0")); |
|
|
|
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("StabilityMatrix", "1.0")); |
|
|
|
await using var file = new FileStream(DownloadLocation, FileMode.Create, FileAccess.Write, FileShare.None); |
|
|
|
await using var file = new FileStream(DownloadLocation, FileMode.Create, FileAccess.Write, FileShare.None); |
|
|
|
using var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead); |
|
|
|
|
|
|
|
var length = response.Content.Headers.ContentLength; |
|
|
|
long contentLength = 0; |
|
|
|
|
|
|
|
var retryCount = 0; |
|
|
|
|
|
|
|
var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead); |
|
|
|
|
|
|
|
while (contentLength == 0 && retryCount++ < 5) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead); |
|
|
|
|
|
|
|
contentLength = response.Content.Headers.ContentLength ?? 0; |
|
|
|
|
|
|
|
Debug.WriteLine("Retrying..."); |
|
|
|
|
|
|
|
Thread.Sleep(50); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var isIndeterminate = contentLength == 0; |
|
|
|
|
|
|
|
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(); |
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(); |
|
|
|
var totalBytesRead = 0; |
|
|
|
var totalBytesRead = 0; |
|
|
@ -44,9 +56,16 @@ public class A3WebUI: BasePackage |
|
|
|
|
|
|
|
|
|
|
|
totalBytesRead += bytesRead; |
|
|
|
totalBytesRead += bytesRead; |
|
|
|
|
|
|
|
|
|
|
|
var progress = (int) (totalBytesRead * 100d / length); |
|
|
|
if (isIndeterminate) |
|
|
|
Debug.WriteLine($"Progress; {progress}"); |
|
|
|
{ |
|
|
|
OnDownloadProgressChanged(progress); |
|
|
|
OnDownloadProgressChanged(-1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var progress = (int) (totalBytesRead * 100d / contentLength); |
|
|
|
|
|
|
|
Debug.WriteLine($"Progress; {progress}"); |
|
|
|
|
|
|
|
OnDownloadProgressChanged(progress); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await file.FlushAsync(); |
|
|
|
await file.FlushAsync(); |
|
|
|