PowerToys/Wox.Core/Updater/WoxUpdateSource.cs

66 lines
2.4 KiB
C#
Raw Normal View History

2015-02-01 22:46:56 +08:00
using System;
using System.IO;
using System.Net;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Sources;
using NAppUpdate.Framework.Utils;
namespace Wox.Core.Updater
{
internal class WoxUpdateSource : IUpdateSource
{
public IWebProxy Proxy { get; set; }
public string FeedUrl { get; set; }
public WoxUpdateSource(string feedUrl,IWebProxy proxy)
{
2016-01-07 05:34:42 +08:00
FeedUrl = feedUrl;
Proxy = proxy;
2015-02-01 22:46:56 +08:00
}
private void TryResolvingHost()
{
2016-01-07 05:34:42 +08:00
Uri uri = new Uri(FeedUrl);
2015-02-01 22:46:56 +08:00
try
{
Dns.GetHostEntry(uri.Host);
}
2016-01-07 05:34:42 +08:00
catch (Exception ex)
2015-02-01 22:46:56 +08:00
{
2016-01-07 05:34:42 +08:00
throw new WebException(string.Format("Failed to resolve {0}. Check your connectivity.", uri.Host), WebExceptionStatus.ConnectFailure);
2015-02-01 22:46:56 +08:00
}
}
public string GetUpdatesFeed()
{
2016-01-07 05:34:42 +08:00
TryResolvingHost();
2015-02-01 22:46:56 +08:00
string str = string.Empty;
2016-01-07 05:34:42 +08:00
WebRequest webRequest = WebRequest.Create(FeedUrl);
2015-02-01 22:46:56 +08:00
webRequest.Method = "GET";
2016-01-07 05:34:42 +08:00
webRequest.Proxy = Proxy;
2015-02-01 22:46:56 +08:00
using (WebResponse response = webRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
using (StreamReader streamReader = new StreamReader(responseStream, true))
str = streamReader.ReadToEnd();
}
}
return str;
}
public bool GetData(string url, string baseUrl, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
{
if (!string.IsNullOrEmpty(baseUrl) && !baseUrl.EndsWith("/"))
baseUrl += "/";
FileDownloader fileDownloader = !Uri.IsWellFormedUriString(url, UriKind.Absolute) ? (!Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute) ? (string.IsNullOrEmpty(baseUrl) ? new FileDownloader(url) : new FileDownloader(new Uri(new Uri(baseUrl), url))) : new FileDownloader(new Uri(new Uri(baseUrl, UriKind.Absolute), url))) : new FileDownloader(url);
2016-01-07 05:34:42 +08:00
fileDownloader.Proxy = Proxy;
2015-02-01 22:46:56 +08:00
if (string.IsNullOrEmpty(tempLocation) || !Directory.Exists(Path.GetDirectoryName(tempLocation)))
tempLocation = Path.GetTempFileName();
return fileDownloader.DownloadToFile(tempLocation, onProgress);
}
}
}