0

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();
        }
    }
}
Eyeball
  • 1,322
  • 2
  • 16
  • 26

1 Answers1

0

Look here for the default implementation of an IRequestParser: QueryCollectionRequestParser.cs

public class QueryCollectionRequestParser : IRequestParser
{
    public CommandCollection ParseRequestCommands(HttpContext context)
    {
        IQueryCollection query = context.Request.Query;
        if (query is null || query.Count == 0)
        {
            return new();
        }

        CommandCollection transformed = new();
        foreach (KeyValuePair<string, StringValues> pair in query)
        {
            string? value = pair.Value[^1];
            if (value is not null)
            {
                transformed[pair.Key] = value;
            }
        }

        return transformed;
    }
}

You want to make a copy of this implementation in your project to be able to modify it and set it using the .SetRequestParser command.

In your case add line to the start of the ParseRequestCommands method:

context.Request.Path = context.Request.Path.Value?.Replace("directory1", "files/directory1");
sboulema
  • 837
  • 1
  • 10
  • 22