Skip to content

Commit 47ddfe5

Browse files
committed
Add temporary directory cleanup in file saving process
1 parent ed5f254 commit 47ddfe5

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.macios.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public sealed partial class FileSaverImplementation : IFileSaver, IDisposable
1010
{
1111
UIDocumentPickerViewController? documentPickerViewController;
1212
TaskCompletionSource<string>? taskCompetedSource;
13+
NSUrl? tempDirectoryPath;
1314

1415
/// <inheritdoc />
1516
public void Dispose()
@@ -21,7 +22,7 @@ async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream
2122
{
2223
cancellationToken.ThrowIfCancellationRequested();
2324
var fileManager = NSFileManager.DefaultManager;
24-
var tempDirectoryPath = fileManager.GetTemporaryDirectory().Append(Guid.NewGuid().ToString(), true);
25+
tempDirectoryPath = fileManager.GetTemporaryDirectory().Append(Guid.NewGuid().ToString(), true);
2526
var isDirectoryCreated = fileManager.CreateDirectory(tempDirectoryPath, true, null, out var error);
2627
if (!isDirectoryCreated)
2728
{
@@ -63,6 +64,7 @@ Task<string> InternalSaveAsync(string fileName, Stream stream, IProgress<double>
6364
void DocumentPickerViewControllerOnWasCancelled(object? sender, EventArgs e)
6465
{
6566
taskCompetedSource?.TrySetException(new FileSaveException("Operation cancelled."));
67+
CleanupTempDirectory();
6668
InternalDispose();
6769
}
6870

@@ -74,9 +76,20 @@ void DocumentPickerViewControllerOnDidPickDocumentAtUrls(object? sender, UIDocum
7476
}
7577
finally
7678
{
79+
CleanupTempDirectory();
7780
InternalDispose();
7881
}
7982
}
83+
84+
void CleanupTempDirectory()
85+
{
86+
if (tempDirectoryPath is not null)
87+
{
88+
var fileManager = NSFileManager.DefaultManager;
89+
fileManager.Remove(tempDirectoryPath, out var _);
90+
}
91+
}
92+
8093

8194
void InternalDispose()
8295
{

0 commit comments

Comments
 (0)