Skip to content

Commit 7efe58f

Browse files
Remove usage of IFileProvider in IImageProvider
1 parent 94cdcc5 commit 7efe58f

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

src/ImageSharp.Web/Providers/PhysicalFileSystemProvider.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.AspNetCore.Http.Extensions;
10-
using Microsoft.Extensions.FileProviders;
1110
using Microsoft.Extensions.Options;
1211
using SixLabors.ImageSharp.Web.Resolvers;
1312

@@ -19,9 +18,9 @@ namespace SixLabors.ImageSharp.Web.Providers
1918
public class PhysicalFileSystemProvider : IImageProvider
2019
{
2120
/// <summary>
22-
/// The file provider abstraction.
21+
/// The root path for the provider.
2322
/// </summary>
24-
private readonly IFileProvider fileProvider;
23+
private readonly string providerRootPath;
2524

2625
/// <summary>
2726
/// Contains various format helper methods based on the current configuration.
@@ -44,18 +43,12 @@ public PhysicalFileSystemProvider(
4443
FormatUtilities formatUtilities)
4544
{
4645
Guard.NotNull(environment, nameof(environment));
47-
48-
// ContentRootPath is never null.
49-
// https://github.com/dotnet/aspnetcore/blob/b89eba6c3cda331ee98063e3c4a04267ec540315/src/Hosting/Hosting/src/WebHostBuilder.cs#L262
5046
Guard.NotNullOrWhiteSpace(environment.WebRootPath, nameof(environment.WebRootPath));
5147

5248
// Allow configuration of the provider without having to register everything
5349
PhysicalFileSystemProviderOptions providerOptions = options != null ? options.Value : new();
54-
string providerRootPath = GetProviderRoot(providerOptions, environment.WebRootPath, environment.ContentRootPath);
50+
this.providerRootPath = GetProviderRoot(providerOptions, environment.WebRootPath, environment.ContentRootPath);
5551

56-
// Ensure provider directory is created before initializing the file provider
57-
Directory.CreateDirectory(providerRootPath);
58-
this.fileProvider = new PhysicalFileProvider(providerRootPath);
5952
this.formatUtilities = formatUtilities;
6053
}
6154

@@ -72,16 +65,16 @@ public bool IsValidRequest(HttpContext context)
7265
/// <inheritdoc/>
7366
public Task<IImageResolver> GetAsync(HttpContext context)
7467
{
75-
// Path has already been correctly parsed before here.
76-
IFileInfo fileInfo = this.fileProvider.GetFileInfo(context.Request.Path.Value);
68+
string path = Path.Join(this.providerRootPath, context.Request.Path.Value);
7769

78-
// Check to see if the file exists.
70+
// Check to see if the file exists
71+
var fileInfo = new FileInfo(path);
7972
if (!fileInfo.Exists)
8073
{
8174
return Task.FromResult<IImageResolver>(null);
8275
}
8376

84-
var metadata = new ImageMetadata(fileInfo.LastModified.UtcDateTime, fileInfo.Length);
77+
var metadata = new ImageMetadata(fileInfo.LastWriteTimeUtc, fileInfo.Length);
8578
return Task.FromResult<IImageResolver>(new PhysicalFileSystemResolver(fileInfo, metadata));
8679
}
8780

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.IO;
55
using System.Threading.Tasks;
6-
using Microsoft.Extensions.FileProviders;
76

87
namespace SixLabors.ImageSharp.Web.Resolvers
98
{
@@ -12,15 +11,15 @@ namespace SixLabors.ImageSharp.Web.Resolvers
1211
/// </summary>
1312
public class PhysicalFileSystemResolver : IImageResolver
1413
{
15-
private readonly IFileInfo fileInfo;
14+
private readonly FileInfo fileInfo;
1615
private readonly ImageMetadata metadata;
1716

1817
/// <summary>
1918
/// Initializes a new instance of the <see cref="PhysicalFileSystemResolver"/> class.
2019
/// </summary>
2120
/// <param name="fileInfo">The input file info.</param>
2221
/// <param name="metadata">The image metadata associated with this file.</param>
23-
public PhysicalFileSystemResolver(IFileInfo fileInfo, in ImageMetadata metadata)
22+
public PhysicalFileSystemResolver(FileInfo fileInfo, in ImageMetadata metadata)
2423
{
2524
this.fileInfo = fileInfo;
2625
this.metadata = metadata;
@@ -30,6 +29,6 @@ public PhysicalFileSystemResolver(IFileInfo fileInfo, in ImageMetadata metadata)
3029
public Task<ImageMetadata> GetMetaDataAsync() => Task.FromResult(this.metadata);
3130

3231
/// <inheritdoc/>
33-
public Task<Stream> OpenReadAsync() => Task.FromResult(this.fileInfo.CreateReadStream());
32+
public Task<Stream> OpenReadAsync() => Task.FromResult<Stream>(this.fileInfo.OpenRead());
3433
}
3534
}

0 commit comments

Comments
 (0)