How to make ImageSharp.Web to resize images from a local directory other then wwwroot

373 Views Asked by At

I'm developing a Blazor server application and I'm trying to implement imagesharp.web (V.3.01)

I have to use a folder called "Uploads" (not inside wwwroot). I'm trying to use PhysicalFileSystemProviderOptions, setting the ProviderRootPath to the desired folder, but it is not working.

Can anyone help on this problem?

var pathToUploads = Path.Combine(Directory.GetCurrentDirectory(), "Uploads");
var pathToUploadsCache = Path.Combine(Directory.GetCurrentDirectory(), "Uploads", "is-cache");

builder.Services.AddImageSharp(options =>
    {
        options.BrowserMaxAge = TimeSpan.FromDays(7);
        options.CacheMaxAge = TimeSpan.FromDays(365);

    }).ClearProviders()
    .AddProvider<WebRootImageProvider>()
    .Configure<PhysicalFileSystemCacheOptions>(options =>
    {
        options.CacheRootPath = null;
        options.CacheFolder = pathToUploadsCache;
        options.CacheFolderDepth = 8;
    })
            .SetCache<PhysicalFileSystemCache>()
    .AddProvider<PhysicalFileSystemProvider>()
    .Configure<PhysicalFileSystemProviderOptions>(options=>
    {
        options.ProviderRootPath = pathToUploads;
        }); 

var app = builder.Build();



// Set Uploads folder
SD.ContentRootPath = app.Environment.ContentRootPath;
SD.UploadsFullServerPath = Path.Combine(app.Environment.ContentRootPath, configuration["UploadsFolder:Name"]);
SD.UploadsRequestPath = configuration["UploadsFolder:WebPath"];
SD.UploadsNameOnly = configuration["UploadsFolder:Name"];
SD.UploadsRootFolderName = configuration["UploadsFolder:RootFolderName"];
SD.UploadsRootFolderWebPath = configuration["UploadsFolder:RootFolderWebPath"];
app.UseImageSharp();

app.UseStaticFiles();

if (!Directory.Exists(SD.UploadsFullServerPath)
    || !Directory.Exists(Path.Combine(SD.UploadsFullServerPath, SD.UploadsRootFolderName)))
{
    Directory.CreateDirectory(Path.Combine(SD.UploadsFullServerPath, SD.UploadsRootFolderName));
}
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, SD.UploadsNameOnly)),
    RequestPath = SD.UploadsRequestPath
});
2

There are 2 best solutions below

0
jmvcollaborator On

Right after AddImageSharp define this method:

services.AddImageSharpCore(
            options =>
            {
                options.MaxBrowserCacheDays = 7;
                options.MaxCacheDays = 365;
                options.CachedNameLength = 8;
                options.OnParseCommands = _ => { };
                options.OnBeforeSave = _ => { };
                options.OnProcessed = _ => { };
                options.OnPrepareResponse = _ => { };
            })

Add UseWebRoot in your Program.cs class:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseWebRoot("myroot") // name it whatever you want
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}
2
Pedro Cavalcante On

this guy here: create-your-own-image.html solve this, in the ImageSharp documentation, its possible to see two types of Image Providers, that's not what we need, but in the top of the page has this note: It is possible to configure your own image provider by implementing and registering your own version of the IImageProvider interface.

read the documentation IImageProvider.