Use HttpClient in DownloadFileAsync function

This commit is contained in:
Jaex 2023-10-27 10:19:36 +03:00
parent d32a050db7
commit 1c29fbe022
3 changed files with 36 additions and 31 deletions

View file

@ -618,26 +618,28 @@ public static string GetFileNameFromWebServer(string url)
return fileName;
}
public static void DownloadFile(string url, string filePath)
public static async Task DownloadFileAsync(string url, string filePath)
{
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(filePath))
{
FileHelpers.CreateDirectoryFromFilePath(filePath);
using (WebClient wc = new WebClient())
HttpClient client = HttpClientFactory.Create();
using (HttpResponseMessage responseMessage = await client.GetAsync(url))
{
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
wc.DownloadFile(url, filePath);
if (responseMessage.IsSuccessStatusCode)
{
using (Stream stream = await responseMessage.Content.ReadAsStreamAsync())
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
await stream.CopyToAsync(fileStream);
}
}
}
}
}
public static async Task DownloadFileAsync(string url, string filePath)
{
await Task.Run(() => DownloadFile(url, filePath));
}
public static async Task<string> DownloadStringAsync(string url)
{
string response = null;
@ -662,35 +664,38 @@ public static async Task<Bitmap> DownloadImageAsync(string url)
{
Bitmap bmp = null;
HttpClient client = HttpClientFactory.Create();
using (HttpResponseMessage responseMessage = await client.GetAsync(url))
if (!string.IsNullOrEmpty(url))
{
if (responseMessage.IsSuccessStatusCode && responseMessage.Content.Headers.ContentType != null)
{
string mediaType = responseMessage.Content.Headers.ContentType.MediaType;
HttpClient client = HttpClientFactory.Create();
string[] supportedImageTypes = new string[]
using (HttpResponseMessage responseMessage = await client.GetAsync(url))
{
if (responseMessage.IsSuccessStatusCode && responseMessage.Content.Headers.ContentType != null)
{
string mediaType = responseMessage.Content.Headers.ContentType.MediaType;
string[] supportedImageTypes = new string[]
{
"image/png",
"image/jpeg",
"image/gif",
"image/bmp",
"image/tiff"
};
};
if (supportedImageTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
{
byte[] data = await responseMessage.Content.ReadAsByteArrayAsync();
MemoryStream ms = new MemoryStream(data);
if (supportedImageTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
{
byte[] data = await responseMessage.Content.ReadAsByteArrayAsync();
MemoryStream memoryStream = new MemoryStream(data);
try
{
bmp = new Bitmap(ms);
}
catch
{
ms.Dispose();
try
{
bmp = new Bitmap(memoryStream);
}
catch
{
memoryStream.Dispose();
}
}
}
}

View file

@ -425,7 +425,7 @@ private static void DownloadFFmpeg()
string filePath = Path.Combine(OutputDir, fileName);
Console.WriteLine("Downloading: " + FFmpegDownloadURL);
URLHelpers.DownloadFile(FFmpegDownloadURL, filePath);
URLHelpers.DownloadFileAsync(FFmpegDownloadURL, filePath).GetAwaiter().GetResult();
Console.WriteLine("Extracting: " + filePath);
ZipManager.Extract(filePath, OutputDir, false, entry => entry.Name.Equals("ffmpeg.exe", StringComparison.OrdinalIgnoreCase));

View file

@ -1092,7 +1092,7 @@ private bool DownloadFromURL(bool upload)
try
{
URLHelpers.DownloadFile(url, Info.FilePath);
URLHelpers.DownloadFileAsync(url, Info.FilePath).GetAwaiter().GetResult();
if (upload)
{