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,56 +28,23 @@ public PhysicalFileSystemProvider(
4128 IWebHostEnvironment environment ,
4229#endif
4330 FormatUtilities formatUtilities )
31+ : base ( GetProvider ( options , environment ) , options . Value . ProcessingBehavior , 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 ;
50- }
51-
52- /// <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 ) ;
7933 }
8034
8135 /// <summary>
8236 /// Determine the provider root path
8337 /// </summary>
84- /// <param name="providerOptions ">The provider options.</param>
38+ /// <param name="options ">The provider options.</param>
8539 /// <param name="webRootPath">The web root path.</param>
8640 /// <param name="contentRootPath">The content root path.</param>
8741 /// <returns><see cref="string"/> representing the fully qualified provider root path.</returns>
88- internal static string GetProviderRoot ( PhysicalFileSystemProviderOptions providerOptions , string webRootPath , string contentRootPath )
42+ internal static string GetProviderRoot ( PhysicalFileSystemProviderOptions options , string webRootPath , string contentRootPath )
8943 {
90- string providerRootPath = providerOptions . ProviderRootPath ?? webRootPath ;
44+ string providerRootPath = options . ProviderRootPath ?? webRootPath ;
9145 if ( string . IsNullOrEmpty ( providerRootPath ) )
9246 {
93- throw new InvalidOperationException ( "The provider root path can't be determined, make sure it's explicitly configured or the webroot is set." ) ;
47+ throw new InvalidOperationException ( "The provider root path cannot be determined, make sure it's explicitly configured or the webroot is set." ) ;
9448 }
9549
9650 if ( ! Path . IsPathFullyQualified ( providerRootPath ) )
@@ -101,5 +55,18 @@ internal static string GetProviderRoot(PhysicalFileSystemProviderOptions provide
10155
10256 return PathUtilities . EnsureTrailingSlash ( providerRootPath ) ;
10357 }
58+
59+ private static PhysicalFileProvider GetProvider (
60+ IOptions < PhysicalFileSystemProviderOptions > options ,
61+ #if NETCOREAPP2_1
62+ IHostingEnvironment environment )
63+ #else
64+ IWebHostEnvironment environment)
65+ #endif
66+ {
67+ Guard . NotNull ( options , nameof ( options ) ) ;
68+ Guard . NotNull ( environment , nameof ( environment ) ) ;
69+ return new ( GetProviderRoot ( options . Value , environment . WebRootPath , environment . ContentRootPath ) ) ;
70+ }
10471 }
10572}
0 commit comments