UpdateDownloader.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 string DownloadLocation = Path.Join(Path.GetTempPath(), "PixiEditor");
  12. public static async Task DownloadReleaseZip(ReleaseInfo release)
  13. {
  14. Asset matchingAsset = GetMatchingAsset(release);
  15. using (HttpClient 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. byte[] bytes = await response.Content.ReadAsByteArrayAsync();
  23. CreateTempDirectory();
  24. File.WriteAllBytes(Path.Join(DownloadLocation, $"update-{release.TagName}.zip"), bytes);
  25. }
  26. }
  27. }
  28. public static void CreateTempDirectory()
  29. {
  30. if (!Directory.Exists(DownloadLocation))
  31. {
  32. Directory.CreateDirectory(DownloadLocation);
  33. }
  34. }
  35. private static Asset GetMatchingAsset(ReleaseInfo release)
  36. {
  37. string arch = IntPtr.Size == 8 ? "x64" : "x86";
  38. return release.Assets.First(x => x.ContentType.Contains("zip")
  39. && x.Name.Contains(arch));
  40. }
  41. }
  42. }