Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/ImageSharp.Web/Providers/FileProviderImageProvider.cs
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));
}
}
}
29 changes: 29 additions & 0 deletions src/ImageSharp.Web/Providers/WebRootImageProvider.cs
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)
{
}
}
}
29 changes: 29 additions & 0 deletions src/ImageSharp.Web/Resolvers/FileProviderImageResolver.cs
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;
Copy link
Contributor Author

@ronaldbarendse ronaldbarendse Apr 2, 2022

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?

Suggested change
public FileProviderImageResolver(IFileInfo fileInfo) => this.fileInfo = fileInfo;
public FileProviderImageResolver(IFileInfo fileInfo) => this.fileInfo = fileInfo ?? throw new ArgumentNullException(nameof(fileInfo));

Copy link
Member

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.


/// <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());
}
}