Skip to content

Commit 4311f3d

Browse files
Use new base class for resolving
1 parent 8892d76 commit 4311f3d

5 files changed

Lines changed: 33 additions & 104 deletions

File tree

src/Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
<PackageReference Update="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
3232
<PackageReference Update="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
3333
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
34+
<PackageReference Update="Microsoft.Extensions.FileProviders.Physical" Version="2.2.0"/>
3435
</ItemGroup>
3536
</Project>

src/ImageSharp.Web/ImageSharp.Web.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<AssemblyTitle>SixLabors.ImageSharp.Web</AssemblyTitle>
@@ -41,6 +41,7 @@
4141
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
4242
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
4343
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
44+
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" />
4445
</ItemGroup>
4546

4647
<ItemGroup>

src/ImageSharp.Web/Providers/FileProviderImageProvider.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Web.Providers
1313
/// <summary>
1414
/// Returns images from an <see cref="IFileProvider"/> abstraction.
1515
/// </summary>
16-
public class FileProviderImageProvider : IImageProvider
16+
public abstract class FileProviderImageProvider : IImageProvider
1717
{
1818
/// <summary>
1919
/// The file provider abstraction.
@@ -30,20 +30,23 @@ public class FileProviderImageProvider : IImageProvider
3030
/// </summary>
3131
/// <param name="fileProvider">The file provider.</param>
3232
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
33-
public FileProviderImageProvider(IFileProvider fileProvider, FormatUtilities formatUtilities)
33+
protected FileProviderImageProvider(IFileProvider fileProvider, FormatUtilities formatUtilities)
3434
{
35-
this.fileProvider = fileProvider ?? throw new ArgumentNullException(nameof(fileProvider));
36-
this.formatUtilities = formatUtilities ?? throw new ArgumentNullException(nameof(formatUtilities));
35+
Guard.NotNull(fileProvider, nameof(fileProvider));
36+
Guard.NotNull(formatUtilities, nameof(formatUtilities));
37+
38+
this.fileProvider = fileProvider;
39+
this.formatUtilities = formatUtilities;
3740
}
3841

3942
/// <inheritdoc/>
40-
public ProcessingBehavior ProcessingBehavior { get; protected set; } = ProcessingBehavior.CommandOnly;
43+
public virtual ProcessingBehavior ProcessingBehavior { get; protected set; } = ProcessingBehavior.CommandOnly;
4144

4245
/// <inheritdoc/>
43-
public Func<HttpContext, bool> Match { get; set; } = _ => true;
46+
public virtual Func<HttpContext, bool> Match { get; set; } = _ => true;
4447

4548
/// <inheritdoc/>
46-
public bool IsValidRequest(HttpContext context)
49+
public virtual bool IsValidRequest(HttpContext context)
4750
=> this.formatUtilities.TryGetExtensionFromUri(context.Request.GetDisplayUrl(), out _);
4851

4952
/// <inheritdoc/>

src/ImageSharp.Web/Providers/PhysicalFileSystemProvider.cs

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,17 @@
33

44
using System;
55
using System.IO;
6-
using System.Threading.Tasks;
76
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Http.Extensions;
7+
using Microsoft.Extensions.FileProviders;
108
using Microsoft.Extensions.Options;
11-
using SixLabors.ImageSharp.Web.Resolvers;
129

1310
namespace SixLabors.ImageSharp.Web.Providers
1411
{
1512
/// <summary>
1613
/// Returns images stored in the local physical file system.
1714
/// </summary>
18-
public class PhysicalFileSystemProvider : IImageProvider
15+
public sealed class PhysicalFileSystemProvider : FileProviderImageProvider
1916
{
20-
/// <summary>
21-
/// The root path for the provider.
22-
/// </summary>
23-
private readonly string providerRootPath;
24-
25-
/// <summary>
26-
/// Contains various format helper methods based on the current configuration.
27-
/// </summary>
28-
private readonly FormatUtilities formatUtilities;
29-
3017
/// <summary>
3118
/// Initializes a new instance of the <see cref="PhysicalFileSystemProvider"/> class.
3219
/// </summary>
@@ -41,53 +28,23 @@ public PhysicalFileSystemProvider(
4128
IWebHostEnvironment environment,
4229
#endif
4330
FormatUtilities formatUtilities)
31+
: base(GetProvider(options?.Value, environment), formatUtilities)
4432
{
45-
Guard.NotNull(options, nameof(options));
46-
Guard.NotNull(environment, nameof(environment));
47-
48-
this.providerRootPath = GetProviderRoot(options.Value, environment.WebRootPath, environment.ContentRootPath);
49-
this.formatUtilities = formatUtilities;
5033
}
5134

5235
/// <inheritdoc/>
53-
public ProcessingBehavior ProcessingBehavior { get; } = ProcessingBehavior.CommandOnly;
54-
55-
/// <inheritdoc/>
56-
public Func<HttpContext, bool> Match { get; set; } = _ => true;
57-
58-
/// <inheritdoc/>
59-
public bool IsValidRequest(HttpContext context)
60-
=> this.formatUtilities.TryGetExtensionFromUri(context.Request.GetDisplayUrl(), out _);
61-
62-
/// <inheritdoc/>
63-
public Task<IImageResolver> GetAsync(HttpContext context)
64-
{
65-
// Use Join because request path starts with a slash
66-
string fullPath = Path.GetFullPath(Path.Join(this.providerRootPath, context.Request.Path.Value));
67-
if (PathUtilities.IsUnderneathRoot(fullPath, this.providerRootPath))
68-
{
69-
// Check to see if the file exists
70-
var fileInfo = new FileInfo(fullPath);
71-
if (fileInfo.Exists)
72-
{
73-
var metadata = new ImageMetadata(fileInfo.LastWriteTimeUtc, fileInfo.Length);
74-
return Task.FromResult<IImageResolver>(new PhysicalFileSystemResolver(fileInfo, metadata));
75-
}
76-
}
77-
78-
return Task.FromResult<IImageResolver>(null);
79-
}
36+
public override ProcessingBehavior ProcessingBehavior { get; protected set; } = ProcessingBehavior.All;
8037

8138
/// <summary>
8239
/// Determine the provider root path
8340
/// </summary>
84-
/// <param name="providerOptions">The provider options.</param>
41+
/// <param name="options">The provider options.</param>
8542
/// <param name="webRootPath">The web root path.</param>
8643
/// <param name="contentRootPath">The content root path.</param>
8744
/// <returns><see cref="string"/> representing the fully qualified provider root path.</returns>
88-
internal static string GetProviderRoot(PhysicalFileSystemProviderOptions providerOptions, string webRootPath, string contentRootPath)
45+
internal static string GetProviderRoot(PhysicalFileSystemProviderOptions options, string webRootPath, string contentRootPath)
8946
{
90-
string providerRootPath = providerOptions.ProviderRootPath ?? webRootPath;
47+
string providerRootPath = options.ProviderRootPath ?? webRootPath;
9148
if (string.IsNullOrEmpty(providerRootPath))
9249
{
9350
throw new InvalidOperationException("The provider root path can't be determined, make sure it's explicitly configured or the webroot is set.");
@@ -101,5 +58,18 @@ internal static string GetProviderRoot(PhysicalFileSystemProviderOptions provide
10158

10259
return PathUtilities.EnsureTrailingSlash(providerRootPath);
10360
}
61+
62+
private static PhysicalFileProvider GetProvider(
63+
PhysicalFileSystemProviderOptions options,
64+
#if NETCOREAPP2_1
65+
IHostingEnvironment environment)
66+
#else
67+
IWebHostEnvironment environment)
68+
#endif
69+
{
70+
Guard.NotNull(options, nameof(options));
71+
Guard.NotNull(environment, nameof(environment));
72+
return new(GetProviderRoot(options, environment.WebRootPath, environment.ContentRootPath));
73+
}
10474
}
10575
}

src/ImageSharp.Web/Resolvers/PhysicalFileSystemResolver.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)