Change Download to DownloadAsync

Add error handling for download as well
This commit is contained in:
弘韬 张 2021-01-04 10:31:05 +08:00
parent a8dc1599d7
commit bc63b9ebfa
2 changed files with 17 additions and 9 deletions

View file

@ -75,17 +75,25 @@ namespace Flow.Launcher.Infrastructure.Http
};
}
public static async Task Download([NotNull] string url, [NotNull] string filePath)
public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath)
{
using var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK)
try
{
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream);
using var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK)
{
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream);
}
else
{
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
}
}
else
catch (HttpRequestException e)
{
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
Log.Exception("Infrastructure.Http", "Http Request Error", e, "DownloadAsync");
throw;
}
}

View file

@ -127,7 +127,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
await Http.Download(plugin.UrlDownload, filePath).ConfigureAwait(false);
await Http.DownloadAsync(plugin.UrlDownload, filePath).ConfigureAwait(false);
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
@ -217,7 +217,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
await Http.Download(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false);
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false);
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));