From 835cbf15bd2102bfbbbaffcbb9785d3120a42bd6 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Thu, 12 Sep 2024 10:46:59 -0500 Subject: [PATCH] Fixes #309 - FolderFileStorage doesn't overwrite existing content --- .../Storage/FileStorageTestsBase.cs | 21 ++++++++++++++++++- src/Foundatio/Storage/FolderFileStorage.cs | 2 +- .../Storage/FolderFileStorageTests.cs | 6 ++++++ .../Storage/InMemoryFileStorageTests.cs | 6 ++++++ .../Storage/ScopedFolderFileStorageTests.cs | 6 ++++++ .../Storage/ScopedInMemoryFileStorageTests.cs | 6 ++++++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs index fd813d36..89d07206 100644 --- a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs +++ b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs @@ -604,9 +604,28 @@ public virtual void CanUseDataDirectory() Assert.NotEqual(DATA_DIRECTORY_QUEUE_FOLDER, storage.Folder); Assert.True(storage.Folder.EndsWith("Queue" + Path.DirectorySeparatorChar), storage.Folder); } + + public virtual async Task CanSaveOverExistingStoredContent() + { + using var storage = GetStorage(); + if (storage == null) + return; + + await ResetAsync(storage); + + var shortIdInfo = new PostInfo { ProjectId = "123" }; + var longIdInfo = new PostInfo { ProjectId = "1234567890" }; + + string path = "test.json"; + await storage.SaveObjectAsync(path, longIdInfo); + await storage.SaveObjectAsync(path, shortIdInfo); + + var actualInfo = await storage.GetObjectAsync(path); + Assert.Equal(shortIdInfo, actualInfo); + } } -public class PostInfo +public record PostInfo { public int ApiVersion { get; set; } public string CharSet { get; set; } diff --git a/src/Foundatio/Storage/FolderFileStorage.cs b/src/Foundatio/Storage/FolderFileStorage.cs index caf54dc3..c3926bc5 100644 --- a/src/Foundatio/Storage/FolderFileStorage.cs +++ b/src/Foundatio/Storage/FolderFileStorage.cs @@ -60,7 +60,7 @@ public Task GetFileStreamAsync(string path, StreamMode streamMode, Cance string fullPath = Path.Combine(Folder, normalizedPath); EnsureDirectory(fullPath); - var stream = streamMode == StreamMode.Read ? File.OpenRead(fullPath) : File.OpenWrite(fullPath); + var stream = streamMode == StreamMode.Read ? File.OpenRead(fullPath) : new FileStream(fullPath, FileMode.Create, FileAccess.Write); return Task.FromResult(stream); } diff --git a/tests/Foundatio.Tests/Storage/FolderFileStorageTests.cs b/tests/Foundatio.Tests/Storage/FolderFileStorageTests.cs index 816d9e5a..ae184947 100644 --- a/tests/Foundatio.Tests/Storage/FolderFileStorageTests.cs +++ b/tests/Foundatio.Tests/Storage/FolderFileStorageTests.cs @@ -135,6 +135,12 @@ public override Task WillWriteStreamContentAsync() return base.WillWriteStreamContentAsync(); } + [Fact] + public override Task CanSaveOverExistingStoredContent() + { + return base.CanSaveOverExistingStoredContent(); + } + [Fact] public async Task WillNotReturnDirectoryInGetPagedFileListAsync() { diff --git a/tests/Foundatio.Tests/Storage/InMemoryFileStorageTests.cs b/tests/Foundatio.Tests/Storage/InMemoryFileStorageTests.cs index d25c45ef..6d11e55b 100644 --- a/tests/Foundatio.Tests/Storage/InMemoryFileStorageTests.cs +++ b/tests/Foundatio.Tests/Storage/InMemoryFileStorageTests.cs @@ -133,4 +133,10 @@ public override Task WillWriteStreamContentAsync() { return base.WillWriteStreamContentAsync(); } + + [Fact] + public override Task CanSaveOverExistingStoredContent() + { + return base.CanSaveOverExistingStoredContent(); + } } diff --git a/tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs b/tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs index 9de3bd7f..02a659f6 100644 --- a/tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs +++ b/tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs @@ -177,6 +177,12 @@ public override Task WillWriteStreamContentAsync() return base.WillWriteStreamContentAsync(); } + [Fact] + public override Task CanSaveOverExistingStoredContent() + { + return base.CanSaveOverExistingStoredContent(); + } + [Fact] public async Task WillNotReturnDirectoryInGetPagedFileListAsync() { diff --git a/tests/Foundatio.Tests/Storage/ScopedInMemoryFileStorageTests.cs b/tests/Foundatio.Tests/Storage/ScopedInMemoryFileStorageTests.cs index f22449fd..42a88496 100644 --- a/tests/Foundatio.Tests/Storage/ScopedInMemoryFileStorageTests.cs +++ b/tests/Foundatio.Tests/Storage/ScopedInMemoryFileStorageTests.cs @@ -133,4 +133,10 @@ public override Task WillWriteStreamContentAsync() { return base.WillWriteStreamContentAsync(); } + + [Fact] + public override Task CanSaveOverExistingStoredContent() + { + return base.CanSaveOverExistingStoredContent(); + } }