From c1893366982d2f7d729a0cbdca6363c1d5b1218c Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 11 Apr 2025 16:46:45 +0800 Subject: [PATCH 1/3] Fix possible directory not found issue when saving storage --- Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs | 3 ++- Flow.Launcher.Infrastructure/Storage/JsonStorage.cs | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs index 43bb8dadecb..414743d22c9 100644 --- a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs @@ -82,8 +82,8 @@ private static async ValueTask DeserializeAsync(Stream stream, T defaultData) public void Save() { + FilesFolders.ValidateDirectory(DirectoryPath); // User may delete the directory, so we need to check it var serialized = MemoryPackSerializer.Serialize(Data); - File.WriteAllBytes(FilePath, serialized); } @@ -103,6 +103,7 @@ public void ClearData() // so we need to pass it to SaveAsync public async ValueTask SaveAsync(T data) { + FilesFolders.ValidateDirectory(DirectoryPath); // User may delete the directory, so we need to check it await using var stream = new FileStream(FilePath, FileMode.Create); await MemoryPackSerializer.SerializeAsync(stream, data); } diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index cdf3ae90962..f283be59e18 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -183,7 +183,10 @@ private void BackupOriginFile() public void Save() { - string serialized = JsonSerializer.Serialize(Data, + // User may delete the directory, so we need to check it + FilesFolders.ValidateDirectory(DirectoryPath); + + var serialized = JsonSerializer.Serialize(Data, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(TempFilePath, serialized); @@ -193,6 +196,9 @@ public void Save() public async Task SaveAsync() { + // User may delete the directory, so we need to check it + FilesFolders.ValidateDirectory(DirectoryPath); + await using var tempOutput = File.OpenWrite(TempFilePath); await JsonSerializer.SerializeAsync(tempOutput, Data, new JsonSerializerOptions { WriteIndented = true }); From 5c43dd45b236c6074f63dc314f0d3668c1653e09 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 11 Apr 2025 16:48:02 +0800 Subject: [PATCH 2/3] Code quality --- Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs index 414743d22c9..b85111756f5 100644 --- a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs @@ -82,7 +82,9 @@ private static async ValueTask DeserializeAsync(Stream stream, T defaultData) public void Save() { - FilesFolders.ValidateDirectory(DirectoryPath); // User may delete the directory, so we need to check it + // User may delete the directory, so we need to check it + FilesFolders.ValidateDirectory(DirectoryPath); + var serialized = MemoryPackSerializer.Serialize(Data); File.WriteAllBytes(FilePath, serialized); } @@ -103,7 +105,9 @@ public void ClearData() // so we need to pass it to SaveAsync public async ValueTask SaveAsync(T data) { - FilesFolders.ValidateDirectory(DirectoryPath); // User may delete the directory, so we need to check it + // User may delete the directory, so we need to check it + FilesFolders.ValidateDirectory(DirectoryPath); + await using var stream = new FileStream(FilePath, FileMode.Create); await MemoryPackSerializer.SerializeAsync(stream, data); } From e6377d046348058c1b32cb2905960747468a5101 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 11 Apr 2025 18:40:03 +0800 Subject: [PATCH 3/3] Fix old Program plugin constructor issue --- .../Storage/BinaryStorage.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs index b85111756f5..64f80918199 100644 --- a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Threading.Tasks; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; @@ -40,6 +41,16 @@ public BinaryStorage(string filename) FilePath = Path.Combine(DirectoryPath, $"{filename}{FileSuffix}"); } + // Let the old Program plugin get this constructor + [Obsolete("This constructor is obsolete. Use BinaryStorage(string filename) instead.")] + public BinaryStorage(string filename, string directoryPath = null!) + { + directoryPath ??= DataLocation.CacheDirectory; + FilesFolders.ValidateDirectory(directoryPath); + + FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}"); + } + public async ValueTask TryLoadAsync(T defaultData) { if (Data != null) return Data;