public static void SetMetadata(string jsonData) { LogFile.WriteLog("Set app metadata on " + Config.Auth0.Domain + " ..."); if (!InternetHelper.CheckConnection()) { return; } try { var appName = Config.Informations.Extension.Name.Replace(" ", "_"); var client = new RestClient("https://" + Config.Auth0.Domain + "/api/v2/users/" + UserId); var request = new RestRequest(Method.PATCH); request.AddHeader("content-type", "application/json"); request.AddHeader("authorization", GetTokenType() + " " + GetToken()); request.AddParameter("application/json", "{\"app_metadata\": {\"" + appName + "\": " + jsonData + "}}", ParameterType.RequestBody); client.Execute(request); } catch (Exception ex) { LogFile.WriteLog(ex); } }
private static void SyncWithServer() { LogFile.WriteLog("Synchronize settings with the server ..."); _syncWithServer = true; try { Sync: if (!InternetHelper.CheckConnection()) { goto Finish; } var serverJson = AccountHelper.ReadMetadata(); var localJson = Config.Settings.Json; DateTime lastServerSync; DateTime lastLocalSync; try { lastServerSync = DateTime.Parse(JsonHelper.ReadString(serverJson, "lastChange"), CultureInfo.InvariantCulture); } catch { lastServerSync = DateTime.Parse("01.01.0001 00:00:00", CultureInfo.InvariantCulture); } try { lastLocalSync = DateTime.Parse(JsonHelper.ReadString(localJson, "lastChange"), CultureInfo.InvariantCulture); } catch { lastLocalSync = DateTime.Parse("01.01.0001 00:00:00", CultureInfo.InvariantCulture); } if (lastServerSync == DateTime.Parse("01.01.0001 00:00:00", CultureInfo.InvariantCulture) || string.IsNullOrEmpty(serverJson)) { AccountHelper.SetMetadata(JsonHelper.ChangeValue(localJson, "lastChange", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture))); goto Sync; } if (string.IsNullOrEmpty(lastLocalSync.ToString(CultureInfo.InvariantCulture)) || string.IsNullOrEmpty(localJson)) { goto Finish; } if (lastServerSync == lastLocalSync) { goto Finish; } if (lastServerSync < lastLocalSync) { AccountHelper.SetMetadata(localJson); } else if (lastServerSync > lastLocalSync) { Config.Settings.Json = serverJson; } } catch (Exception ex) { LogFile.WriteLog(ex); } Finish: _syncWithServer = false; }
public static void Update(bool updateExtension) { if (updateExtension && string.IsNullOrEmpty(Config.ExtensionDirectoryName)) { return; } LogFile.WriteLog("Check for new " + (updateExtension ? "extension" : "program") + " updates ..."); if (updateExtension) { Config.Updater.Extension.NewestVersion = null; } if (!updateExtension) { Config.Updater.Programm.NewestVersion = null; } try { if (!InternetHelper.CheckConnection()) { return; } var file = updateExtension ? Path.Combine(Config.ExtensionsDirectory, Config.ExtensionDirectoryName ?? throw new InvalidOperationException(), Config.Updater.Extension.RunningVersion.ToString(), "VersionHistory.xml") : Path.Combine( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName) ?? throw new InvalidOperationException(), "VersionHistory.xml"); try { using (var client = new WebClient()) { client.DownloadFile( updateExtension ? Config.Updater.Extension.VersionsHistoryFile : Config.Updater.Programm.VersionsHistoryFile, file); } } catch { return; } var userUpdateChannel = updateExtension ? (int)Enum.Parse(typeof(UpdateChannels), JsonHelper.ReadString(Config.Settings.Json, "updateChannel").ToLower()) : (int)Enum.Parse(typeof(UpdateChannels), Config.Settings.MainUpdateChannel.ToLower()); var updateChannels = XmlHelper.ReadStringList(file, "updateChannel"); var versions = new List <Version>(); for (var i = 0; i != updateChannels.Count; ++i) { versions.Add(new Version(XmlHelper.ReadStringList(file, "version")[i])); } var latestVersion = versions.Max(); var serverUpdateFile = ""; var useSetup = false; var updateChannel = (int)Enum.GetValues(typeof(UpdateChannels)).Cast <UpdateChannels>().Max(); checkUpdateChannel: for (var i = 0; i != versions.Count; ++i) { if (latestVersion != versions[i]) { continue; } if (!Enum.IsDefined(typeof(UpdateChannels), (int)Enum.Parse(typeof(UpdateChannels), updateChannels[i].ToLower()))) { continue; } updateChannel = (int)Enum.Parse(typeof(UpdateChannels), updateChannels[i].ToLower()); serverUpdateFile = XmlHelper.ReadStringList(file, "file")[i]; useSetup = XmlHelper.ReadBoolList(file, "setup")[i]; } if (updateChannel < userUpdateChannel) { if (latestVersion == versions.Min()) { goto updateFinished; } latestVersion = versions.Where(s => s < latestVersion).Max(); goto checkUpdateChannel; } var currentVersion = updateExtension ? Config.Updater.Extension.Version : Config.Updater.Programm.Version; switch (updateExtension) { case true: Config.Updater.Extension.NewestVersion = latestVersion.ToString(); break; case false: Config.Updater.Programm.NewestVersion = latestVersion.ToString(); break; } if (latestVersion <= currentVersion) { goto updateFinished; } LogFile.WriteLog("New " + (updateExtension ? "extension" : "program") + " update found: Latest version: " + latestVersion + " / Installed version: " + currentVersion); IsDownloading = true; try { if (!useSetup) { LogFile.WriteLog("Download and install update ..."); var localUpdateFile = updateExtension ? Path.Combine(Config.ExtensionsDirectory, Config.ExtensionDirectoryName, "update.zip") : Path.Combine( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName) ?? throw new InvalidOperationException(), "update.zip"); using (var client = new WebClient()) { client.DownloadFile(serverUpdateFile, localUpdateFile); } var newExtensionDirectory = updateExtension ? Path.Combine(Config.ExtensionsDirectory, Config.ExtensionDirectoryName, latestVersion.ToString()) : Path.Combine( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName) ?? throw new InvalidOperationException(), "update"); Directory.CreateDirectory(newExtensionDirectory); ZipFile.ExtractToDirectory(localUpdateFile, newExtensionDirectory); File.Delete(localUpdateFile); } else { LogFile.WriteLog("Download the setup ..."); var localUpdateFile = updateExtension ? Path.Combine(Config.ExtensionsDirectory, Config.ExtensionDirectoryName, "updater.exe") : Path.Combine( Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName) ?? throw new InvalidOperationException(), "updater.exe"); using (var client = new WebClient()) { client.DownloadFile(serverUpdateFile, localUpdateFile); } } } catch (Exception ex) { LogFile.WriteLog(ex); } updateFinished: File.Delete(file); } catch (Exception ex) { LogFile.WriteLog(ex); } IsDownloading = false; }