2013-11-14 21:01:21 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
|
|
|
|
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 Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-12-15 19:22:09 +13:00
|
|
|
|
using System.Linq;
|
2013-11-14 21:01:21 +13:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Cache;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2013-11-28 12:18:00 +13:00
|
|
|
|
public class GitHubUpdateChecker : UpdateChecker
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2013-11-15 18:30:54 +13:00
|
|
|
|
public string Owner { get; private set; }
|
|
|
|
|
public string Repo { get; private set; }
|
2014-12-15 19:22:09 +13:00
|
|
|
|
public bool IncludePreRelease { get; set; }
|
2017-11-02 04:09:19 +13:00
|
|
|
|
public bool IsPreRelease { get; private set; }
|
2013-11-28 12:18:00 +13:00
|
|
|
|
|
|
|
|
|
private const string APIURL = "https://api.github.com";
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
private string ReleasesURL => $"{APIURL}/repos/{Owner}/{Repo}/releases";
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2013-11-28 12:18:00 +13:00
|
|
|
|
public GitHubUpdateChecker(string owner, string repo)
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
|
|
|
|
Owner = owner;
|
|
|
|
|
Repo = repo;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 12:18:00 +13:00
|
|
|
|
public override void CheckUpdate()
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2013-11-14 22:31:38 +13:00
|
|
|
|
try
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
GitHubRelease latestRelease = GetLatestRelease(IncludePreRelease);
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2018-04-29 11:05:40 +12:00
|
|
|
|
if (UpdateReleaseInfo(latestRelease, IsPortable, IsPortable))
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
RefreshStatus();
|
|
|
|
|
return;
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 22:31:38 +13:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2014-06-15 22:29:09 +12:00
|
|
|
|
DebugHelper.WriteException(e, "GitHub update check failed");
|
2013-11-14 22:31:38 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 12:18:49 +12:00
|
|
|
|
Status = UpdateStatus.UpdateCheckFailed;
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
public string GetLatestDownloadURL(bool includePreRelease, bool isPortable, bool isBrowserDownloadURL)
|
2014-05-07 20:59:19 +12:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
GitHubRelease latestRelease = GetLatestRelease(includePreRelease);
|
2014-05-07 20:59:19 +12:00
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
if (UpdateReleaseInfo(latestRelease, isPortable, isBrowserDownloadURL))
|
|
|
|
|
{
|
|
|
|
|
return DownloadURL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2014-05-07 20:59:19 +12:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
DebugHelper.WriteException(e);
|
2014-05-07 20:59:19 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
private List<GitHubRelease> GetReleases()
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
using (WebClient wc = new WebClient())
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
2017-03-12 10:35:18 +13:00
|
|
|
|
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
2016-06-20 12:24:52 +12:00
|
|
|
|
wc.Proxy = Proxy;
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
string response = wc.DownloadString(ReleasesURL);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
return JsonConvert.DeserializeObject<List<GitHubRelease>>(response);
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
private GitHubRelease GetLatestRelease(bool includePreRelease)
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
GitHubRelease latestRelease = null;
|
|
|
|
|
|
|
|
|
|
List<GitHubRelease> releases = GetReleases();
|
|
|
|
|
|
|
|
|
|
if (releases != null && releases.Count > 0)
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
if (includePreRelease)
|
|
|
|
|
{
|
|
|
|
|
latestRelease = releases[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
latestRelease = releases.FirstOrDefault(x => !x.prerelease);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-28 09:04:12 +13:00
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
return latestRelease;
|
|
|
|
|
}
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2016-06-20 17:50:49 +12:00
|
|
|
|
private bool UpdateReleaseInfo(GitHubRelease release, bool isPortable, bool isBrowserDownloadURL)
|
2016-06-20 12:24:52 +12:00
|
|
|
|
{
|
|
|
|
|
if (release != null && !string.IsNullOrEmpty(release.tag_name) && release.tag_name.Length > 1 && release.tag_name[0] == 'v')
|
|
|
|
|
{
|
|
|
|
|
LatestVersion = new Version(release.tag_name.Substring(1));
|
|
|
|
|
|
|
|
|
|
if (release.assets != null && release.assets.Count > 0)
|
2013-11-14 21:01:21 +13:00
|
|
|
|
{
|
2016-06-20 12:24:52 +12:00
|
|
|
|
string endsWith;
|
|
|
|
|
|
|
|
|
|
if (isPortable)
|
|
|
|
|
{
|
|
|
|
|
endsWith = "portable.zip";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
endsWith = ".exe";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (GitHubAsset asset in release.assets)
|
|
|
|
|
{
|
|
|
|
|
if (asset != null && !string.IsNullOrEmpty(asset.name) && asset.name.EndsWith(endsWith, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Filename = asset.name;
|
|
|
|
|
|
|
|
|
|
if (isBrowserDownloadURL)
|
|
|
|
|
{
|
|
|
|
|
DownloadURL = asset.browser_download_url;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DownloadURL = asset.url;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 04:09:19 +13:00
|
|
|
|
IsPreRelease = release.prerelease;
|
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 12:24:52 +12:00
|
|
|
|
return false;
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 12:56:03 +12:00
|
|
|
|
private class GitHubRelease
|
|
|
|
|
{
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
public string assets_url { get; set; }
|
|
|
|
|
public string upload_url { get; set; }
|
|
|
|
|
public string html_url { get; set; }
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string tag_name { get; set; }
|
|
|
|
|
public string target_commitish { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string body { get; set; }
|
|
|
|
|
public bool draft { get; set; }
|
|
|
|
|
public bool prerelease { get; set; }
|
|
|
|
|
public string created_at { get; set; }
|
|
|
|
|
public string published_at { get; set; }
|
|
|
|
|
public List<GitHubAsset> assets { get; set; }
|
|
|
|
|
public string tarball_url { get; set; }
|
|
|
|
|
public string zipball_url { get; set; }
|
|
|
|
|
}
|
2013-11-14 21:01:21 +13:00
|
|
|
|
|
2016-07-31 12:56:03 +12:00
|
|
|
|
private class GitHubAsset
|
|
|
|
|
{
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string label { get; set; }
|
|
|
|
|
public string content_type { get; set; }
|
|
|
|
|
public string state { get; set; }
|
|
|
|
|
public int size { get; set; }
|
|
|
|
|
public int download_count { get; set; }
|
|
|
|
|
public string created_at { get; set; }
|
|
|
|
|
public string updated_at { get; set; }
|
|
|
|
|
public string browser_download_url { get; set; }
|
|
|
|
|
}
|
2013-11-14 21:01:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|