Skip to content

Commit f575fe1

Browse files
committed
Ensuring that GetDirectories only returns directories
1 parent 34271a4 commit f575fe1

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/UmbracoFileSystemProviders.Azure.Tests/AzureBlobFileSystemTestsBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,21 @@ public void TestDeleteDirectory()
455455
Assert.IsFalse(provider.FileExists("1010/media.jpg"));
456456
}
457457

458+
[Test]
459+
public void TestValidDirectory()
460+
{
461+
AzureBlobFileSystem provider = this.CreateAzureBlobFileSystem();
462+
provider.AddFile("testvalid/test.txt", Stream.Null);
463+
Assert.IsTrue(provider.DirectoryExists("testvalid"));
464+
}
465+
466+
[Test]
467+
public void TestInvalidDirectory()
468+
{
469+
AzureBlobFileSystem provider = this.CreateAzureBlobFileSystem();
470+
Assert.IsFalse(provider.DirectoryExists("testinvalid/"));
471+
}
472+
458473
/// <summary>
459474
/// Asserts that the file system correctly deletes a directory when the input has been prefixed.
460475
/// </summary>

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void DeleteDirectory(string path, bool recursive)
285285
{
286286
try
287287
{
288-
blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
288+
blockBlob.DeleteIfExists(DeleteSnapshotsOption.IncludeSnapshots);
289289
}
290290
catch (Exception ex)
291291
{
@@ -337,7 +337,10 @@ public void DeleteFile(string path)
337337
/// </returns>
338338
public bool DirectoryExists(string path)
339339
{
340-
return this.GetDirectories(path).Any();
340+
var fixedPath = this.FixPath(path);
341+
var directory = this.cloudBlobContainer.GetDirectoryReference(fixedPath);
342+
343+
return directory.ListBlobs().Any();
341344
}
342345

343346
/// <summary>
@@ -385,11 +388,12 @@ public IEnumerable<string> GetDirectories(string path)
385388
{
386389
CloudBlobDirectory directory = this.GetDirectoryReference(path);
387390

388-
IEnumerable<IListBlobItem> blobs = directory.ListBlobs();
391+
IEnumerable<IListBlobItem> blobs = directory.ListBlobs().Where(blob => blob is CloudBlobDirectory);
389392

390393
// Always get last segment for media sub folder simulation. E.g 1001, 1002
391394
return blobs.Select(cd =>
392395
cd.Uri.Segments[cd.Uri.Segments.Length - 1].Split(Delimiter.ToCharArray())[0]);
396+
393397
}
394398

395399
/// <summary>

0 commit comments

Comments
 (0)