Skip to content

Commit db4b093

Browse files
authored
Merge pull request #48 from warrenbuckley/Paths
Path FixUp & SubDirectories
2 parents 34271a4 + 4914a27 commit db4b093

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

src/UmbracoFileSystemProviders.Azure.Tests/AzureBlobFileSystemTestsBase.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void TestGetDirectoriesPrefixed()
318318
string[] expected = { "1010", "1011", "1012" };
319319
Assert.IsTrue(expected.SequenceEqual(actual));
320320
}
321-
321+
322322
/// <summary>
323323
/// Asserts that the file system correctly returns a sequence of files from the root
324324
/// container in the correct format.
@@ -455,6 +455,24 @@ 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+
// Tidy up after test
466+
provider.DeleteDirectory("testvalid");
467+
}
468+
469+
[Test]
470+
public void TestInvalidDirectory()
471+
{
472+
AzureBlobFileSystem provider = this.CreateAzureBlobFileSystem();
473+
Assert.IsFalse(provider.DirectoryExists("testinvalid/"));
474+
}
475+
458476
/// <summary>
459477
/// Asserts that the file system correctly deletes a directory when the input has been prefixed.
460478
/// </summary>
@@ -498,5 +516,60 @@ public void TestDeleteDirectoryRelative()
498516
Assert.IsFalse(provider.DirectoryExists("media/1010/"));
499517
Assert.IsFalse(provider.FileExists("media/1010/media.jpg"));
500518
}
519+
520+
/// <summary>
521+
/// Asserts that the file system correctly returns a sequence of sub-directories in the
522+
/// correct format.
523+
/// </summary>
524+
[Test]
525+
public void TestGetSubDirectories()
526+
{
527+
// Arrange
528+
AzureBlobFileSystem provider = this.CreateAzureBlobFileSystem();
529+
provider.AddFile("forms/form_123/kitty.jpg", Stream.Null);
530+
provider.AddFile("forms/form_123/dog.jpg", Stream.Null);
531+
provider.AddFile("forms/form_456/panda.jpg", Stream.Null);
532+
533+
// Act
534+
IEnumerable<string> actual = provider.GetDirectories("forms");
535+
536+
// Assert
537+
string[] expected = { "forms/form_123", "forms/form_456" };
538+
Assert.IsTrue(expected.SequenceEqual(actual));
539+
540+
// Tidy up after test
541+
provider.DeleteDirectory("forms");
542+
}
543+
544+
/// <summary>
545+
/// Asserts that the file system correctly returns a sequence of sub-directories in the
546+
/// correct format.
547+
/// </summary>
548+
[Test]
549+
public void TestGetSubDirectoriesAndFiles()
550+
{
551+
// Arrange
552+
AzureBlobFileSystem provider = this.CreateAzureBlobFileSystem();
553+
provider.AddFile("forms/form_123/kitty.jpg", Stream.Null);
554+
provider.AddFile("forms/form_123/dog.jpg", Stream.Null);
555+
provider.AddFile("forms/form_456/panda.jpg", Stream.Null);
556+
557+
// Act
558+
var subfolders = provider.GetDirectories("forms");
559+
var actual = new List<string>();
560+
561+
foreach (var folder in subfolders)
562+
{
563+
// Get files in subfolder and add to a single collection
564+
actual.AddRange(provider.GetFiles(folder));
565+
}
566+
567+
// Assert
568+
string[] expected = { "forms/form_123/dog.jpg", "forms/form_123/kitty.jpg", "forms/form_456/panda.jpg" };
569+
Assert.IsTrue(expected.SequenceEqual(actual));
570+
571+
// Tidy up after test
572+
provider.DeleteDirectory("forms");
573+
}
501574
}
502575
}

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 25 additions & 7 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.Delete(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
{
@@ -337,7 +353,10 @@ public void DeleteFile(string path)
337353
/// </returns>
338354
public bool DirectoryExists(string path)
339355
{
340-
return this.GetDirectories(path).Any();
356+
var fixedPath = this.FixPath(path);
357+
var directory = this.cloudBlobContainer.GetDirectoryReference(fixedPath);
358+
359+
return directory.ListBlobs().Any();
341360
}
342361

343362
/// <summary>
@@ -385,11 +404,10 @@ public IEnumerable<string> GetDirectories(string path)
385404
{
386405
CloudBlobDirectory directory = this.GetDirectoryReference(path);
387406

388-
IEnumerable<IListBlobItem> blobs = directory.ListBlobs();
407+
IEnumerable<IListBlobItem> blobs = directory.ListBlobs().Where(blob => blob is CloudBlobDirectory).ToList();
389408

390409
// Always get last segment for media sub folder simulation. E.g 1001, 1002
391-
return blobs.Select(cd =>
392-
cd.Uri.Segments[cd.Uri.Segments.Length - 1].Split(Delimiter.ToCharArray())[0]);
410+
return blobs.Cast<CloudBlobDirectory>().Select(cd => cd.Prefix.TrimEnd('/'));
393411
}
394412

395413
/// <summary>

0 commit comments

Comments
 (0)