In my ImageSharp.Web test project, I am trying to redirect image requests to a different directory in an Azure container. For example, I want requests to https://example.com/directory1/12345/image.jpg to instead look in /files/directory1/12345/image.jpg.
I came across a related StackOverflow question (Imagesharp.Web Custom URL Pattern) where the suggested answer was to use IRequestParser. Unfortunately, I haven't been able to find any examples of what this would look like.
Can anyone provide an example of how to use IRequestParser in ImageSharp to redirect image requests to a different directory?
Thanks in advance!
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.DependencyInjection;
using SixLabors.ImageSharp.Web.Processors;
using SixLabors.ImageSharp.Web.Providers.Azure;
using SixLabors.ImageSharp.Web.Providers;
namespace ImageSharpTest
{
public class Program
{
public static void Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
IServiceCollection services = builder.Services;
services.AddRazorPages();
services.AddImageSharp()
.ClearProviders()
.SetRequestParser<QueryCollectionRequestParser>()
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheRootPath = null;
options.CacheFolder = "is-cache";
options.CacheFolderDepth = 8;
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
.SetCacheHash<SHA256CacheHash>()
.Configure<PhysicalFileSystemProviderOptions>(options => options.ProviderRootPath = null)
.Configure<AzureBlobStorageImageProviderOptions>(options =>
{
options.BlobContainers.Add(new AzureBlobContainerClientOptions
{
ConnectionString = "DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=ACCOUNT_KEY;EndpointSuffix=core.windows.net",
ContainerName = "files"
});
})
.AddProvider<AzureBlobStorageImageProvider>()
.AddProvider<PhysicalFileSystemProvider>()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>()
.AddProcessor<AutoOrientWebProcessor>();
WebApplication app = builder.Build();
app.UseDeveloperExceptionPage();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseImageSharp();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
}
}
}