mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 04:37:30 +02:00
use unified http method for plugin installation + add more exceptions
#573 #610
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace Wox.Plugin.PluginManagement
|
||||
{
|
||||
public class HttpRequest
|
||||
{
|
||||
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
|
||||
|
||||
public static HttpWebResponse CreateGetHttpResponse(string url,IHttpProxy proxy)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
throw new ArgumentNullException("url");
|
||||
}
|
||||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||||
if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server))
|
||||
{
|
||||
if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password))
|
||||
{
|
||||
request.Proxy = new WebProxy(proxy.Server, proxy.Port);
|
||||
}
|
||||
else
|
||||
{
|
||||
request.Proxy = new WebProxy(proxy.Server, proxy.Port);
|
||||
request.Proxy.Credentials = new NetworkCredential(proxy.UserName, proxy.Password);
|
||||
}
|
||||
}
|
||||
request.Method = "GET";
|
||||
request.UserAgent = DefaultUserAgent;
|
||||
return request.GetResponse() as HttpWebResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Http;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Plugin.PluginManagement
|
||||
{
|
||||
@@ -108,68 +110,71 @@ namespace Wox.Plugin.PluginManagement
|
||||
List<Result> results = new List<Result>();
|
||||
string pluginName = query.SecondSearch;
|
||||
if (string.IsNullOrEmpty(pluginName)) return results;
|
||||
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy);
|
||||
Stream s = response.GetResponseStream();
|
||||
if (s != null)
|
||||
string json;
|
||||
try
|
||||
{
|
||||
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
||||
string json = reader.ReadToEnd();
|
||||
List<WoxPluginResult> searchedPlugins = null;
|
||||
try
|
||||
{
|
||||
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
|
||||
return results;
|
||||
}
|
||||
var task = Http.Get(pluginSearchUrl + pluginName, context.Proxy);
|
||||
task.RunSynchronously();
|
||||
json = task.Result;
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
Log.Warn("Can't connect to Wox plugin website, check your conenction");
|
||||
Log.Error(e);
|
||||
return new List<Result>();
|
||||
}
|
||||
List<WoxPluginResult> searchedPlugins;
|
||||
try
|
||||
{
|
||||
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
|
||||
}
|
||||
catch(JsonSerializationException e)
|
||||
{
|
||||
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
|
||||
Log.Error(e);
|
||||
return results;
|
||||
}
|
||||
|
||||
foreach (WoxPluginResult r in searchedPlugins)
|
||||
foreach (WoxPluginResult r in searchedPlugins)
|
||||
{
|
||||
WoxPluginResult r1 = r;
|
||||
results.Add(new Result
|
||||
{
|
||||
WoxPluginResult r1 = r;
|
||||
results.Add(new Result
|
||||
Title = r.name,
|
||||
SubTitle = r.description,
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = c =>
|
||||
{
|
||||
Title = r.name,
|
||||
SubTitle = r.description,
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
|
||||
"Install plugin", MessageBoxButton.YesNo);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
|
||||
"Install plugin", MessageBoxButton.YesNo);
|
||||
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
|
||||
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
|
||||
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
context.API.StartLoadingBar();
|
||||
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
|
||||
|
||||
try
|
||||
{
|
||||
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
|
||||
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
|
||||
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
|
||||
|
||||
context.API.StartLoadingBar();
|
||||
Task.Run(() =>
|
||||
{
|
||||
using (WebClient Client = new WebClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
|
||||
Client.DownloadFile(pluginUrl, filePath);
|
||||
context.API.InstallPlugin(filePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.API.StopLoadingBar();
|
||||
}
|
||||
}
|
||||
});
|
||||
Http.Download(pluginUrl, filePath, context.Proxy);
|
||||
}
|
||||
return false;
|
||||
catch (WebException e)
|
||||
{
|
||||
var info = "download plugin " + r.name + "failed.";
|
||||
MessageBox.Show(info);
|
||||
Log.Warn(info);
|
||||
Log.Error(e);
|
||||
return false;
|
||||
}
|
||||
context.API.InstallPlugin(filePath);
|
||||
context.API.StopLoadingBar();
|
||||
}
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@
|
||||
<Compile Include="..\..\SolutionAssemblyInfo.cs">
|
||||
<Link>Properties\SolutionAssemblyInfo.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="HttpRequest.cs" />
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="WoxPluginResult.cs" />
|
||||
|
||||
Reference in New Issue
Block a user