Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region imports
- using Installer.Models;
- using ReactiveUI;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- #endregion
- namespace Installer.ViewModels;
- public partial class MainWindowViewModel : ViewModelBase
- {
- #region Private Properties
- private static HttpClient _client;
- private readonly string _url = "https://api.github.com/repos/mekasu0124/Todo/releases/latest";
- #endregion
- #region Observable Properties
- public double _value;
- public string _currentInformation;
- public string _userOs;
- public string _osDownloadUrl;
- public bool _isUpdating;
- public bool _canRun;
- public bool _isUpdateFinished;
- public record VersionData(string DownloadName, string DownloadUrl, ulong DownloadSize);
- #endregion
- #region Getter and Setters
- private double Value
- {
- get => _value;
- set => this.RaiseAndSetIfChanged(ref _value, value);
- }
- private string CurrentInformation
- {
- get => _currentInformation;
- set => this.RaiseAndSetIfChanged(ref _currentInformation, value);
- }
- private string UserOs
- {
- get => _userOs;
- set => this.RaiseAndSetIfChanged(ref _userOs, value);
- }
- private string OsDownloadUrl
- {
- get => _osDownloadUrl;
- set => this.RaiseAndSetIfChanged(ref _osDownloadUrl, value);
- }
- private bool IsUpdating
- {
- get => _isUpdating;
- set => this.RaiseAndSetIfChanged(ref _isUpdating, value);
- }
- private bool CanRun
- {
- get => _canRun;
- set => this.RaiseAndSetIfChanged(ref _canRun, value);
- }
- private bool IsUpdateFinished
- {
- get => _isUpdateFinished;
- set => this.RaiseAndSetIfChanged(ref _isUpdateFinished, value);
- }
- #endregion
- public async void BeginUpdate()
- {
- var data = await GetDownloadUrl();
- if (data is { } d)
- {
- bool check = await DownloadAndExtract(d);
- if (check)
- {
- AlertUserProcessFinished();
- }
- }
- }
- SupportedOperatingSystem GetSupportedOs()
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- return SupportedOperatingSystem.Windows;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- return SupportedOperatingSystem.Linux;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- return SupportedOperatingSystem.Mac;
- throw new PlatformNotSupportedException();
- }
- async Task<VersionData?> GetDownloadUrl()
- {
- _client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
- try
- {
- var data = await _client.GetFromJsonAsync<ReleaseResponseDTO>(_url);
- var releases = data.Assets;
- var os = GetSupportedOs();
- var osReleaseData = releases.FirstOrDefault(x =>
- {
- var name = x.DownloadName;
- return os switch
- {
- SupportedOperatingSystem.Windows => name.Contains("Windows"),
- SupportedOperatingSystem.Linux => name.Contains("Linux"),
- SupportedOperatingSystem.Mac => name.Contains("Mac"),
- _ => false
- };
- });
- if (osReleaseData != null)
- return new VersionData(
- osReleaseData.DownloadName,
- osReleaseData.DownloadUrl,
- osReleaseData.DownloadSize);
- CurrentInformation = "Failed to obtain download URL for given OS.";
- return null;
- }
- catch (HttpRequestException ex)
- {
- CurrentInformation = ex.Message;
- return new VersionData("Error", ex.Message, 0);
- }
- }
- async Task<bool> DownloadAndExtract(VersionData versionData)
- {
- try
- {
- // download zip folder to current Directory
- var bytes = await _client.GetByteArrayAsync(versionData.DownloadUrl);
- await File.WriteAllBytesAsync(Directory.GetCurrentDirectory(), bytes);
- // create zip directory path
- string zipPath = (Directory.GetCurrentDirectory() + "\\" + versionData.DownloadName);
- // create temp folder for zipped files to go to
- string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
- // create extract path
- string extractToPath = Directory.GetCurrentDirectory();
- // unzip the folder to the temporary path
- ZipFile.ExtractToDirectory(zipPath, tempPath);
- // build an array of the unzipped files
- string[] files = Directory.GetFiles(tempPath);
- // iterate over each file
- foreach (string file in files)
- {
- // get file info
- FileInfo f = new FileInfo(file);
- // if file exists
- if (File.Exists(Path.Combine(extractToPath, f.Name)))
- {
- // delete existing
- File.Delete(Path.Combine(extractToPath, f.Name));
- // move new in it's place
- File.Move(f.FullName, Path.Combine(extractToPath, f.Name));
- }
- else
- {
- // otherwise, just move the new file
- File.Move(f.FullName, Path.Combine(extractToPath, f.Name));
- }
- }
- // clean up temp files
- Directory.Delete(tempPath);
- // clean up zip folder itself
- Directory.Delete(zipPath);
- IsUpdateFinished = true;
- return true;
- }
- catch (Exception ex)
- {
- CurrentInformation = ex.Message;
- return false;
- }
- }
- // used to increment progress bar
- public void IncrementProgress(double progVal)
- {
- Value += progVal;
- }
- public void AlertUserProcessFinished()
- {
- CurrentInformation = "THe Update Has Finished. Click Launch App To Open Todo App";
- CanRun = true;
- }
- public void LaunchApplication()
- {
- var userOs = GetSupportedOs();
- switch (userOs)
- {
- case SupportedOperatingSystem.Windows:
- var p = new Process();
- p.StartInfo.FileName = "Todo.exe";
- Environment.Exit(0);
- break;
- case SupportedOperatingSystem.Linux:
- // some code to launch application on linux
- Environment.Exit(0);
- break;
- case SupportedOperatingSystem.Mac:
- // some code to launch application on Mac
- Environment.Exit(0);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement