Skip to content

Commit 8947219

Browse files
Use delimiter when listing blobs
1 parent 66d3001 commit 8947219

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/Umbraco.StorageProviders.AzureBlob/IO/AzureBlobFileSystem.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public IEnumerable<string> GetDirectories(string path)
9898
{
9999
ArgumentNullException.ThrowIfNull(path);
100100

101-
return ListBlobs(GetDirectoryPath(path))
101+
return ListBlobs(path)
102102
.Where(x => x.IsPrefix)
103-
.Select(x => GetRelativePath($"/{x.Prefix}").Trim('/'));
103+
.Select(x => GetRelativePath(x.Prefix).TrimEnd('/'));
104104
}
105105

106106
/// <inheritdoc />
@@ -118,13 +118,9 @@ public void DeleteDirectory(string path, bool recursive)
118118
{
119119
ArgumentNullException.ThrowIfNull(path);
120120

121-
foreach (var blob in ListBlobs(GetDirectoryPath(path)))
121+
foreach (BlobHierarchyItem blob in ListBlobs(path, true))
122122
{
123-
if (blob.IsPrefix)
124-
{
125-
DeleteDirectory(blob.Prefix, true);
126-
}
127-
else if (blob.IsBlob)
123+
if (blob.IsBlob)
128124
{
129125
_container.GetBlobClient(blob.Blob.Name).DeleteIfExists();
130126
}
@@ -137,7 +133,7 @@ public bool DirectoryExists(string path)
137133
{
138134
ArgumentNullException.ThrowIfNull(path);
139135

140-
return GetBlobClient(GetDirectoryPath(path)).Exists();
136+
return ListBlobs(path).Any();
141137
}
142138

143139
/// <inheritdoc />
@@ -159,10 +155,10 @@ public void AddFile(string path, Stream stream, bool overrideIfExists)
159155
ArgumentNullException.ThrowIfNull(path);
160156
ArgumentNullException.ThrowIfNull(stream);
161157

162-
var blob = GetBlobClient(path);
158+
BlobClient blob = GetBlobClient(path);
163159
if (!overrideIfExists && blob.Exists())
164160
{
165-
throw new InvalidOperationException($"A file at path '{path}' already exists");
161+
throw new InvalidOperationException($"A file at path '{path}' already exists.");
166162
}
167163

168164
var headers = new BlobHttpHeaders();
@@ -187,10 +183,10 @@ public void AddFile(string path, string physicalPath, bool overrideIfExists = tr
187183
ArgumentNullException.ThrowIfNull(path);
188184
ArgumentNullException.ThrowIfNull(physicalPath);
189185

190-
var destinationBlob = GetBlobClient(path);
186+
BlobClient destinationBlob = GetBlobClient(path);
191187
if (!overrideIfExists && destinationBlob.Exists())
192188
{
193-
throw new InvalidOperationException($"A file at path '{path}' already exists");
189+
throw new InvalidOperationException($"A file at path '{path}' already exists.");
194190
}
195191

196192
var sourceBlob = GetBlobClient(physicalPath);
@@ -227,15 +223,15 @@ public IEnumerable<string> GetFiles(string path, string? filter)
227223
{
228224
ArgumentNullException.ThrowIfNull(path);
229225

230-
var files = ListBlobs(GetDirectoryPath(path)).Where(x => x.IsBlob).Select(x => x.Blob.Name);
226+
IEnumerable<string> files = ListBlobs(path).Where(x => x.IsBlob).Select(x => x.Blob.Name);
231227
if (!string.IsNullOrEmpty(filter) && filter != "*.*")
232228
{
233229
// TODO: Might be better to use a globbing library
234230
filter = filter.TrimStart("*");
235231
files = files.Where(x => x.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) > -1);
236232
}
237233

238-
return files.Select(x => GetRelativePath($"/{x}"));
234+
return files.Select(GetRelativePath);
239235
}
240236

241237
/// <inheritdoc />
@@ -337,7 +333,7 @@ public BlobClient GetBlobClient(string path)
337333
{
338334
ArgumentNullException.ThrowIfNull(path);
339335

340-
return _container.GetBlobClient(GetBlobPath(path));
336+
return _container.GetBlobClient(GetBlobName(path));
341337
}
342338

343339
/// <inheritdoc />
@@ -346,19 +342,15 @@ public BlobClient GetBlobClient(string path)
346342
private static string EnsureUrlSeparatorChar(string path)
347343
=> path.Replace("\\", "/", StringComparison.InvariantCultureIgnoreCase);
348344

349-
private string GetDirectoryPath(string fullPathOrUrl)
345+
private Pageable<BlobHierarchyItem> ListBlobs(string path, bool recursive = false)
350346
{
351-
var path = GetFullPath(fullPathOrUrl);
347+
string? delimiter = recursive ? null : "/";
348+
string prefix = GetFullPath(path).EnsureEndsWith('/');
352349

353-
return path.Length == 0 ? path : path.EnsureEndsWith('/');
354-
}
355-
356-
private IEnumerable<BlobHierarchyItem> ListBlobs(string path)
357-
{
358-
return _container.GetBlobsByHierarchy(prefix: path);
350+
return _container.GetBlobsByHierarchy(delimiter: delimiter, prefix: prefix);
359351
}
360352

361-
private string GetBlobPath(string path)
353+
private string GetBlobName(string path)
362354
{
363355
path = EnsureUrlSeparatorChar(path);
364356

0 commit comments

Comments
 (0)