Files
UtilitatPdf/UtilitatPdf/Updater/Updater.cs
2021-01-07 12:27:49 +01:00

151 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Path = System.IO.Path;
namespace UtilitatPdf.CheckUpdates
{
class Updater
{
public bool IsNewerVersion { get; private set; }
public string NewReleaseVersion { get; private set; }
public float DownloadProgress { get; private set; }
public long BytesReceived { get; private set; }
public long RemainingBytes { get; private set; }
public string UserState { get; private set; }
public bool DownloadComplete { get { return _downloadComplete; } private set { value = _downloadComplete; } }
public string CurrentVersion { get { return _currentVersion; } }
private static string _currentVersion = "1.3";
private string _downloadRelease = _currentVersion;
private static HttpClient Client = new HttpClient();
private Uri _urlDescarga = null;
private bool _downloadComplete = false;
private List<Gitea> llistaIds = new List<Gitea>();
private static string self = Process.GetCurrentProcess().MainModule.FileName;
private static string selfFileName = Path.GetFileName(self);
private static string selfWithoutExt = Path.Combine(Path.GetDirectoryName(self), Path.GetFileNameWithoutExtension(self));
public Updater()
{
this.GetItems();
this.CheckForUpdates();
}
private void CheckForUpdates()
{
foreach (Gitea releases in llistaIds)
{
var version1 = new Version(_downloadRelease);
var version2 = new Version(releases.TagName);
var result = version1.CompareTo(version2);
if (result < 0)
{
_downloadRelease = releases.TagName;
List<Asset> assets = releases.Assets;
foreach (Asset asset in assets)
{
_urlDescarga = asset.BrowserDownloadUrl;
}
}
}
if (_currentVersion != _downloadRelease)
{
IsNewerVersion = true;
NewReleaseVersion = _downloadRelease;
}
}
private void GetItems()
{
string url = $"https://git.vconesa.net/api/v1/repos/amorfo77/UtilitatPdf/releases";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Accept = "application/json";
try
{
using (WebResponse response = request.GetResponse())
{
using (Stream strReader = response.GetResponseStream())
{
if (strReader == null) return;
using (StreamReader objReader = new StreamReader(strReader))
{
string responseBody = objReader.ReadToEnd();
llistaIds = Gitea.FromJson(responseBody);
}
}
}
}
catch (WebException)
{
throw new WebException();
}
}
public async Task DownloadReleaseAsync()
{
_downloadComplete = false;
try
{
var response = await Client.GetAsync(_urlDescarga);
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
{
var fileInfo = new FileInfo(selfWithoutExt + "Update.exe");
using (var fileStream = fileInfo.OpenWrite())
{
await stream.CopyToAsync(fileStream);
}
}
_downloadComplete = true;
}
catch (Exception)
{
throw new Exception();
}
}
public void UpdateSelf()
{
if (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX)
{
Process.Start(self);
// Sleep for half a second to avoid an exception
Thread.Sleep(500);
Environment.Exit(0);
}
else
{
using (var batFile = new StreamWriter(File.Create(selfWithoutExt + "Update.bat")))
{
batFile.WriteLine("@ECHO OFF");
batFile.WriteLine("TIMEOUT /t 1 /nobreak > NUL");
batFile.WriteLine("TASKKILL /IM \"{0}\" > NUL", selfFileName);
batFile.WriteLine("MOVE \"{0}\" \"{1}\"", selfWithoutExt + "Update.exe", self);
batFile.WriteLine("DEL \"%~f0\" & START \"\" /B \"{0}\"", self);
}
ProcessStartInfo startInfo = new ProcessStartInfo(selfWithoutExt + "Update.bat");
// Hide the terminal window
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetDirectoryName(self);
Process.Start(startInfo);
Environment.Exit(0);
}
}
}
}