From 51fac770978e1f963b08684f98d9c7dd1e8c841e Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Tue, 24 Jan 2023 20:42:53 -0600 Subject: [PATCH 1/2] Use File.Move when the setting file does not exists --- .../Storage/JsonStorage.cs | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index 500b0829e..43e7ddab7 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -13,12 +13,17 @@ namespace Flow.Launcher.Infrastructure.Storage public class JsonStorage where T : new() { protected T? Data; + // need a new directory name public const string DirectoryName = "Settings"; public const string FileSuffix = ".json"; + protected string FilePath { get; init; } = null!; + private string TempFilePath => $"{FilePath}.tmp"; + private string BackupFilePath => $"{FilePath}.bak"; + protected string DirectoryPath { get; init; } = null!; @@ -35,7 +40,7 @@ namespace Flow.Launcher.Infrastructure.Storage { try { - Data = JsonSerializer.Deserialize(serialized)?? TryLoadBackup() ?? LoadDefault(); + Data = JsonSerializer.Deserialize(serialized) ?? TryLoadBackup() ?? LoadDefault(); } catch (JsonException) { @@ -46,6 +51,7 @@ namespace Flow.Launcher.Infrastructure.Storage { Data = TryLoadBackup() ?? LoadDefault(); } + return Data.NonNull(); } @@ -67,12 +73,15 @@ namespace Flow.Launcher.Infrastructure.Storage try { var data = JsonSerializer.Deserialize(File.ReadAllText(BackupFilePath)); + if (data != null) { Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully"); File.Replace(BackupFilePath, FilePath, null); + return data; } + return default; } catch (JsonException) @@ -94,14 +103,22 @@ namespace Flow.Launcher.Infrastructure.Storage public void Save() { - string serialized = JsonSerializer.Serialize(Data, new JsonSerializerOptions - { - WriteIndented = true - }); + string serialized = JsonSerializer.Serialize(Data, + new JsonSerializerOptions + { + WriteIndented = true + }); File.WriteAllText(TempFilePath, serialized); - File.Replace(TempFilePath, FilePath, BackupFilePath); - File.Delete(TempFilePath); + + if (!File.Exists(FilePath)) + { + File.Move(TempFilePath, FilePath); + } + else + { + File.Replace(TempFilePath, FilePath, BackupFilePath); + } } } } From abc13a4de54c3b6579a3ad0da004330695dca928 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 25 Jan 2023 20:47:11 +1100 Subject: [PATCH 2/2] version bump --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index a327fa3a9..29fc4cf4b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: '1.12.0.{build}' +version: '1.12.1.{build}' init: - ps: |