From 6d8a23504e493b48dca62446e66165e90bd4103d Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 17 Mar 2020 11:39:55 +0100 Subject: [PATCH 1/2] Remove resize parameters if larger than 5000 --- .../config/imageprocessor/processing.config | 99 ++++++++++++------- ...bracoWebsiteServiceCollectionExtensions.cs | 73 +++++++++++++- 2 files changed, 138 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Web.UI/config/imageprocessor/processing.config b/src/Umbraco.Web.UI/config/imageprocessor/processing.config index dddcddb0bd..da9b6fa180 100644 --- a/src/Umbraco.Web.UI/config/imageprocessor/processing.config +++ b/src/Umbraco.Web.UI/config/imageprocessor/processing.config @@ -2,37 +2,70 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs b/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs index 5fb2825269..35772e6ab0 100644 --- a/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs @@ -1,5 +1,15 @@ +using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Web; +using SixLabors.ImageSharp.Web.Caching; +using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.DependencyInjection; +using SixLabors.ImageSharp.Web.Middleware; +using SixLabors.ImageSharp.Web.Processors; +using SixLabors.ImageSharp.Web.Providers; +using SixLabors.Memory; namespace Umbraco.Web.Website.AspNetCore { @@ -7,11 +17,72 @@ namespace Umbraco.Web.Website.AspNetCore { public static IServiceCollection AddUmbracoWebsite(this IServiceCollection services) { - services.AddImageSharp(); + services.AddImageSharpCore( + options => + { + options.Configuration = SixLabors.ImageSharp.Configuration.Default; + options.MaxBrowserCacheDays = 7; + options.MaxCacheDays = 365; + options.CachedNameLength = 8; + options.OnParseCommands = context => + { + RemoveIntParamenterIfValueGreatherThen(context.Commands, ResizeWebProcessor.Width, 5000); + RemoveIntParamenterIfValueGreatherThen(context.Commands, ResizeWebProcessor.Height, 5000); + }; + options.OnBeforeSave = _ => { }; + options.OnProcessed = _ => { }; + options.OnPrepareResponse = _ => { }; + }) + .SetRequestParser() + .SetMemoryAllocator(provider => ArrayPoolMemoryAllocator.CreateWithMinimalPooling()) + .Configure(options => + { + options.CacheFolder = "is-cache"; + + }) + .SetCache() + .SetCacheHash() + .AddProvider() + .AddProcessor() + .AddProcessor() + .AddProcessor(); return services; } + private static void RemoveIntParamenterIfValueGreatherThen(IDictionary commands, string parameter, int maxValue) + { + if (commands.TryGetValue(parameter, out var command)) + { + if (int.TryParse(command, out var i)) + { + if (i > maxValue) + { + commands.Remove(parameter); + } + } + } + } + } + + public class UmbracoResizeWebProcessor : ResizeWebProcessor + { + + public new FormattedImage Process(FormattedImage image, ILogger logger, IDictionary commands) + { + if(commands.TryGetValue("width", out var command)) + { + if (int.TryParse(command, out var width)) + { + if (width > 5000) + { + commands.Remove("width"); + } + } + } + + return Process(image, logger, commands); + } } } From 64ba5b6879eb73cf41c4e0a789a4ffcba8a19248 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 17 Mar 2020 15:36:48 +0100 Subject: [PATCH 2/2] Clean up --- ...bracoWebsiteServiceCollectionExtensions.cs | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs b/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs index 35772e6ab0..aeae455294 100644 --- a/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Website/AspNetCore/UmbracoWebsiteServiceCollectionExtensions.cs @@ -16,6 +16,13 @@ namespace Umbraco.Web.Website.AspNetCore public static class UmbracoBackOfficeServiceCollectionExtensions { public static IServiceCollection AddUmbracoWebsite(this IServiceCollection services) + { + services.AddUmbracoImageSharp(); + + return services; + } + + public static IServiceCollection AddUmbracoImageSharp(this IServiceCollection services) { services.AddImageSharpCore( options => @@ -37,8 +44,7 @@ namespace Umbraco.Web.Website.AspNetCore .SetMemoryAllocator(provider => ArrayPoolMemoryAllocator.CreateWithMinimalPooling()) .Configure(options => { - options.CacheFolder = "is-cache"; - + options.CacheFolder = "../app_data/cache"; }) .SetCache() .SetCacheHash() @@ -47,7 +53,6 @@ namespace Umbraco.Web.Website.AspNetCore .AddProcessor() .AddProcessor(); - return services; } @@ -66,23 +71,4 @@ namespace Umbraco.Web.Website.AspNetCore } } - public class UmbracoResizeWebProcessor : ResizeWebProcessor - { - - public new FormattedImage Process(FormattedImage image, ILogger logger, IDictionary commands) - { - if(commands.TryGetValue("width", out var command)) - { - if (int.TryParse(command, out var width)) - { - if (width > 5000) - { - commands.Remove("width"); - } - } - } - - return Process(image, logger, commands); - } - } }