Skip to content

Commit a628490

Browse files
author
Warren Buckley
committed
Allow sub-directories to be deleted when calling DeleteDirectory. Was failing when trying to tidy up my unit test with subfolders
1 parent b6f5239 commit a628490

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/UmbracoFileSystemProviders.Azure.Tests/AzureBlobFileSystemTestsBase.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public void TestGetDirectories()
294294
IEnumerable<string> actual = provider.GetDirectories("/");
295295

296296
// Assert
297-
string[] expected = { "1010/", "1011/", "1012/", "forms/", "testvalid/" };
297+
string[] expected = { "1010/", "1011/", "1012/" };
298298
Assert.IsTrue(expected.SequenceEqual(actual));
299299
}
300300

@@ -315,7 +315,7 @@ public void TestGetDirectoriesPrefixed()
315315
IEnumerable<string> actual = provider.GetDirectories("/");
316316

317317
// Assert
318-
string[] expected = { "1010/", "1011/", "1012/", "testvalid/" };
318+
string[] expected = { "1010/", "1011/", "1012/" };
319319
Assert.IsTrue(expected.SequenceEqual(actual));
320320
}
321321

@@ -533,6 +533,9 @@ public void TestGetSubDirectories()
533533
// Assert
534534
string[] expected = { "forms/form_123/", "forms/form_456/" };
535535
Assert.IsTrue(expected.SequenceEqual(actual));
536+
537+
// Tidy up after test
538+
provider.DeleteDirectory("forms");
536539
}
537540

538541
/// <summary>
@@ -559,10 +562,11 @@ public void TestGetSubDirectoriesAndFiles()
559562
}
560563

561564
// Assert
562-
string[] expected = { "forms/form_123/kitty.jpg", "forms/form_123/dog.jpg", "forms/form_456/panda.jpg" };
565+
string[] expected = { "forms/form_123/dog.jpg", "forms/form_123/kitty.jpg", "forms/form_456/panda.jpg" };
563566
Assert.IsTrue(expected.SequenceEqual(actual));
564-
}
565-
566567

568+
// Tidy up after test
569+
provider.DeleteDirectory("forms");
570+
}
567571
}
568572
}

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,31 @@ public void DeleteDirectory(string path, bool recursive)
277277

278278
CloudBlobDirectory directory = this.GetDirectoryReference(path);
279279

280-
IEnumerable<CloudBlockBlob> blobs = directory.ListBlobs().OfType<CloudBlockBlob>();
280+
// WB: This will only delete a folder if it only has files & not sub directories
281+
// IEnumerable<CloudBlockBlob> blobs = directory.ListBlobs().OfType<CloudBlockBlob>();
282+
283+
IEnumerable<IListBlobItem> blobs = directory.ListBlobs();
284+
281285

282286
if (recursive)
283287
{
284-
foreach (CloudBlockBlob blockBlob in blobs)
288+
foreach (var blobItem in blobs)
285289
{
286290
try
287291
{
288-
blockBlob.DeleteIfExists(DeleteSnapshotsOption.IncludeSnapshots);
292+
if (blobItem is CloudBlobDirectory)
293+
{
294+
var blobFolder = blobItem as CloudBlobDirectory;
295+
296+
// Resurssively call this method
297+
this.DeleteDirectory(blobFolder.Prefix);
298+
}
299+
else
300+
{
301+
// Can assume its a file aka CloudBlob
302+
var blobFile = blobItem as CloudBlockBlob;
303+
blobFile?.DeleteIfExists(DeleteSnapshotsOption.IncludeSnapshots);
304+
}
289305
}
290306
catch (Exception ex)
291307
{

0 commit comments

Comments
 (0)