UpdateDownloader.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PixiEditor.UpdateModule
  9. {
  10. public static class UpdateDownloader
  11. {
  12. public static string DownloadLocation = AppDomain.CurrentDomain.BaseDirectory;
  13. public static async Task DownloadReleaseZip(ReleaseInfo release)
  14. {
  15. Asset matchingAsset = GetMatchingAsset(release);
  16. using (HttpClient client = new HttpClient())
  17. {
  18. client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
  19. client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");
  20. var response = await client.GetAsync(matchingAsset.Url);
  21. if (response.StatusCode == HttpStatusCode.OK)
  22. {
  23. byte[] bytes = await response.Content.ReadAsByteArrayAsync();
  24. File.WriteAllBytes(Path.Join(DownloadLocation, $"update-{release.TagName}.zip"), bytes);
  25. }
  26. }
  27. }
  28. private static Asset GetMatchingAsset(ReleaseInfo release)
  29. {
  30. string arch = IntPtr.Size == 8 ? "x64" : "x86";
  31. return release.Assets.First(x => x.ContentType == "application/x-zip-compressed"
  32. && x.Name.Contains(arch));
  33. }
  34. }
  35. }