0

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
});
Stelios
  • 45
  • 8

1 Answers1

0

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();
}
jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17