private async Task <bool> TryDownloadNewLauncher(string downloadTargetPath) { var downloadLauncherUrl = Constants.GetLauncherFileDownloadUrl(_settingsProvider.GetCurrentSettings().UpdateChannel); _logger.Write("Downloading new launcher."); try { using (var httpClientWithProgress = new HttpClientDownloadWithProgress(downloadLauncherUrl, downloadTargetPath)) { httpClientWithProgress.ProgressChanged += (size, downloaded, percentage) => UpdateStatusChanged?.Invoke(percentage ?? 50); await httpClientWithProgress.StartDownload(); } return(true); } catch (Exception e) { _logger.Write(e); } return(false); }
private async Task <bool> DownloadNewClientFiles() { var updateFilePath = GetUpdateDownloadPath(); FileOperationsHelper.EnsureFileDirectory(updateFilePath); var downloadUpdateUrl = Constants.GetUpdateFileDownloadUrl(_settingsProvider.GetCurrentSettings().UpdateChannel); using (var client = new HttpClientDownloadWithProgress(downloadUpdateUrl, updateFilePath)) { //client.ProgressChanged += (size, downloaded, percentage) => // _splashScreenProgressProvider.UpdateStep(LaunchStep.DownloadNewClient, LaunchStepStatus.Processing, percentage); await client.StartDownload(); } if (File.Exists(updateFilePath)) { return(await ExtractUpdate(updateFilePath)); } _notificationService.ShowNotification("Failed to download update.", true); return(false); }
public static async Task <bool> CheckForUpdates() { #if DEBUG File.WriteAllText("../../../Installer/latest.json", JsonSerializer.Serialize(SerializableVersion.GetAppVersion()) ); #endif if (Config.AppWasUpdated) { Log.Information("Application was updated last time it ran, cleaning up."); if (File.Exists("./PingLogger-old.exe")) { File.Delete("./PingLogger-old.exe"); } if (Config.LastTempDir != string.Empty && Directory.Exists(Config.LastTempDir)) { File.Delete(Config.LastTempDir + "/PingLogger.Setup.exe"); Directory.Delete(Config.LastTempDir); Config.LastTempDir = string.Empty; } Config.AppWasUpdated = false; } else { if (Config.UpdateLastChecked.Date >= DateTime.Today) { Log.Information("Application already checked for update today, skipping."); return(true); } SplashScreenViewModel = new ViewModels.SplashScreenViewModel(); SplashScreen = new Views.SplashScreen() { DataContext = SplashScreenViewModel }; SplashScreen.Show(); SplashScreenViewModel.ProgressBarIndeterminate = true; var localVersion = Assembly.GetExecutingAssembly().GetName().Version; try { var httpClient = new WebClient(); bool downloadComplete = false; httpClient.DownloadFileCompleted += (_, _) => { downloadComplete = true; }; string serverUrl = "https://pinglogger.lexdysia.com/"; await httpClient.DownloadFileTaskAsync($"{serverUrl}/latest.json", "./latest.json"); while (!downloadComplete) { await Task.Delay(100); } var latestJson = await File.ReadAllTextAsync("./latest.json"); var remoteVersion = JsonSerializer.Deserialize <SerializableVersion>(latestJson); File.Delete("./latest.json"); Log.Information($"Remote version is {remoteVersion}, currently running {localVersion}"); if (remoteVersion > localVersion) { Log.Information("Remote contains a newer version"); if (true) { if (Config.IsInstalled) { Config.LastTempDir = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Temp\\{RandomString(8)}"; Directory.CreateDirectory(Config.LastTempDir); Log.Information($"Creating temporary path {Config.LastTempDir}"); Log.Information($"Downloading newest installer to {Config.LastTempDir}\\PingLogger-Setup.msi"); if (remoteVersion is not null) { var downloadURL = $"{serverUrl}/{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/win/install/PingLogger.Setup.exe"; Log.Information($"Downloading from {downloadURL}"); using var downloader = new HttpClientDownloadWithProgress(downloadURL, Config.LastTempDir + "\\PingLogger.Setup.exe"); SplashScreenViewModel.UpdateMessage = $"Downloading PingLogger setup v{remoteVersion}"; downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); } Config.AppWasUpdated = true; Log.Information("Uninstalling current version."); Process.Start(new ProcessStartInfo { FileName = $"{Config.LastTempDir}/PingLogger.Setup.exe", UseShellExecute = true, Arguments = "/SILENT /CLOSEAPPLICATIONS" }); Log.Information("Installer started, closing."); Environment.Exit(0); } else { Log.Information("Renamed PingLogger.exe to PingLogger-old.exe"); File.Move("./PingLogger.exe", "./PingLogger-old.exe"); Log.Information("Downloading new PingLogger.exe"); if (remoteVersion is not null) { string[] fileList = { "libHarfBuzzSharp.dll", "libSkiaSharp.dll", "PingLogger.exe" }; SplashScreenViewModel.UpdateMessage = $"Downloading PingLogger v{remoteVersion}"; foreach (var file in fileList) { var downloadUrl = $"{serverUrl}{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/win/sf/{file}"; Log.Information($"Downloading from {downloadUrl}"); using var downloader = new HttpClientDownloadWithProgress(downloadUrl, $"./{file}"); downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); } } Config.AppWasUpdated = true; Process.Start(new ProcessStartInfo { FileName = "./PingLogger.exe" }); Log.Information("Starting new version of PingLogger"); Environment.Exit(0); } } } } catch (HttpRequestException ex) { Log.Error("Unable to auto update: " + ex.Message); return(true); } } Config.UpdateLastChecked = DateTime.Now; CloseSplashScreen(); return(true); }
public static async Task GetConstNotifications() { //Debug.WriteLine("GetConstNotifications: Start Get List"); var values = new Dictionary <string, string> { { "secureToken", "fc712a198e2520b9f3773518a519e415asdtwe" }, }; using (var client = new HttpClient()) { try { var content = new FormUrlEncodedContent(values); var response = await client.PostAsync(Api_BaseUrl + Api_ConstNotification, content); Task <string> responseString = response.Content.ReadAsStringAsync(); string outputJson = await responseString; var SettingFile = File.Create(DatabaseNotification); SettingFile.Close(); JToken token = JObject.Parse(outputJson); int error_code = (int)token["error_code"]; File.WriteAllText(DatabaseNotification, "[]"); if (error_code == 0) { JArray arr = new JArray(); foreach (var item in token["data"]) { var downloadFileUrl = item["icon_url"].ToString(); var destinationFilePath = DatabaseConstIconListFolder; Uri uri = new Uri(downloadFileUrl); destinationFilePath = destinationFilePath + Path.GetFileName(uri.LocalPath); using (var hcdp = new HttpClientDownloadWithProgress(downloadFileUrl, destinationFilePath)) { //hcdp.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) => //{ // //Debug.WriteLine($"{progressPercentage}% ({totalBytesDownloaded}/{totalFileSize})"); //}; await hcdp.StartDownload(); } JObject o = new JObject(); o["id"] = int.Parse(item["id"].ToString()); o["title"] = item["title"]; o["content"] = item["content"]; o["icon_url"] = item["icon_url"]; o["time"] = item["time"]; o["days"] = item["days"]; o["local_icon_url"] = destinationFilePath; arr.Add(o); } TextWriter tw = new StreamWriter(DatabaseNotification); tw.WriteLine(arr); tw.Close(); } } catch (Exception ex) { //Debug.WriteLine(ex.Message); } } //Debug.WriteLine("GetConstNotifications: End Get List"); }
public static async Task CheckForUpdates() { if (Config.AppWasUpdated) { Logger.Info("Application was updated last time it ran, cleaning up."); if (File.Exists("./PingLogger-old.exe")) { File.Delete("./PingLogger-old.exe"); } if (Config.LastTempDir != string.Empty && Directory.Exists(Config.LastTempDir)) { File.Delete(Config.LastTempDir + "/PingLogger-Setup.msi"); Directory.Delete(Config.LastTempDir); Config.LastTempDir = string.Empty; } Config.AppWasUpdated = false; } else { if (Config.UpdateLastChecked.Date >= DateTime.Today) { Logger.Info("Application already checked for update today, skipping."); return; } splashScreen = new Controls.SplashScreen(); splashScreen.Show(); splashScreen.dlProgress.IsIndeterminate = true; splashScreen.dlProgress.Value = 1; var localVersion = Assembly.GetExecutingAssembly().GetName().Version; try { var httpClient = new WebClient(); bool downloadComplete = false; httpClient.DownloadFileCompleted += (o, i) => { downloadComplete = true; }; string azureURL = "https://pingloggerfiles.blob.core.windows.net/"; await httpClient.DownloadFileTaskAsync($"{azureURL}version/latest.json", $"./latest.json"); while (!downloadComplete) { await Task.Delay(100); } var latestJson = File.ReadAllText("./latest.json"); var remoteVersion = JsonSerializer.Deserialize <SerializableVersion>(latestJson); File.Delete("./latest.json"); Logger.Info($"Remote version is {remoteVersion}, currently running {localVersion}"); if (remoteVersion > localVersion) { Logger.Info("Remote contains a newer version"); if (Controls.UpdatePromptDialog.Show()) { if (Config.IsInstalled) { Config.LastTempDir = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Temp\\{RandomString(8)}"; Directory.CreateDirectory(Config.LastTempDir); Logger.Info($"Creating temporary path {Config.LastTempDir}"); Logger.Info($"Downloading newest installer to {Config.LastTempDir}\\PingLogger-Setup.msi"); var downloadURL = $"{azureURL}v{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/PingLogger-Setup.msi"; Logger.Info($"Downloading from {downloadURL}"); using var downloader = new HttpClientDownloadWithProgress(downloadURL, Config.LastTempDir + "\\PingLogger-Setup.msi"); splashScreen.mainLabel.Text = $"Downloading PingLogger setup v{remoteVersion}"; downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); Config.AppWasUpdated = true; Logger.Info("Uninstalling current version."); string batchFile = $@"@echo off msiexec.exe /q /l* '{ AppContext.BaseDirectory}Logs\Installer - v{localVersion}.log' /x {Config.InstallerGUID} msiexec.exe /l* '{ AppContext.BaseDirectory}Logs\Installer - v{remoteVersion}.log' /i {Config.LastTempDir}/PingLogger-Setup.msi"; File.WriteAllText(Config.LastTempDir + "/install.bat", batchFile); Process.Start(new ProcessStartInfo { FileName = "cmd.exe", UseShellExecute = true, Arguments = $"{Config.LastTempDir}/install.bat" }); Logger.Info("Installer started, closing."); Environment.Exit(0); } else { Logger.Info("Renamed PingLogger.exe to PingLogger-old.exe"); File.Move("./PingLogger.exe", "./PingLogger-old.exe"); Logger.Info("Downloading new PingLogger.exe"); var downloadURL = $"{azureURL}v{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/PingLogger.exe"; Logger.Info($"Downloading from {downloadURL}"); using var downloader = new HttpClientDownloadWithProgress(downloadURL, "./PingLogger.exe"); splashScreen.mainLabel.Text = $"Downloading PingLogger v{remoteVersion}"; downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); Config.AppWasUpdated = true; Process.Start(new ProcessStartInfo { FileName = "./PingLogger.exe" }); Logger.Info("Starting new version of PingLogger"); Environment.Exit(0); } } } } catch (HttpRequestException ex) { Logger.Error("Unable to auto update: " + ex.Message); return; } } Config.UpdateLastChecked = DateTime.Now; return; }