|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Security; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows.Forms; |
| 7 | +using System.ComponentModel; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace FlexInstaller { |
| 11 | +public class FileTransferManager |
| 12 | +{ |
| 13 | +private ProgressBar barProgress; |
| 14 | +private Label txtStatus; |
| 15 | +private WebClient client; |
| 16 | +private bool finished; |
| 17 | +private bool worked; |
| 18 | + |
| 19 | +public FileTransferManager(ProgressBar progress,Label status) { |
| 20 | +barProgress=progress; |
| 21 | +txtStatus=status; |
| 22 | +} |
| 23 | + |
| 24 | +public async Task<bool> RetrieveFileFromCloud(string webUrl,string localFile) |
| 25 | +{ |
| 26 | +try { |
| 27 | +if(string.IsNullOrEmpty(webUrl)||!Uri.IsWellFormedUriString(webUrl,UriKind.Absolute)) { |
| 28 | +txtStatus.Text="Invalid download URL"; |
| 29 | +return false; |
| 30 | +} |
| 31 | + |
| 32 | +string processedUrl = ProcessDownloadVariation(webUrl); |
| 33 | +if(processedUrl.Contains("api.github.com")) { |
| 34 | +return await HandleGitHubApiDownload(processedUrl, localFile); |
| 35 | +} |
| 36 | + |
| 37 | +txtStatus.Text="Connecting to download server..."; |
| 38 | +Application.DoEvents(); |
| 39 | + |
| 40 | +ServicePointManager.SecurityProtocol=(SecurityProtocolType)3072|(SecurityProtocolType)768|(SecurityProtocolType)192; |
| 41 | +ServicePointManager.ServerCertificateValidationCallback=delegate{return true;}; |
| 42 | + |
| 43 | +client=new WebClient(); |
| 44 | +client.Headers.Add("User-Agent",AppConfig.appName+" Installer v"+AppConfig.appVer); |
| 45 | + |
| 46 | +finished=false; |
| 47 | +worked=false; |
| 48 | + |
| 49 | +client.DownloadProgressChanged+=(sender,e)=> { |
| 50 | +barProgress.Value=e.ProgressPercentage; |
| 51 | +txtStatus.Text=string.Format("Downloading... {0}% ({1:F1} MB of {2:F1} MB)", |
| 52 | +e.ProgressPercentage, |
| 53 | +e.BytesReceived/1024.0/1024.0, |
| 54 | +e.TotalBytesToReceive/1024.0/1024.0); |
| 55 | +Application.DoEvents(); |
| 56 | +}; |
| 57 | + |
| 58 | +client.DownloadFileCompleted+=(sender,e)=> { |
| 59 | +finished=true; |
| 60 | +if(e.Error!=null) { |
| 61 | +txtStatus.Text=string.Format("Download failed: {0}",e.Error.Message); |
| 62 | +worked=false; |
| 63 | +} else if(e.Cancelled) { |
| 64 | +txtStatus.Text="Download was cancelled"; |
| 65 | +worked=false; |
| 66 | +} else { |
| 67 | +txtStatus.Text="Download completed successfully"; |
| 68 | +worked=true; |
| 69 | +} |
| 70 | +}; |
| 71 | + |
| 72 | +client.DownloadFileAsync(new Uri(processedUrl),localFile); |
| 73 | + |
| 74 | +while(!finished) { |
| 75 | +await Task.Delay(100); |
| 76 | +Application.DoEvents(); |
| 77 | +} |
| 78 | + |
| 79 | +client.Dispose(); |
| 80 | +return worked; |
| 81 | +} catch(Exception ex) { |
| 82 | +txtStatus.Text=string.Format("Download error: {0}",ex.Message); |
| 83 | +if(client!=null) { |
| 84 | +client.Dispose(); |
| 85 | +} |
| 86 | +return false; |
| 87 | +} |
| 88 | +} |
| 89 | + |
| 90 | +private async Task<bool> HandleGitHubApiDownload(string apiUrl, string localFile) |
| 91 | +{ |
| 92 | +try { |
| 93 | +txtStatus.Text="Getting GitHub release info..."; |
| 94 | +Application.DoEvents(); |
| 95 | + |
| 96 | +ServicePointManager.SecurityProtocol=(SecurityProtocolType)3072|(SecurityProtocolType)768|(SecurityProtocolType)192; |
| 97 | +ServicePointManager.ServerCertificateValidationCallback=delegate{return true;}; |
| 98 | + |
| 99 | +using(WebClient apiClient = new WebClient()) { |
| 100 | +apiClient.Headers.Add("User-Agent",AppConfig.appName+" Installer v"+AppConfig.appVer); |
| 101 | +apiClient.Headers.Add("Accept","application/vnd.github.v3+json"); |
| 102 | +string response = await apiClient.DownloadStringTaskAsync(apiUrl); |
| 103 | + |
| 104 | +string downloadUrl = ExtractAssetUrl(response); |
| 105 | +if(string.IsNullOrEmpty(downloadUrl)) { |
| 106 | +txtStatus.Text="Could not find download asset"; |
| 107 | +return false; |
| 108 | +} |
| 109 | + |
| 110 | +return await DownloadFromUrl(downloadUrl, localFile); |
| 111 | +} |
| 112 | +} catch(Exception ex) { |
| 113 | +txtStatus.Text=string.Format("GitHub API error: {0}",ex.Message); |
| 114 | +return false; |
| 115 | +} |
| 116 | +} |
| 117 | + |
| 118 | +private async Task<bool> DownloadFromUrl(string url, string localFile) |
| 119 | +{ |
| 120 | +txtStatus.Text="Connecting to download server..."; |
| 121 | +Application.DoEvents(); |
| 122 | + |
| 123 | +client=new WebClient(); |
| 124 | +client.Headers.Add("User-Agent",AppConfig.appName+" Installer v"+AppConfig.appVer); |
| 125 | + |
| 126 | +finished=false; |
| 127 | +worked=false; |
| 128 | + |
| 129 | +client.DownloadProgressChanged+=(sender,e)=> { |
| 130 | +barProgress.Value=e.ProgressPercentage; |
| 131 | +txtStatus.Text=string.Format("Downloading... {0}% ({1:F1} MB of {2:F1} MB)", |
| 132 | +e.ProgressPercentage, |
| 133 | +e.BytesReceived/1024.0/1024.0, |
| 134 | +e.TotalBytesToReceive/1024.0/1024.0); |
| 135 | +Application.DoEvents(); |
| 136 | +}; |
| 137 | + |
| 138 | +client.DownloadFileCompleted+=(sender,e)=> { |
| 139 | +finished=true; |
| 140 | +if(e.Error!=null) { |
| 141 | +txtStatus.Text=string.Format("Download failed: {0}",e.Error.Message); |
| 142 | +worked=false; |
| 143 | +} else if(e.Cancelled) { |
| 144 | +txtStatus.Text="Download was cancelled"; |
| 145 | +worked=false; |
| 146 | +} else { |
| 147 | +txtStatus.Text="Download completed successfully"; |
| 148 | +worked=true; |
| 149 | +} |
| 150 | +}; |
| 151 | + |
| 152 | +client.DownloadFileAsync(new Uri(url),localFile); |
| 153 | + |
| 154 | +while(!finished) { |
| 155 | +await Task.Delay(100); |
| 156 | +Application.DoEvents(); |
| 157 | +} |
| 158 | + |
| 159 | +client.Dispose(); |
| 160 | +return worked; |
| 161 | +} |
| 162 | + |
| 163 | +private string ExtractAssetUrl(string jsonResponse) |
| 164 | +{ |
| 165 | +try { |
| 166 | +int assetsStart = jsonResponse.IndexOf("\"assets\":"); |
| 167 | +if(assetsStart == -1) return null; |
| 168 | + |
| 169 | +string assetsSection = jsonResponse.Substring(assetsStart); |
| 170 | +int nameIndex = assetsSection.IndexOf("\"name\":\"" + AppConfig.exeName + "\""); |
| 171 | +if(nameIndex == -1) { |
| 172 | +nameIndex = assetsSection.IndexOf("\"name\":"); |
| 173 | +if(nameIndex == -1) return null; |
| 174 | +} |
| 175 | + |
| 176 | +int urlStart = assetsSection.IndexOf("\"browser_download_url\":\"", nameIndex); |
| 177 | +if(urlStart == -1) return null; |
| 178 | + |
| 179 | +urlStart += 25; |
| 180 | +int urlEnd = assetsSection.IndexOf("\"", urlStart); |
| 181 | +if(urlEnd == -1) return null; |
| 182 | + |
| 183 | +return assetsSection.Substring(urlStart, urlEnd - urlStart); |
| 184 | +} catch { |
| 185 | +return null; |
| 186 | +} |
| 187 | +} |
| 188 | + |
| 189 | +private string ProcessDownloadVariation(string originalUrl) |
| 190 | +{ |
| 191 | +try { |
| 192 | +if(originalUrl.Contains("github.com") && originalUrl.Contains("/releases/")) { |
| 193 | +if(originalUrl.Contains("/download/")) { |
| 194 | +return originalUrl; |
| 195 | +} else if(originalUrl.Contains("/tag/")) { |
| 196 | +string tagUrl = originalUrl; |
| 197 | +string[] parts = tagUrl.Split('/'); |
| 198 | +if(parts.Length >= 7) { |
| 199 | +string owner = parts[3]; |
| 200 | +string repo = parts[4]; |
| 201 | +string tag = parts[6]; |
| 202 | +return string.Format("https://api.github.com/repos/{0}/{1}/releases/tags/{2}",owner,repo,tag); |
| 203 | +} |
| 204 | +} |
| 205 | +} else if(originalUrl.Contains("dropbox.com") && originalUrl.Contains("?dl=0")) { |
| 206 | +return originalUrl.Replace("?dl=0","?dl=1"); |
| 207 | +} else if(originalUrl.Contains("dropbox.com") && !originalUrl.Contains("?dl=")) { |
| 208 | +return originalUrl + "?dl=1"; |
| 209 | +} |
| 210 | +return originalUrl; |
| 211 | +} catch { |
| 212 | +return originalUrl; |
| 213 | +} |
| 214 | +} |
| 215 | + |
| 216 | +public static void Dispose() { |
| 217 | +} |
| 218 | +} |
| 219 | +} |
0 commit comments