BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
Updater.cs
1using Gtk;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using Flurl;
9using Newtonsoft.Json;
10using Flurl.Http;
11namespace BioGTK
12{
13 public class Updater : Dialog
14 {
15 #region Properties
16 private Builder _builder;
17#pragma warning disable 649
18 [Builder.Object]
19 private Gtk.TextView textBox;
20 [Builder.Object]
21 private Gtk.Button upBut;
22#pragma warning restore 649
23
24 #endregion
25
26 public class GitHubFile
27 {
28 public string Name { get; set; }
29 public string Path { get; set; }
30 public string Sha { get; set; }
31 public int Size { get; set; }
32 public string Url { get; set; }
33 public string HtmlUrl { get; set; }
34 public string GitUrl { get; set; }
35 public string DownloadUrl { get; set; }
36 public string Type { get; set; }
37 public Links _Links { get; set; }
38
39 // Nested class for the _links object
40 public class Links
41 {
42 [JsonProperty("self")]
43 public string Self { get; set; }
44
45 [JsonProperty("git")]
46 public string Git { get; set; }
47
48 [JsonProperty("html")]
49 public string Html { get; set; }
50 }
51 }
52
53 #region Constructors / Destructors
54 string selection;
55 Enum ens;
59 public static Updater Create()
60 {
61 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/Updater.glade", FileMode.Open));
62 return new Updater(builder, builder.GetObject("updater").Handle);
63 }
64
65 /* The constructor for the Updater class. */
66 protected Updater(Builder builder, IntPtr handle) : base(handle)
67 {
68 _builder = builder;
69 builder.Autoconnect(this);
70 this.DeleteEvent += Updater_DeleteEvent;
71 upBut.ButtonPressEvent += UpButton_ButtonPressEvent;
72 string st = BioLib.Settings.GetSettings("UpdateSites");
73 if(st!="")
74 {
75 string[] ups = st.Split(',');
76 for(int i = 0; i < ups.Length; i++)
77 {
78 if (i < ups.Length - 1)
79 textBox.Buffer.Text += ups[i] + Environment.NewLine;
80 else
81 textBox.Buffer.Text += ups[i];
82 }
83 }
84 App.ApplyStyles(this);
85 }
86
87 private void UpButton_ButtonPressEvent(object o, ButtonPressEventArgs args)
88 {
89 string[] sts;
90 if (textBox.Buffer.Text.Contains('\n'))
91 sts = textBox.Buffer.Text.Split('\n');
92 else
93 sts = new string[] { textBox.Buffer.Text };
94 foreach (string s in sts)
95 {
96 string st = s;
97 if (!s.EndsWith("/contents/"))
98 st += "/contents/";
99 if (!s.Contains("api"))
100 st = st.Replace("github.com","api.github.com/repos");
101 string j = GetHtmlContentAsync(st).Result;
102 var gitHubFile = JsonConvert.DeserializeObject<GitHubFile[]>(j);
103 foreach (var item in gitHubFile)
104 {
105 if(item.Name.EndsWith(".dll"))
106 {
107 string url = s.Replace("github.com", "raw.githubusercontent.com") + "/main/" + item.Name;
108 DownloadFileAsync(url, System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/Plugins/" + item.Name).Wait();
109 }
110 }
111 }
112 }
113
114 private void Updater_DeleteEvent(object o, DeleteEventArgs args)
115 {
116 string[] sts = textBox.Buffer.Text.Split(Environment.NewLine);
117 string ups = "";
118 for (int i = 0; i < sts.Length; i++)
119 {
120 if(i<sts.Length-1)
121 ups += sts[i] + ",";
122 else
123 ups += sts[i];
124 }
125 BioLib.Settings.AddSettings("UpdateSites", ups);
126 BioLib.Settings.Save();
127 }
128 public static async Task DownloadFileAsync(string fileUrl, string savePath)
129 {
130 try
131 {
132 // Download the file data as byte array
133 byte[] fileBytes = await fileUrl.WithHeader("User-Agent", "BioGTK").GetBytesAsync().ConfigureAwait(false);
134
135 // Write the byte array to a file
136 await File.WriteAllBytesAsync(savePath, fileBytes);
137 Console.WriteLine($"File downloaded successfully and saved to {savePath}");
138 }
139 catch (FlurlHttpException e)
140 {
141 // Handle HTTP errors here (e.g., 404 or 403 errors)
142 Console.WriteLine($"Failed to download the file. HTTP Error: {e.Message}");
143 }
144 catch (Exception e)
145 {
146 // Handle other errors here
147 Console.WriteLine($"An error occurred: {e.Message}");
148 }
149 }
150
151 public static async Task<string> GetHtmlContentAsync(string url)
152 {
153 try
154 {
155 // Fetch the HTML content of the page
156 string htmlContent = await url.WithHeader("User-Agent", "BioGTK").GetStringAsync().ConfigureAwait(false);
157 return htmlContent;
158 }
159 catch (FlurlHttpException e)
160 {
161 // Handle HTTP errors (e.g., 404, 500)
162 Console.WriteLine($"HTTP Request Failed: {e.Message}");
163 return null;
164 }
165 }
166 #endregion
167
168 }
169}
static Updater Create()
Definition Updater.cs:59
Definition ML.cs:17
Definition Zarr.cs:20