diff --git a/zero/Media/ImageSharp/PresetRequestParser.cs b/zero/Media/ImageSharp/PresetRequestParser.cs index 50e2d2fd..606c810d 100644 --- a/zero/Media/ImageSharp/PresetRequestParser.cs +++ b/zero/Media/ImageSharp/PresetRequestParser.cs @@ -54,14 +54,26 @@ public sealed class PresetRequestParser : IRequestParser return []; } - FallbackFormat fallbackFormat = FindFallbackFormatInContext(context); + // get default processing commands (which are added to each request) from options + Dictionary defaults = _options.ImageSharp.DefaultCommands.Select(x => x.Split("=", 2)).ToDictionary(x => x[0], x => x[1]); + // fall back to webp, as avif is not supported yet by ImageSharp - string format = fallbackFormat != FallbackFormat.None ? "webp" : null; - string quality = _options.ImageSharp.DefaultQuality.ToString(); + FallbackFormat fallbackFormat = FindFallbackFormatInContext(context); + string format = defaults.GetValueOrDefault("format", fallbackFormat != FallbackFormat.None ? "webp" : null); + string quality = defaults.GetValueOrDefault("quality"); CommandCollection filters = []; + foreach (KeyValuePair kv in defaults) + { + string key = kv.Key; + if (key != "format" && key != "quality") + { + filters[key] = kv.Value; + } + } + foreach (string[] kv in transformed.Select(x => x.Split("=", 2))) { string key = kv[0]; @@ -79,25 +91,28 @@ public sealed class PresetRequestParser : IRequestParser continue; } - filters.Add(key, value); + filters[key] = value; } // add positioning filters if (focalPoint != null) { - filters.Add("rxy", focalPoint); + filters["rxy"] = focalPoint; } else { - filters.Add("ranchor", "center"); + filters["ranchor"] = "center"; } // add format/quality filters if (format.HasValue()) { - filters.Add("format", format); + filters["format"] = format; + } + if (quality.HasValue()) + { + filters["quality"] = quality; } - filters.Add("quality", quality); return filters; } diff --git a/zero/Media/MediaOptions.cs b/zero/Media/MediaOptions.cs index b70e522f..c74e48a7 100644 --- a/zero/Media/MediaOptions.cs +++ b/zero/Media/MediaOptions.cs @@ -25,9 +25,9 @@ public class ImageSharpOptions public PhysicalFileSystemCacheOptions Cache { get; set; } = new(); - public Dictionary Presets { get; set; } = new(); + public Dictionary Presets { get; set; } = []; - public int DefaultQuality { get; set; } = 75; + public string[] DefaultCommands { get; set; } = []; } diff --git a/zero/Media/ZeroMediaModule.cs b/zero/Media/ZeroMediaModule.cs index 5aabadf2..48869e7a 100644 --- a/zero/Media/ZeroMediaModule.cs +++ b/zero/Media/ZeroMediaModule.cs @@ -23,6 +23,7 @@ internal class ZeroMediaModule : ZeroModule .ClearProviders() .AddProvider() .AddProcessor() + .AddProcessor() .Configure(configuration.GetSection("Zero:Media:ImageSharp:Cache")); //configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Config/imaging.json"), true, true); @@ -32,15 +33,17 @@ internal class ZeroMediaModule : ZeroModule { IOptions options = svc.GetRequiredService>(); IWebHostEnvironment env = svc.GetRequiredService(); + Console.WriteLine("media path: " + options.Value.FolderPath); 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().Bind(configuration.GetSection("Zero:Media")).Configure(opts => + services.AddOptions().Configure(opts => { opts.FolderPath = "media"; opts.AllowedOtherFileExtensions = new() { ".pdf", ".docx", ".doc", ".svg", ".xml" }; @@ -50,7 +53,7 @@ internal class ZeroMediaModule : ZeroModule // { "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 => // {