Skip to content

Commit 93dc059

Browse files
Rename rootUrl to requestRootPath and consolidate constructors
1 parent 6bac375 commit 93dc059

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Umbraco.StorageProviders.AzureBlob.IO;
1212
/// <inheritdoc />
1313
public sealed class AzureBlobFileSystem : IAzureBlobFileSystem, IFileProviderFactory
1414
{
15-
private readonly string _rootUrl;
15+
private readonly string _requestRootPath;
1616
private readonly string _containerRootPath;
1717
private readonly BlobContainerClient _container;
1818
private readonly IIOHelper _ioHelper;
@@ -21,7 +21,7 @@ public sealed class AzureBlobFileSystem : IAzureBlobFileSystem, IFileProviderFac
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="AzureBlobFileSystem"/> class.
2323
/// </summary>
24-
/// <param name="options">The options.</param>
24+
/// <param name="options">The Azure Blob File System options.</param>
2525
/// <param name="hostingEnvironment">The hosting environment.</param>
2626
/// <param name="ioHelper">The I/O helper.</param>
2727
/// <param name="contentTypeProvider">The content type provider.</param>
@@ -30,35 +30,27 @@ public sealed class AzureBlobFileSystem : IAzureBlobFileSystem, IFileProviderFac
3030
/// <exception cref="System.ArgumentNullException"><paramref name="ioHelper" /> is <c>null</c>.</exception>
3131
/// <exception cref="System.ArgumentNullException"><paramref name="contentTypeProvider" /> is <c>null</c>.</exception>
3232
public AzureBlobFileSystem(AzureBlobFileSystemOptions options, IHostingEnvironment hostingEnvironment, IIOHelper ioHelper, IContentTypeProvider contentTypeProvider)
33-
{
34-
ArgumentNullException.ThrowIfNull(options);
35-
ArgumentNullException.ThrowIfNull(hostingEnvironment);
36-
37-
_rootUrl = EnsureUrlSeparatorChar(hostingEnvironment.ToAbsolute(options.VirtualPath)).TrimEnd('/');
38-
_containerRootPath = options.ContainerRootPath ?? _rootUrl;
39-
_container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
40-
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
41-
_contentTypeProvider = contentTypeProvider ?? throw new ArgumentNullException(nameof(contentTypeProvider));
42-
}
33+
: this(GetRequestRootPath(options, hostingEnvironment), new BlobContainerClient(options.ConnectionString, options.ContainerName), ioHelper, contentTypeProvider, options.ContainerRootPath)
34+
{ }
4335

4436
/// <summary>
4537
/// Initializes a new instance of the <see cref="AzureBlobFileSystem"/> class.
4638
/// </summary>
47-
/// <param name="rootUrl">The root URL.</param>
39+
/// <param name="requestRootPath">The request/URL root path.</param>
4840
/// <param name="blobContainerClient">The blob container client.</param>
4941
/// <param name="ioHelper">The I/O helper.</param>
5042
/// <param name="contentTypeProvider">The content type provider.</param>
51-
/// <param name="containerRootPath">The container root path (uses the root URL if not set).</param>
52-
/// <exception cref="System.ArgumentNullException"><paramref name="rootUrl" /> is <c>null</c>.</exception>
43+
/// <param name="containerRootPath">The container root path (uses the request/URL root path if not set).</param>
44+
/// <exception cref="System.ArgumentNullException"><paramref name="requestRootPath" /> is <c>null</c>.</exception>
5345
/// <exception cref="System.ArgumentNullException"><paramref name="blobContainerClient" /> is <c>null</c>.</exception>
5446
/// <exception cref="System.ArgumentNullException"><paramref name="ioHelper" /> is <c>null</c>.</exception>
5547
/// <exception cref="System.ArgumentNullException"><paramref name="contentTypeProvider" /> is <c>null</c>.</exception>
56-
public AzureBlobFileSystem(string rootUrl, BlobContainerClient blobContainerClient, IIOHelper ioHelper, IContentTypeProvider contentTypeProvider, string? containerRootPath = null)
48+
public AzureBlobFileSystem(string requestRootPath, BlobContainerClient blobContainerClient, IIOHelper ioHelper, IContentTypeProvider contentTypeProvider, string? containerRootPath = null)
5749
{
58-
ArgumentNullException.ThrowIfNull(rootUrl);
50+
ArgumentNullException.ThrowIfNull(requestRootPath);
5951

60-
_rootUrl = EnsureUrlSeparatorChar(rootUrl).TrimEnd('/');
61-
_containerRootPath = containerRootPath ?? _rootUrl;
52+
_requestRootPath = EnsureUrlSeparatorChar(requestRootPath).TrimEnd('/');
53+
_containerRootPath = containerRootPath ?? _requestRootPath;
6254
_container = blobContainerClient ?? throw new ArgumentNullException(nameof(blobContainerClient));
6355
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
6456
_contentTypeProvider = contentTypeProvider ?? throw new ArgumentNullException(nameof(contentTypeProvider));
@@ -275,11 +267,11 @@ public string GetRelativePath(string fullPathOrUrl)
275267
// test url
276268
var path = EnsureUrlSeparatorChar(fullPathOrUrl); // ensure url separator char
277269

278-
// if it starts with the root url, strip it and trim the starting slash to make it relative
270+
// if it starts with the request/URL root path, strip it and trim the starting slash to make it relative
279271
// eg "/Media/1234/img.jpg" => "1234/img.jpg"
280-
if (_ioHelper.PathStartsWith(path, _rootUrl, '/'))
272+
if (_ioHelper.PathStartsWith(path, _requestRootPath, '/'))
281273
{
282-
path = path[_rootUrl.Length..].TrimStart('/');
274+
path = path[_requestRootPath.Length..].TrimStart('/');
283275
}
284276

285277
// unchanged - what else?
@@ -293,16 +285,17 @@ public string GetFullPath(string path)
293285
ArgumentNullException.ThrowIfNull(path);
294286

295287
path = EnsureUrlSeparatorChar(path);
296-
return (_ioHelper.PathStartsWith(path, _rootUrl, '/') ? path : $"{_rootUrl}/{path}").Trim('/');
288+
return (_ioHelper.PathStartsWith(path, _requestRootPath, '/') ? path : $"{_requestRootPath}/{path}").Trim('/');
297289
}
298290

299291
/// <inheritdoc />
300292
/// <exception cref="System.ArgumentNullException"><paramref name="path" /> is <c>null</c>.</exception>
293+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1055:URI-like return values should not be strings", Justification = "This method is inherited from an interface.")]
301294
public string GetUrl(string? path)
302295
{
303296
ArgumentNullException.ThrowIfNull(path);
304297

305-
return $"{_rootUrl}/{EnsureUrlSeparatorChar(path).Trim('/')}";
298+
return $"{_requestRootPath}/{EnsureUrlSeparatorChar(path).Trim('/')}";
306299
}
307300

308301
/// <inheritdoc />
@@ -344,6 +337,14 @@ public BlobClient GetBlobClient(string path)
344337
/// <inheritdoc />
345338
public IFileProvider Create() => new AzureBlobFileProvider(_container, _containerRootPath);
346339

340+
private static string GetRequestRootPath(AzureBlobFileSystemOptions options, IHostingEnvironment hostingEnvironment)
341+
{
342+
ArgumentNullException.ThrowIfNull(options);
343+
ArgumentNullException.ThrowIfNull(hostingEnvironment);
344+
345+
return hostingEnvironment.ToAbsolute(options.VirtualPath);
346+
}
347+
347348
private static string EnsureUrlSeparatorChar(string path)
348349
=> path.Replace("\\", "/", StringComparison.InvariantCultureIgnoreCase);
349350

@@ -354,7 +355,7 @@ private string GetDirectoryPath(string fullPathOrUrl)
354355
return path.Length == 0 ? path : path.EnsureEndsWith('/');
355356
}
356357

357-
private IEnumerable<BlobHierarchyItem> ListBlobs(string path)
358+
private Pageable<BlobHierarchyItem> ListBlobs(string path)
358359
=> _container.GetBlobsByHierarchy(prefix: path);
359360

360361
private string GetBlobPath(string path)
@@ -368,10 +369,10 @@ private string GetBlobPath(string path)
368369
return path;
369370
}
370371

371-
if (_ioHelper.PathStartsWith(path, _rootUrl, '/'))
372+
if (_ioHelper.PathStartsWith(path, _requestRootPath, '/'))
372373
{
373-
// Remove root URL from path (e.g. /media/abc123/file.ext to /abc123/file.ext)
374-
path = path[_rootUrl.Length..];
374+
// Remove request/URL root path from path (e.g. /media/abc123/file.ext to /abc123/file.ext)
375+
path = path[_requestRootPath.Length..];
375376
}
376377

377378
path = $"{_containerRootPath}/{path.TrimStart('/')}";

0 commit comments

Comments
 (0)