Skip to content

Make UploadedFileStorage and ReturnedFileStorage more strict in deleting files #1934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ private async Task StoreMetadata(Guid id, ReturnedFileMetadata metadata)

private string GetDataFilePath(Guid id)
{
return Path.Combine(TempDirectory, id + ".data");
return Path.Combine(TempDirectory, $"dotvvm-returned-file-{id}.data");
}

private string GetMetadataFilePath(Guid id)
{
return Path.Combine(TempDirectory, id + ".metadata");
return Path.Combine(TempDirectory, $"dotvvm-returned-file-{id}.metadata");
}

public Task<ReturnedFile> GetFileAsync(Guid id)
Expand Down Expand Up @@ -142,7 +142,10 @@ public Task DeleteFileAsync(Guid id)

public void DeleteOldFiles(DateTime maxCreatedDate)
{
var files = Directory.GetFiles(TempDirectory).Where(t => File.GetCreationTime(t) < maxCreatedDate);
var files = Directory.GetFiles(TempDirectory)
.Where(t => File.GetCreationTime(t) < maxCreatedDate)
.Where(t => Path.GetFileName(t).StartsWith("dotvvm-returned-file-", StringComparison.OrdinalIgnoreCase));

foreach (var file in files)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class FileSystemUploadedFileStorage : IUploadedFileStorage, IDisposable
/// </summary>
public FileSystemUploadedFileStorage(string tempDirectory, TimeSpan autoDeleteInterval)
{
if (string.IsNullOrEmpty(tempDirectory))
{
throw new ArgumentNullException(nameof(tempDirectory));
}

TempDirectory = tempDirectory;
AutoDeleteInterval = autoDeleteInterval;

Expand Down Expand Up @@ -95,6 +100,7 @@ public void DeleteOldFiles(DateTime maxCreatedDate)
{
files = Directory.GetFiles(TempDirectory)
.Where(t => File.GetCreationTime(t) < maxCreatedDate)
.Where(t => Path.GetFileName(t).StartsWith("dotvvm-uploaded-file-", StringComparison.OrdinalIgnoreCase))
.ToList();
}
catch (IOException)
Expand All @@ -121,7 +127,7 @@ public void DeleteOldFiles(DateTime maxCreatedDate)
/// </summary>
private string GetFileName(Guid id)
{
return Path.Combine(TempDirectory, id + ".tmp");
return Path.Combine(TempDirectory, $"dotvvm-uploaded-file-{id}.tmp");
}


Expand Down
Loading