From f499631db87e2fff19a637ddc62d9474cd0c717c Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Wed, 1 Nov 2023 23:10:05 +0100 Subject: [PATCH 01/12] Adding appendVersion option to Media.Shortcodes --- .../Shortcodes/ImageShortcodeProvider.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index b6a8c20a154..8d7effd5641 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Options; using OrchardCore.Infrastructure.Html; @@ -24,17 +25,23 @@ public class ImageShortcodeProvider : IShortcodeProvider private readonly IHtmlSanitizerService _htmlSanitizerService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly ResourceManagementOptions _options; + private readonly IFileVersionProvider _fileVersionProvider; + private readonly IOrchardHelper _orchardHelper; public ImageShortcodeProvider( IMediaFileStore mediaFileStore, IHtmlSanitizerService htmlSanitizerService, IHttpContextAccessor httpContextAccessor, - IOptions options) + IOptions options, + IFileVersionProvider fileVersionProvider, + IOrchardHelper orchardHelper) { _mediaFileStore = mediaFileStore; _htmlSanitizerService = htmlSanitizerService; _httpContextAccessor = httpContextAccessor; _options = options.Value; + _fileVersionProvider = fileVersionProvider; + _orchardHelper = orchardHelper; } public ValueTask EvaluateAsync(string identifier, Arguments arguments, string content, Context context) @@ -73,6 +80,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s } var className = string.Empty; var altText = string.Empty; + if (arguments.Any()) { var queryStringParams = new Dictionary(); @@ -82,6 +90,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s var mode = arguments.Named("mode"); var quality = arguments.Named("quality"); var format = arguments.Named("format"); + var appendVersion = arguments.Named("appendVersion"); className = arguments.Named("class"); altText = arguments.Named("alt"); @@ -110,6 +119,11 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s queryStringParams.Add("format", format); } + if (appendVersion == "true") + { + content = _fileVersionProvider.AddFileVersionToPath(_orchardHelper.HttpContext.Request.PathBase, content); + } + if (className != null) { className = "class=\"" + className + "\" "; From 9db93189d92a7ae645405749de66a6bf2eba9636 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Wed, 1 Nov 2023 23:19:46 +0100 Subject: [PATCH 02/12] Updating README.md --- src/docs/reference/modules/Shortcodes/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/docs/reference/modules/Shortcodes/README.md b/src/docs/reference/modules/Shortcodes/README.md index 2d642a88a12..2c26c5eb9c7 100644 --- a/src/docs/reference/modules/Shortcodes/README.md +++ b/src/docs/reference/modules/Shortcodes/README.md @@ -141,6 +141,7 @@ The following parameters can be used: - **alt:** Adds alternative text to your image for the benefit of readers who can't see the image and also good for SEO. - **class:** Adds an html class attribute to the image tag for styling. +- **appendVersion:** Adds cache busting if set to `true`. - **format:** Change the file format from the original file. Can be jpeg, png, gif or bmp. - **quality:** Sets the encoding quality to use for jpeg images. The higher the quality, the larger the file size will be. The value can be from 0 to 100 and defaults to 75. - **width, height:** The width and height can be set to resize the image. The possible values are limited to prevent malicious clients from creating too many variations of the same image. The values can be 16, 32, 50, 100, 160, 240, 480, 600, 1024, 2048. From c932a8b58e08a5d343aa54265852c707c71393d8 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Wed, 1 Nov 2023 23:29:47 +0100 Subject: [PATCH 03/12] Using _httpContextAccessor instead of _orchardHelper --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index 8d7effd5641..48ebb2c9ce7 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -26,7 +26,6 @@ public class ImageShortcodeProvider : IShortcodeProvider private readonly IHttpContextAccessor _httpContextAccessor; private readonly ResourceManagementOptions _options; private readonly IFileVersionProvider _fileVersionProvider; - private readonly IOrchardHelper _orchardHelper; public ImageShortcodeProvider( IMediaFileStore mediaFileStore, @@ -41,7 +40,6 @@ public ImageShortcodeProvider( _httpContextAccessor = httpContextAccessor; _options = options.Value; _fileVersionProvider = fileVersionProvider; - _orchardHelper = orchardHelper; } public ValueTask EvaluateAsync(string identifier, Arguments arguments, string content, Context context) @@ -121,7 +119,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s if (appendVersion == "true") { - content = _fileVersionProvider.AddFileVersionToPath(_orchardHelper.HttpContext.Request.PathBase, content); + content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content); } if (className != null) From 829593490af06113bd84a55acb14371e53d81f56 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Wed, 1 Nov 2023 23:30:56 +0100 Subject: [PATCH 04/12] Removing leftover orchardHelper --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index 48ebb2c9ce7..c6b94e6ce24 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -32,8 +32,7 @@ public ImageShortcodeProvider( IHtmlSanitizerService htmlSanitizerService, IHttpContextAccessor httpContextAccessor, IOptions options, - IFileVersionProvider fileVersionProvider, - IOrchardHelper orchardHelper) + IFileVersionProvider fileVersionProvider) { _mediaFileStore = mediaFileStore; _htmlSanitizerService = htmlSanitizerService; From ad6ce252b0eb054255335fa9bbeff68137916fe8 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Wed, 1 Nov 2023 23:48:28 +0100 Subject: [PATCH 05/12] Fixing tests --- .../Modules/OrchardCore.Media/ImageShortcodeTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/OrchardCore.Tests/Modules/OrchardCore.Media/ImageShortcodeTests.cs b/test/OrchardCore.Tests/Modules/OrchardCore.Media/ImageShortcodeTests.cs index 4f49f18a560..28ec458cd7a 100644 --- a/test/OrchardCore.Tests/Modules/OrchardCore.Media/ImageShortcodeTests.cs +++ b/test/OrchardCore.Tests/Modules/OrchardCore.Media/ImageShortcodeTests.cs @@ -50,6 +50,8 @@ public async Task ShouldProcess(string cdnBaseUrl, string text, string expected) Enumerable.Empty(), Mock.Of>()); + var fileVersionProvider = Mock.Of(); + var sanitizer = new HtmlSanitizerService(Options.Create(sanitizerOptions)); var defaultHttpContext = new DefaultHttpContext(); @@ -58,7 +60,7 @@ public async Task ShouldProcess(string cdnBaseUrl, string text, string expected) var options = Options.Create(new ResourceManagementOptions { CdnBaseUrl = cdnBaseUrl }); - var imageProvider = new ImageShortcodeProvider(fileStore, sanitizer, httpContextAccessor, options); + var imageProvider = new ImageShortcodeProvider(fileStore, sanitizer, httpContextAccessor, options, fileVersionProvider); var processor = new ShortcodeService(new IShortcodeProvider[] { imageProvider }, Enumerable.Empty()); From 372e9f5511e3dd7f1d7d260ad80900b00bfb3990 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme <80963259+DemeSzabolcs@users.noreply.github.com> Date: Thu, 2 Nov 2023 01:59:02 +0100 Subject: [PATCH 06/12] Update src/docs/reference/modules/Shortcodes/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zoltán Lehóczky --- src/docs/reference/modules/Shortcodes/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docs/reference/modules/Shortcodes/README.md b/src/docs/reference/modules/Shortcodes/README.md index 2c26c5eb9c7..d1c3cd9510e 100644 --- a/src/docs/reference/modules/Shortcodes/README.md +++ b/src/docs/reference/modules/Shortcodes/README.md @@ -141,7 +141,7 @@ The following parameters can be used: - **alt:** Adds alternative text to your image for the benefit of readers who can't see the image and also good for SEO. - **class:** Adds an html class attribute to the image tag for styling. -- **appendVersion:** Adds cache busting if set to `true`. +- **appendVersion:** Adds a cache busting query string parameter if set to `true`, i.e. `appendVersion="true"`. - **format:** Change the file format from the original file. Can be jpeg, png, gif or bmp. - **quality:** Sets the encoding quality to use for jpeg images. The higher the quality, the larger the file size will be. The value can be from 0 to 100 and defaults to 75. - **width, height:** The width and height can be set to resize the image. The possible values are limited to prevent malicious clients from creating too many variations of the same image. The values can be 16, 32, 50, 100, 160, 240, 480, 600, 1024, 2048. From c23f62f9e3165cd05f8a79089e56289cac5689df Mon Sep 17 00:00:00 2001 From: Szabolcs Deme <80963259+DemeSzabolcs@users.noreply.github.com> Date: Thu, 2 Nov 2023 01:59:06 +0100 Subject: [PATCH 07/12] Update src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zoltán Lehóczky --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index c6b94e6ce24..abcb2845d97 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -116,7 +116,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s queryStringParams.Add("format", format); } - if (appendVersion == "true") + if (appendVersion.Equals("true", StringComparison.InvariantCultureIgnoreCase)) { content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content); } From 936791ddbc71d5232ec034f7fa14d4d35dfaca1f Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 2 Nov 2023 02:01:49 +0100 Subject: [PATCH 08/12] Renaming appendVersion to append_version --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 2 +- src/docs/reference/modules/Shortcodes/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index abcb2845d97..35b65e09f3e 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -87,7 +87,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s var mode = arguments.Named("mode"); var quality = arguments.Named("quality"); var format = arguments.Named("format"); - var appendVersion = arguments.Named("appendVersion"); + var appendVersion = arguments.Named("append_version"); className = arguments.Named("class"); altText = arguments.Named("alt"); diff --git a/src/docs/reference/modules/Shortcodes/README.md b/src/docs/reference/modules/Shortcodes/README.md index d1c3cd9510e..fe84ecf0521 100644 --- a/src/docs/reference/modules/Shortcodes/README.md +++ b/src/docs/reference/modules/Shortcodes/README.md @@ -141,7 +141,7 @@ The following parameters can be used: - **alt:** Adds alternative text to your image for the benefit of readers who can't see the image and also good for SEO. - **class:** Adds an html class attribute to the image tag for styling. -- **appendVersion:** Adds a cache busting query string parameter if set to `true`, i.e. `appendVersion="true"`. +- **append_version:** Adds a cache busting query string parameter if set to `true`, i.e. `append_version="true"`. - **format:** Change the file format from the original file. Can be jpeg, png, gif or bmp. - **quality:** Sets the encoding quality to use for jpeg images. The higher the quality, the larger the file size will be. The value can be from 0 to 100 and defaults to 75. - **width, height:** The width and height can be set to resize the image. The possible values are limited to prevent malicious clients from creating too many variations of the same image. The values can be 16, 32, 50, 100, 160, 240, 480, 600, 1024, 2048. From e7c418feb83e37e6dd3fc85661800fa2706b4570 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 2 Nov 2023 02:17:35 +0100 Subject: [PATCH 09/12] Adding nullcheck --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index 35b65e09f3e..b4d9f6bd775 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -116,7 +116,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s queryStringParams.Add("format", format); } - if (appendVersion.Equals("true", StringComparison.InvariantCultureIgnoreCase)) + if (appendVersion?.Equals("true", StringComparison.InvariantCultureIgnoreCase) == true) { content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content); } From e4ec4152682aa6ffc609b570b5eefa890348bd10 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 2 Nov 2023 15:35:34 +0100 Subject: [PATCH 10/12] Using OrdinalIgnoreCase instead of InvariantCultureIgnoreCase --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index b4d9f6bd775..1923acbd6a1 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -116,7 +116,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s queryStringParams.Add("format", format); } - if (appendVersion?.Equals("true", StringComparison.InvariantCultureIgnoreCase) == true) + if (appendVersion?.Equals("true", StringComparison.OrdinalIgnoreCase) == true) { content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content); } From 73564df849a88c6d6b494567e89aba4358e18181 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme <80963259+DemeSzabolcs@users.noreply.github.com> Date: Thu, 2 Nov 2023 17:20:10 +0100 Subject: [PATCH 11/12] Update src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs Co-authored-by: Hisham Bin Ateya --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index 1923acbd6a1..67bcbed3448 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -77,7 +77,6 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s } var className = string.Empty; var altText = string.Empty; - if (arguments.Any()) { var queryStringParams = new Dictionary(); From a95c759a7a1de7dac5210178a67a3301b9a3cb40 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 2 Nov 2023 18:03:58 +0100 Subject: [PATCH 12/12] Changing nullcheck --- .../OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs index 67bcbed3448..6d58f73b7c1 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Shortcodes/ImageShortcodeProvider.cs @@ -115,7 +115,7 @@ public ValueTask EvaluateAsync(string identifier, Arguments arguments, s queryStringParams.Add("format", format); } - if (appendVersion?.Equals("true", StringComparison.OrdinalIgnoreCase) == true) + if (appendVersion?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false) { content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content); }