-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FileProviderImageProvider and WebRootImageProvider #243
Merged
JimBobSquarePants
merged 3 commits into
SixLabors:main
from
ronaldbarendse:feature/fileproviderimageprovider
Apr 4, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Extensions; | ||
using Microsoft.Extensions.FileProviders; | ||
using SixLabors.ImageSharp.Web.Resolvers; | ||
|
||
namespace SixLabors.ImageSharp.Web.Providers | ||
{ | ||
/// <summary> | ||
/// Returns images from an <see cref="IFileProvider"/> abstraction. | ||
/// </summary> | ||
public class FileProviderImageProvider : IImageProvider | ||
{ | ||
/// <summary> | ||
/// The file provider abstraction. | ||
/// </summary> | ||
private readonly IFileProvider fileProvider; | ||
|
||
/// <summary> | ||
/// Contains various format helper methods based on the current configuration. | ||
/// </summary> | ||
private readonly FormatUtilities formatUtilities; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FileProviderImageProvider"/> class. | ||
/// </summary> | ||
/// <param name="fileProvider">The file provider.</param> | ||
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param> | ||
public FileProviderImageProvider(IFileProvider fileProvider, FormatUtilities formatUtilities) | ||
{ | ||
this.fileProvider = fileProvider ?? throw new ArgumentNullException(nameof(fileProvider)); | ||
this.formatUtilities = formatUtilities ?? throw new ArgumentNullException(nameof(formatUtilities)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public ProcessingBehavior ProcessingBehavior { get; protected set; } = ProcessingBehavior.CommandOnly; | ||
|
||
/// <inheritdoc/> | ||
public Func<HttpContext, bool> Match { get; set; } = _ => true; | ||
|
||
/// <inheritdoc/> | ||
public bool IsValidRequest(HttpContext context) | ||
=> this.formatUtilities.TryGetExtensionFromUri(context.Request.GetDisplayUrl(), out _); | ||
|
||
/// <inheritdoc/> | ||
public Task<IImageResolver> GetAsync(HttpContext context) | ||
{ | ||
IFileInfo fileInfo = this.fileProvider.GetFileInfo(context.Request.Path); | ||
if (!fileInfo.Exists) | ||
{ | ||
return Task.FromResult<IImageResolver>(null); | ||
} | ||
|
||
return Task.FromResult<IImageResolver>(new FileProviderImageResolver(fileInfo)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace SixLabors.ImageSharp.Web.Providers | ||
{ | ||
/// <summary> | ||
/// Returns images from the web root file provider. | ||
/// </summary> | ||
public sealed class WebRootImageProvider : FileProviderImageProvider | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WebRootImageProvider"/> class. | ||
/// </summary> | ||
/// <param name="environment">The web hosting environment.</param> | ||
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param> | ||
public WebRootImageProvider( | ||
#if NETCOREAPP2_1 | ||
IHostingEnvironment environment, | ||
#else | ||
IWebHostEnvironment environment, | ||
#endif | ||
FormatUtilities formatUtilities) | ||
: base(environment.WebRootFileProvider, formatUtilities) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.FileProviders; | ||
|
||
namespace SixLabors.ImageSharp.Web.Resolvers | ||
{ | ||
/// <summary> | ||
/// Provides means to manage image buffers from an <see cref="IFileInfo"/> instance. | ||
/// </summary> | ||
public sealed class FileProviderImageResolver : IImageResolver | ||
{ | ||
private readonly IFileInfo fileInfo; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FileProviderImageResolver"/> class. | ||
/// </summary> | ||
/// <param name="fileInfo">The file info.</param> | ||
public FileProviderImageResolver(IFileInfo fileInfo) => this.fileInfo = fileInfo; | ||
|
||
/// <inheritdoc/> | ||
public Task<ImageMetadata> GetMetaDataAsync() => Task.FromResult(new ImageMetadata(this.fileInfo.LastModified.UtcDateTime, this.fileInfo.Length)); | ||
|
||
/// <inheritdoc/> | ||
public Task<Stream> OpenReadAsync() => Task.FromResult(this.fileInfo.CreateReadStream()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also do a null check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't bother. The default implementation is smart enough to only call this type when the
IFileInfo
is not null. If someone doesn't do this with their custom code then it's up to them.