Added backup update method if GitHub api fails

This commit is contained in:
Jaex 2013-11-28 01:18:00 +02:00
parent 1340a2d495
commit e0e3f8db7a
11 changed files with 189 additions and 142 deletions

View file

@ -193,6 +193,7 @@
<Compile Include="Cryptographic\TranslatorHelper.cs" />
<Compile Include="UnsafeBitmap.cs" />
<Compile Include="UpdateChecker\GitHubUpdateChecker.cs" />
<Compile Include="UpdateChecker\UpdateChecker.cs" />
<Compile Include="UpdateChecker\UpdaterForm.cs">
<SubType>Form</SubType>
</Compile>
@ -282,7 +283,7 @@
<Compile Include="UserControls\MyPictureBox.Designer.cs">
<DependentUpon>MyPictureBox.cs</DependentUpon>
</Compile>
<Compile Include="UpdateChecker\UpdateChecker.cs" />
<Compile Include="UpdateChecker\XMLUpdateChecker.cs" />
<Compile Include="UpdateChecker\UpdateInfo.cs" />
<Compile Include="Links.cs" />
<Compile Include="WindowState.cs" />

View file

@ -34,15 +34,12 @@ You should have received a copy of the GNU General Public License
namespace HelpersLib
{
public class GitHubUpdateChecker
public class GitHubUpdateChecker : UpdateChecker
{
private const string APIURL = "https://api.github.com";
public string Owner { get; private set; }
public string Repo { get; private set; }
public Version CurrentVersion { get; private set; }
public IWebProxy Proxy { get; set; }
public UpdateInfo UpdateInfo { get; private set; }
private const string APIURL = "https://api.github.com";
private string ReleasesURL
{
@ -52,14 +49,13 @@ private string ReleasesURL
}
}
public GitHubUpdateChecker(string owner, string repo, Version currentVersion)
public GitHubUpdateChecker(string owner, string repo)
{
Owner = owner;
Repo = repo;
CurrentVersion = currentVersion;
}
public bool CheckUpdate()
public override void CheckUpdate()
{
UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion };
@ -85,7 +81,7 @@ public bool CheckUpdate()
UpdateInfo.Filename = asset.name;
UpdateInfo.DownloadURL = asset.url;
UpdateInfo.RefreshStatus();
return true;
return;
}
}
}
@ -97,8 +93,6 @@ public bool CheckUpdate()
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;
return false;
}
private string GetDownloadURL(GitHubRelease release)

View file

@ -35,96 +35,18 @@ You should have received a copy of the GNU General Public License
namespace HelpersLib
{
public class UpdateChecker
public abstract class UpdateChecker
{
public string URL { get; private set; }
public string ApplicationName { get; private set; }
public Version ApplicationVersion { get; private set; }
public ReleaseChannelType ReleaseChannel { get; private set; }
public IWebProxy Proxy { get; private set; }
public UpdateInfo UpdateInfo { get; private set; }
public Version CurrentVersion { get; set; }
public ReleaseChannelType ReleaseType { get; set; }
public IWebProxy Proxy { get; set; }
public UpdateInfo UpdateInfo { get; protected set; }
public UpdateChecker(string url, string applicationName, Version applicationVersion,
ReleaseChannelType channel = ReleaseChannelType.Stable, IWebProxy proxy = null)
public UpdateChecker()
{
URL = url;
ApplicationName = applicationName;
ApplicationVersion = applicationVersion;
ReleaseChannel = channel;
Proxy = proxy;
ReleaseType = ReleaseChannelType.Stable;
}
public bool CheckUpdate()
{
UpdateInfo = new UpdateInfo();
UpdateInfo.ReleaseChannel = ReleaseChannel;
UpdateInfo.CurrentVersion = ApplicationVersion;
try
{
RequestCachePolicy cachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
using (WebClient wc = new WebClient { Proxy = Proxy, CachePolicy = cachePolicy })
using (MemoryStream ms = new MemoryStream(wc.DownloadData(URL)))
using (XmlTextReader xml = new XmlTextReader(ms))
{
XDocument xd = XDocument.Load(xml);
if (xd != null)
{
string node;
switch (ReleaseChannel)
{
default:
case ReleaseChannelType.Stable:
node = "Stable";
break;
case ReleaseChannelType.Beta:
node = "Beta|Stable";
break;
case ReleaseChannelType.Dev:
node = "Dev|Beta|Stable";
break;
}
string path = string.Format("Update/{0}/{1}", ApplicationName, node);
XElement xe = xd.GetNode(path);
if (xe != null)
{
UpdateInfo.LatestVersion = new Version(xe.GetValue("Version"));
UpdateInfo.DownloadURL = xe.GetValue("URL");
UpdateInfo.UpdateNotes = xe.GetValue("Summary");
UpdateInfo.RefreshStatus();
if (UpdateInfo.Status == UpdateStatus.UpdateAvailable && !string.IsNullOrEmpty(UpdateInfo.UpdateNotes) &&
UpdateInfo.UpdateNotes.IsValidUrl())
{
try
{
wc.Encoding = Encoding.UTF8;
UpdateInfo.UpdateNotes = wc.DownloadString(UpdateInfo.UpdateNotes.Trim());
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
}
}
return true;
}
}
}
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;
return false;
}
public abstract void CheckUpdate();
}
}

View file

@ -29,9 +29,11 @@ You should have received a copy of the GNU General Public License
namespace HelpersLib
{
public delegate UpdateChecker CheckUpdate();
public partial class UpdateCheckerLabel : UserControl
{
private GitHubUpdateChecker updateChecker;
private UpdateChecker updateChecker;
private bool isBusy;
public UpdateCheckerLabel()
@ -39,10 +41,8 @@ public UpdateCheckerLabel()
InitializeComponent();
}
public void CheckUpdate(GitHubUpdateChecker updateChecker)
public void CheckUpdate(CheckUpdate checkUpdate)
{
this.updateChecker = updateChecker;
if (!isBusy)
{
isBusy = true;
@ -53,13 +53,13 @@ public void CheckUpdate(GitHubUpdateChecker updateChecker)
pbLoading.Visible = true;
lblCheckingUpdates.Visible = true;
new Thread(CheckingUpdate).Start();
new Thread(() => CheckingUpdate(checkUpdate)).Start();
}
}
private void CheckingUpdate()
private void CheckingUpdate(CheckUpdate checkUpdate)
{
updateChecker.CheckUpdate();
updateChecker = checkUpdate();
UpdateControls();
isBusy = false;
}
@ -93,7 +93,7 @@ private void llblUpdateAvailable_LinkClicked(object sender, LinkLabelLinkClicked
{
if (updateChecker != null && updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable)
{
UpdaterForm updaterForm = UpdaterForm.GetGitHubUpdaterForm(updateChecker);
UpdaterForm updaterForm = new UpdaterForm(updateChecker);
updaterForm.ShowDialog();
if (updaterForm.Status == DownloaderFormStatus.InstallStarted)

View file

@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License
using HelpersLib;
using System;
using System.Text;
using System.Web;
namespace HelpersLib
{
@ -34,9 +35,27 @@ public class UpdateInfo
public UpdateStatus Status { get; set; }
public Version CurrentVersion { get; set; }
public Version LatestVersion { get; set; }
public string Filename { get; set; }
private string filename;
public string Filename
{
get
{
if (string.IsNullOrEmpty(filename))
{
return HttpUtility.UrlDecode(DownloadURL.Substring(DownloadURL.LastIndexOf('/') + 1));
}
return filename;
}
set
{
filename = value;
}
}
public string DownloadURL { get; set; }
public string UpdateNotes { get; set; }
public ReleaseChannelType ReleaseChannel { get; set; }
private bool forceUpdate = false; // For testing purposes

View file

@ -71,39 +71,20 @@ private UpdaterForm()
AutoStartInstall = true;
}
public UpdaterForm(string url, IWebProxy proxy, string filename = "", string changelog = "")
public UpdaterForm(UpdateChecker updateChecker)
: this()
{
URL = url;
Proxy = proxy;
if (string.IsNullOrEmpty(filename))
{
Filename = HttpUtility.UrlDecode(URL.Substring(URL.LastIndexOf('/') + 1));
}
else
{
Filename = filename;
}
URL = updateChecker.UpdateInfo.DownloadURL;
Proxy = updateChecker.Proxy;
Filename = updateChecker.UpdateInfo.Filename;
lblFilename.Text = "Filename: " + Filename;
if (!string.IsNullOrEmpty(changelog))
if (updateChecker is GitHubUpdateChecker)
{
Changelog = changelog;
txtChangelog.Text = Changelog;
cbShowChangelog.Visible = true;
AcceptHeader = "application/octet-stream";
}
}
public static UpdaterForm GetGitHubUpdaterForm(GitHubUpdateChecker updateChecker)
{
UpdaterForm updaterForm = new UpdaterForm(updateChecker.UpdateInfo.DownloadURL, updateChecker.Proxy,
updateChecker.UpdateInfo.Filename, updateChecker.UpdateInfo.UpdateNotes);
updaterForm.AcceptHeader = "application/octet-stream";
return updaterForm;
}
private void UpdaterForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

View file

@ -0,0 +1,106 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2013 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using HelpersLib;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace HelpersLib
{
public class XMLUpdateChecker : UpdateChecker
{
public string URL { get; private set; }
public string ApplicationName { get; private set; }
public XMLUpdateChecker(string url, string applicationName)
{
URL = url;
ApplicationName = applicationName;
}
public override void CheckUpdate()
{
UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion, ReleaseChannel = ReleaseType };
try
{
using (WebClient wc = new WebClient())
{
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
wc.Headers.Add("user-agent", "ShareX");
wc.Proxy = Proxy;
using (MemoryStream ms = new MemoryStream(wc.DownloadData(URL)))
using (XmlTextReader xml = new XmlTextReader(ms))
{
XDocument xd = XDocument.Load(xml);
if (xd != null)
{
string node;
switch (ReleaseType)
{
default:
case ReleaseChannelType.Stable:
node = "Stable";
break;
case ReleaseChannelType.Beta:
node = "Beta|Stable";
break;
case ReleaseChannelType.Dev:
node = "Dev|Beta|Stable";
break;
}
string path = string.Format("Update/{0}/{1}", ApplicationName, node);
XElement xe = xd.GetNode(path);
if (xe != null)
{
UpdateInfo.LatestVersion = new Version(xe.Element("Version").Value);
UpdateInfo.DownloadURL = xe.Element("URL").Value;
UpdateInfo.RefreshStatus();
return;
}
}
}
}
}
catch (Exception e)
{
DebugHelper.WriteException(e, "Update check failed");
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;
}
}
}

View file

@ -43,10 +43,7 @@ public AboutForm()
Text = Program.FullTitle;
lblProductName.Text = Program.FullTitle;
GitHubUpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX", Program.AssemblyVersion);
updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy();
uclUpdate.CheckUpdate(updateChecker);
uclUpdate.CheckUpdate(TaskHelpers.CheckUpdate);
rtbShareXInfo.AddContextMenu();
rtbCredits.AddContextMenu();

View file

@ -411,15 +411,13 @@ private void UpdateUploaderMenuNames()
private void CheckUpdate()
{
GitHubUpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX", Program.AssemblyVersion);
updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy();
updateChecker.CheckUpdate();
UpdateChecker updateChecker = TaskHelpers.CheckUpdate();
if (updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable &&
MessageBox.Show("An update is available for ShareX.\r\nWould you like to download it?", "ShareX",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
UpdaterForm updaterForm = UpdaterForm.GetGitHubUpdaterForm(updateChecker);
UpdaterForm updaterForm = new UpdaterForm(updateChecker);
updaterForm.ShowDialog();
if (updaterForm.Status == DownloaderFormStatus.InstallStarted)

View file

@ -34,6 +34,7 @@ You should have received a copy of the GNU General Public License
using System.IO;
using System.Media;
using System.Windows.Forms;
using UploadersLib;
namespace ShareX
{
@ -317,6 +318,25 @@ public static Icon GetProgressIcon(int percentage)
return Icon.FromHandle(bmp.GetHicon());
}
}
public static UpdateChecker CheckUpdate()
{
UpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX");
updateChecker.CurrentVersion = Program.AssemblyVersion;
updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy();
updateChecker.CheckUpdate();
// Backup if GitHub API fails
if (updateChecker.UpdateInfo == null || updateChecker.UpdateInfo.Status == UpdateStatus.UpdateCheckFailed)
{
updateChecker = new XMLUpdateChecker("https://raw.github.com/ShareX/ShareX/master/Update.xml", "ShareX");
updateChecker.CurrentVersion = Program.AssemblyVersion;
updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy();
updateChecker.CheckUpdate();
}
return updateChecker;
}
}
public class PointInfo

9
Update.xml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<Update>
<ShareX>
<Stable>
<Version>8.4.0</Version>
<URL>https://github.com/ShareX/ShareX/releases/download/v8.4.0/ShareX-8.4.0-setup.exe</URL>
</Stable>
</ShareX>
</Update>