using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.DependencyInjection; using System.IO; using zero.Media.ImageSharp; using zero.Media.ImageSharp.Processors; namespace zero.Media; internal class ZeroMediaModule : ZeroModule { public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { services.AddImageSharp() .SetRequestParser() .ClearProviders() .AddProvider() .AddProcessor() .AddProcessor() .AddProcessor() .AddProcessor(); services.AddOptions().Configure(opts => { opts.CacheRootPath = "~/"; opts.CacheFolder = "cache"; }).Bind(configuration.GetSection("Zero:Media:ImageSharp:Cache")); //configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Config/imaging.json"), true, true); //configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), $"Config/imaging.{builder.Environment.EnvironmentName}.json"), true); services.AddSingleton(svc => { IOptions options = svc.GetRequiredService>(); IWebHostEnvironment env = svc.GetRequiredService(); return new(Path.Combine(env.WebRootPath, options.Value.FolderPath), options.Value.PublicPathPrefix + options.Value.FolderPath.EnsureStartsWith('/')); }); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); services.AddSingleton(); services.AddOptions().Configure(opts => { opts.FolderPath = "media"; opts.AllowedOtherFileExtensions = new() { ".pdf", ".docx", ".doc", ".svg", ".xml" }; opts.AllowedImageFileExtensions = new() { ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".gif", ".avif" }; // opts.Thumbnails = new() // { // { "thumb", new ResizeOptions() { Size = new(100, 100), Mode = ResizeMode.Max } }, // { "preview", new ResizeOptions() { Size = new(210, 210), Mode = ResizeMode.Min } } // }; }).Bind(configuration.GetSection("Zero:Media")); // services.Configure(opts => // { // RavenOptions raven = opts.For(); // raven.Indexes.Add(); // raven.Indexes.Add(); // }); } public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) { app.UseImageSharp(); } }