Skip to content

Commit

Permalink
Fixed a bug where the in memory file storage couldn't delete files wi…
Browse files Browse the repository at this point in the history
…th a wildcard ending (no extension)
  • Loading branch information
niemyjski committed Sep 25, 2024
1 parent 8a7d6a4 commit 18652f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -402,6 +402,10 @@ public virtual async Task CanDeleteNestedFolderAsync()
Assert.True(await storage.ExistsAsync(@"x\hello.txt"));
Assert.False(await storage.ExistsAsync(@"x\nested\hello.txt"));
Assert.False(await storage.ExistsAsync(@"x\nested\world.csv"));

Assert.Equal(1, await storage.DeleteFilesAsync(@"x\hello*"));
Assert.Empty(await storage.GetFileListAsync());
Assert.False(await storage.ExistsAsync(@"x\hello.txt"));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Foundatio/Storage/InMemoryFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ public Task<int> DeleteFilesAsync(string searchPattern = null, CancellationToken

if (searchPattern[searchPattern.Length - 1] == Path.DirectorySeparatorChar)
searchPattern = $"{searchPattern}*";
else if (!searchPattern.EndsWith(Path.DirectorySeparatorChar + "*") && !Path.HasExtension(searchPattern))
else if (!searchPattern.EndsWith("*") && !Path.HasExtension(searchPattern))
searchPattern = Path.Combine(searchPattern, "*");

var regex = new Regex($"^{Regex.Escape(searchPattern).Replace("\\*", ".*?")}$");

var keys = _storage.Keys.Where(k => regex.IsMatch(k)).Select(k => _storage[k].Spec).ToList();

_logger.LogInformation("Deleting {FileCount} files matching {SearchPattern} (Regex={SearchPatternRegex})", keys.Count, searchPattern, regex);
Expand Down

0 comments on commit 18652f9

Please sign in to comment.