public async Task DownloadFile(string url, string targetFile, Action <int> progress) { using (var wc = Utility.CreateWebClient()) { var failedUrl = default(string); wc.DownloadProgressChanged += (sender, args) => progress(args.ProgressPercentage); retry: try { this.Log().Info("Downloading file: " + (failedUrl ?? url)); await this.WarnIfThrows(() => wc.DownloadFileTaskAsync(failedUrl ?? url, targetFile), "Failed downloading URL: " + (failedUrl ?? url)); } catch (Exception) { // NB: Some super brain-dead services are case-sensitive yet // corrupt case on upload. I can't even. if (failedUrl != null) { throw; } failedUrl = url.ToLower(); progress(0); goto retry; } } }
public async Task <byte[]> DownloadUrl(string url) { using (var wc = _providedClient ?? Utility.CreateWebClient()) { var failedUrl = default(string); bool retry = false; do { try { this.Log().Info("Downloading url: " + (failedUrl ?? url)); return(await this.WarnIfThrows(() => wc.DownloadDataTaskAsync(failedUrl ?? url), "Failed to download url: " + (failedUrl ?? url))); } catch (Exception) { // NB: Some super brain-dead services are case-sensitive yet // corrupt case on upload. I can't even. if (failedUrl != null) { throw; } failedUrl = url.ToLower(); retry = true; } } while (retry); return(await this.WarnIfThrows(() => wc.DownloadDataTaskAsync(failedUrl ?? url), "Failed to download url: " + (failedUrl ?? url))); } }
public async Task DownloadFile(string url, string targetFile, Action <int> progress) { using (var wc = _providedClient ?? Utility.CreateWebClient()) { var failedUrl = default(string); var lastSignalled = DateTime.MinValue; wc.DownloadProgressChanged += (sender, args) => { var now = DateTime.Now; if (now - lastSignalled > TimeSpan.FromMilliseconds(500)) { lastSignalled = now; progress(args.ProgressPercentage); } }; bool retry = false; do { try { this.Log().Info("Downloading file: " + (failedUrl ?? url)); await this.WarnIfThrows( async() => { await wc.DownloadFileTaskAsync(failedUrl ?? url, targetFile); progress(100); }, "Failed downloading URL: " + (failedUrl ?? url)); } catch (Exception) { if (failedUrl != null) { throw; } failedUrl = url.ToLower(); progress(0); retry = true; } } while (retry); } }
public async Task DownloadFile(string url, string targetFile, Action <int> progress) { using (var wc = _providedClient ?? Utility.CreateWebClient()) { var failedUrl = default(string); progress = progress ?? (s => { }); var lastSignalled = DateTime.MinValue; wc.DownloadProgressChanged += (sender, args) => { var now = DateTime.Now; if (now - lastSignalled > TimeSpan.FromMilliseconds(500)) { lastSignalled = now; progress(args.ProgressPercentage); } }; retry: try { this.Log().Info("Downloading file: " + (failedUrl ?? url)); await this.WarnIfThrows( async() => { await wc.DownloadFileTaskAsync(failedUrl ?? url, targetFile); progress(100); }, "Failed downloading URL: " + (failedUrl ?? url)); } catch (Exception) { // NB: Some super brain-dead services are case-sensitive yet // corrupt case on upload. I can't even. if (failedUrl != null) { throw; } failedUrl = url.ToLower(); progress(0); goto retry; } } }
public async Task <byte[]> DownloadUrl(string url) { using (var wc = Utility.CreateWebClient()) { var failedUrl = default(string); retry: try { this.Log().Info("Downloading url: " + (failedUrl ?? url)); // Start download on the threadpool, // in order to avoid DNS resolution hanging UI thread. return(await this.WarnIfThrows(async() => { Exception ex = null; byte[] result = await Task.Run(() => { try { return wc.DownloadDataTaskAsync(failedUrl ?? url); } catch (Exception exOnThread) { ex = exOnThread; return null; } }); if (ex != null) { throw ex; } return result; }, "Failed to download url: " + failedUrl ?? url)); } catch (Exception) { // NB: Some super brain-dead services are case-sensitive yet // corrupt case on upload. I can't even. if (failedUrl != null) { throw; } failedUrl = url.ToLower(); goto retry; } } }
public async Task DownloadFile(string url, string targetFile) { var wc = Utility.CreateWebClient(); var failedUrl = default(string); retry: try { this.Log().Info("Downloading file: " + failedUrl ?? url); await this.WarnIfThrows(() => wc.DownloadFileTaskAsync(failedUrl ?? url, targetFile), "Failed downloading URL: " + failedUrl ?? url); } catch (Exception) { // NB: Some super brain-dead services are case-sensitive yet // corrupt case on upload. I can't even. if (failedUrl != null) { throw; } failedUrl = url.ToLower(); goto retry; } }
public async Task <RegistryKey> CreateUninstallerRegistryEntry(string uninstallCmd, string quietSwitch) { var releaseContent = File.ReadAllText(Path.Combine(rootAppDirectory, "packages", "RELEASES"), Encoding.UTF8); var releases = ReleaseEntry.ParseReleaseFile(releaseContent); var latest = releases.Where(x => !x.IsDelta).OrderByDescending(x => x.Version).First(); // Download the icon and PNG => ICO it. If this doesn't work, who cares var pkgPath = Path.Combine(rootAppDirectory, "packages", latest.Filename); var zp = new ZipPackage(pkgPath); var targetPng = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".png"); var targetIco = Path.Combine(rootAppDirectory, "app.ico"); // NB: Sometimes the Uninstall key doesn't exist using (var parentKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) .CreateSubKey("Uninstall", RegistryKeyPermissionCheck.ReadWriteSubTree)) {; } var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) .CreateSubKey(uninstallRegSubKey + "\\" + applicationName, RegistryKeyPermissionCheck.ReadWriteSubTree); if (zp.IconUrl != null && !File.Exists(targetIco)) { try { using (var wc = Utility.CreateWebClient()) { await wc.DownloadFileTaskAsync(zp.IconUrl, targetPng); using (var fs = new FileStream(targetIco, FileMode.Create)) { if (zp.IconUrl.AbsolutePath.EndsWith("ico")) { var bytes = File.ReadAllBytes(targetPng); fs.Write(bytes, 0, bytes.Length); } else { using (var bmp = (Bitmap)Image.FromFile(targetPng)) using (var ico = Icon.FromHandle(bmp.GetHicon())) { ico.Save(fs); } } key.SetValue("DisplayIcon", targetIco, RegistryValueKind.String); } } } catch (Exception ex) { this.Log().InfoException("Couldn't write uninstall icon, don't care", ex); } finally { File.Delete(targetPng); } } var stringsToWrite = new[] { new { Key = "DisplayName", Value = zp.Title ?? zp.Description ?? zp.Summary }, new { Key = "DisplayVersion", Value = zp.Version.ToString() }, new { Key = "InstallDate", Value = DateTime.Now.ToString("yyyyMMdd") }, new { Key = "InstallLocation", Value = rootAppDirectory }, new { Key = "Publisher", Value = String.Join(",", zp.Authors) }, new { Key = "QuietUninstallString", Value = String.Format("{0} {1}", uninstallCmd, quietSwitch) }, new { Key = "UninstallString", Value = uninstallCmd }, new { Key = "URLUpdateInfo", Value = zp.ProjectUrl != null?zp.ProjectUrl.ToString() : "", } }; var dwordsToWrite = new[] { new { Key = "EstimatedSize", Value = (int)((new FileInfo(pkgPath)).Length / 1024) }, new { Key = "NoModify", Value = 1 }, new { Key = "NoRepair", Value = 1 }, new { Key = "Language", Value = 0x0409 }, }; foreach (var kvp in stringsToWrite) { key.SetValue(kvp.Key, kvp.Value, RegistryValueKind.String); } foreach (var kvp in dwordsToWrite) { key.SetValue(kvp.Key, kvp.Value, RegistryValueKind.DWord); } return(key); }