/
FileProviderImageProvider.cs
66 lines (56 loc) · 2.46 KB
/
FileProviderImageProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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 abstract 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="processingBehavior">The processing behavior.</param>
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
protected FileProviderImageProvider(IFileProvider fileProvider, ProcessingBehavior processingBehavior, FormatUtilities formatUtilities)
{
Guard.NotNull(fileProvider, nameof(fileProvider));
Guard.NotNull(formatUtilities, nameof(formatUtilities));
this.fileProvider = fileProvider;
this.formatUtilities = formatUtilities;
this.ProcessingBehavior = processingBehavior;
}
/// <inheritdoc/>
public ProcessingBehavior ProcessingBehavior { get; }
/// <inheritdoc/>
public virtual Func<HttpContext, bool> Match { get; set; } = _ => true;
/// <inheritdoc/>
public virtual 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));
}
}
}