mekasu0124

Untitled

Jan 26th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. #region imports
  2. using Installer.Models;
  3. using ReactiveUI;
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Net.Http.Json;
  12. using System.Runtime.InteropServices;
  13. using System.Threading.Tasks;
  14. #endregion
  15.  
  16. namespace Installer.ViewModels;
  17.  
  18. public partial class MainWindowViewModel : ViewModelBase
  19. {
  20.     #region Private Properties
  21.     private static HttpClient _client;
  22.     private readonly string _url = "https://api.github.com/repos/mekasu0124/Todo/releases/latest";
  23.     #endregion
  24.  
  25.     #region Observable Properties
  26.     public ulong _value;
  27.     public string _currentInformation;
  28.     public string _userOs;
  29.     public string _osDownloadUrl;
  30.     public bool _isUpdating;
  31.     public bool _canRun;
  32.     public bool _isUpdateFinished;
  33.     public record VersionData(string DownloadName, string DownloadUrl, ulong DownloadSize);
  34.     #endregion
  35.  
  36.     #region Getter and Setters
  37.     private ulong Value
  38.     {
  39.         get => _value;
  40.         set => this.RaiseAndSetIfChanged(ref _value, value);
  41.     }
  42.  
  43.     private string CurrentInformation
  44.     {
  45.         get => _currentInformation;
  46.         set => this.RaiseAndSetIfChanged(ref _currentInformation, value);
  47.     }
  48.  
  49.     private string UserOs
  50.     {
  51.         get => _userOs;
  52.         set => this.RaiseAndSetIfChanged(ref _userOs, value);
  53.     }
  54.  
  55.     private string OsDownloadUrl
  56.     {
  57.         get => _osDownloadUrl;
  58.         set => this.RaiseAndSetIfChanged(ref _osDownloadUrl, value);
  59.     }
  60.  
  61.     private bool IsUpdating
  62.     {
  63.         get => _isUpdating;
  64.         set => this.RaiseAndSetIfChanged(ref _isUpdating, value);
  65.     }
  66.  
  67.     private bool CanRun
  68.     {
  69.         get => _canRun;
  70.         set => this.RaiseAndSetIfChanged(ref _canRun, value);
  71.     }
  72.  
  73.     private bool IsUpdateFinished
  74.     {
  75.         get => _isUpdateFinished;
  76.         set => this.RaiseAndSetIfChanged(ref _isUpdateFinished, value);
  77.     }
  78.     #endregion
  79.  
  80.     public async void BeginUpdate()
  81.     {
  82.         var data = await GetDownloadUrl();
  83.  
  84.         if (data is { } d)
  85.         {
  86.             bool check = await DownloadAndExtract(d);
  87.  
  88.             if (check)
  89.             {
  90.                 AlertUserProcessFinished();
  91.             }
  92.         }
  93.     }
  94.  
  95.     SupportedOperatingSystem GetSupportedOs()
  96.     {
  97.         if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  98.             return SupportedOperatingSystem.Windows;
  99.         if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  100.             return SupportedOperatingSystem.Linux;
  101.         if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  102.             return SupportedOperatingSystem.Mac;
  103.         throw new PlatformNotSupportedException();
  104.     }
  105.    
  106.     async Task<VersionData?> GetDownloadUrl()
  107.     {
  108.         _client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
  109.  
  110.         try
  111.         {
  112.             var data = await _client.GetFromJsonAsync<ReleaseResponseDTO>(_url);
  113.             var releases = data.Assets;
  114.             var os = GetSupportedOs();
  115.             var osReleaseData = releases.FirstOrDefault(x =>
  116.             {
  117.                 var name = x.DownloadName;
  118.                 return os switch
  119.                 {
  120.                     SupportedOperatingSystem.Windows => name.Contains("Windows"),
  121.                     SupportedOperatingSystem.Linux => name.Contains("Linux"),
  122.                     SupportedOperatingSystem.Mac => name.Contains("Mac"),
  123.                     _ => false
  124.                 };
  125.             });
  126.  
  127.             if (osReleaseData != null)
  128.                 return new VersionData(
  129.                     osReleaseData.DownloadName,
  130.                     osReleaseData.DownloadUrl,
  131.                     osReleaseData.DownloadSize);
  132.  
  133.             CurrentInformation = "Failed to obtain download URL for given OS.";
  134.             return null;
  135.         }
  136.         catch (HttpRequestException ex)
  137.         {
  138.             CurrentInformation = ex.Message;
  139.             return new VersionData("Error", ex.Message, 0);
  140.         }
  141.     }
  142.  
  143.     async Task<bool> DownloadAndExtract(VersionData versionData)
  144.     {
  145.         try
  146.         {
  147.             var bytes = await _client.GetByteArrayAsync(versionData.DownloadUrl);
  148.             await File.WriteAllBytesAsync(Directory.GetCurrentDirectory(), bytes);
  149.  
  150.             string zipPath = (Directory.GetCurrentDirectory() + "\\" + versionData.DownloadName);
  151.  
  152.             string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
  153.  
  154.             try
  155.             {
  156.                 string extractToPath = Directory.GetCurrentDirectory();
  157.  
  158.                 ZipFile.ExtractToDirectory(zipPath, tempPath);
  159.  
  160.                 string[] files = Directory.GetFiles(tempPath);
  161.  
  162.                 foreach (string file in files)
  163.                 {
  164.                     FileInfo f = new FileInfo(file);
  165.  
  166.                     if (File.Exists(Path.Combine(extractToPath, f.Name)))
  167.                     {
  168.                         File.Delete(Path.Combine(extractToPath, f.Name));
  169.                         File.Move(f.FullName, Path.Combine(extractToPath, f.Name));
  170.                     }
  171.                     else
  172.                     {
  173.                         File.Move(f.FullName, Path.Combine(extractToPath, f.Name));
  174.                     }
  175.                 }
  176.             }
  177.             finally
  178.             {
  179.                 Directory.Delete(tempPath);
  180.                 Directory.Delete(zipPath);
  181.             }
  182.  
  183.             IsUpdateFinished = true;
  184.             return true;
  185.         }
  186.         catch (Exception ex)
  187.         {
  188.             CurrentInformation = ex.Message;
  189.             return false;
  190.         }
  191.     }
  192.  
  193.     public void IncrementProgress(ulong progVal)
  194.     {
  195.         Value += progVal;
  196.     }
  197.  
  198.     public void AlertUserProcessFinished()
  199.     {
  200.         CurrentInformation = "THe Update Has Finished. Click Launch App To Open Todo App";
  201.         CanRun = true;
  202.     }
  203.  
  204.     public void LaunchApplication()
  205.     {
  206.         var userOs = GetSupportedOs();
  207.  
  208.         switch (userOs)
  209.         {
  210.             case SupportedOperatingSystem.Windows:
  211.                 var p = new Process();
  212.                 p.StartInfo.FileName = "/Executables/win/Todo.exe";
  213.                 Environment.Exit(0);
  214.                 break;
  215.  
  216.             case SupportedOperatingSystem.Linux:
  217.                 // some code to launch application on linux
  218.                 Environment.Exit(0);
  219.                 break;
  220.  
  221.             case SupportedOperatingSystem.Mac:
  222.                 // some code to launch application on Mac
  223.                 Environment.Exit(0);
  224.                 break;
  225.         }
  226.     }
  227. }
Add Comment
Please, Sign In to add comment