mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #1843 from Flow-Launcher/save_exception_file_not_found
Use File.Move when the setting file does not exists
This commit is contained in:
commit
4cef53b75d
1 changed files with 24 additions and 7 deletions
|
|
@ -13,12 +13,17 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
public class JsonStorage<T> where T : new()
|
public class JsonStorage<T> where T : new()
|
||||||
{
|
{
|
||||||
protected T? Data;
|
protected T? Data;
|
||||||
|
|
||||||
// need a new directory name
|
// need a new directory name
|
||||||
public const string DirectoryName = "Settings";
|
public const string DirectoryName = "Settings";
|
||||||
public const string FileSuffix = ".json";
|
public const string FileSuffix = ".json";
|
||||||
|
|
||||||
protected string FilePath { get; init; } = null!;
|
protected string FilePath { get; init; } = null!;
|
||||||
|
|
||||||
private string TempFilePath => $"{FilePath}.tmp";
|
private string TempFilePath => $"{FilePath}.tmp";
|
||||||
|
|
||||||
private string BackupFilePath => $"{FilePath}.bak";
|
private string BackupFilePath => $"{FilePath}.bak";
|
||||||
|
|
||||||
protected string DirectoryPath { get; init; } = null!;
|
protected string DirectoryPath { get; init; } = null!;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -35,7 +40,7 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Data = JsonSerializer.Deserialize<T>(serialized)?? TryLoadBackup() ?? LoadDefault();
|
Data = JsonSerializer.Deserialize<T>(serialized) ?? TryLoadBackup() ?? LoadDefault();
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
{
|
{
|
||||||
|
|
@ -46,6 +51,7 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
{
|
{
|
||||||
Data = TryLoadBackup() ?? LoadDefault();
|
Data = TryLoadBackup() ?? LoadDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Data.NonNull();
|
return Data.NonNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,12 +73,15 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var data = JsonSerializer.Deserialize<T>(File.ReadAllText(BackupFilePath));
|
var data = JsonSerializer.Deserialize<T>(File.ReadAllText(BackupFilePath));
|
||||||
|
|
||||||
if (data != null)
|
if (data != null)
|
||||||
{
|
{
|
||||||
Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully");
|
Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully");
|
||||||
File.Replace(BackupFilePath, FilePath, null);
|
File.Replace(BackupFilePath, FilePath, null);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
|
|
@ -94,14 +103,22 @@ namespace Flow.Launcher.Infrastructure.Storage
|
||||||
|
|
||||||
public void Save()
|
public void Save()
|
||||||
{
|
{
|
||||||
string serialized = JsonSerializer.Serialize(Data, new JsonSerializerOptions
|
string serialized = JsonSerializer.Serialize(Data,
|
||||||
{
|
new JsonSerializerOptions
|
||||||
WriteIndented = true
|
{
|
||||||
});
|
WriteIndented = true
|
||||||
|
});
|
||||||
|
|
||||||
File.WriteAllText(TempFilePath, serialized);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue