33
44using System ;
55using System . IO ;
6- using System . Threading . Tasks ;
76using Microsoft . AspNetCore . Hosting ;
8- using Microsoft . AspNetCore . Http ;
9- using Microsoft . AspNetCore . Http . Extensions ;
7+ using Microsoft . Extensions . FileProviders ;
108using Microsoft . Extensions . Options ;
11- using SixLabors . ImageSharp . Web . Resolvers ;
129
1310namespace 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}
0 commit comments