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