Skip to content

Commit

Permalink
Add a shortcode for cache busting (#14621)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltán Lehóczky <[email protected]>
Co-authored-by: Hisham Bin Ateya <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent a690515 commit 23e1a4b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,17 +25,20 @@ public class ImageShortcodeProvider : IShortcodeProvider
private readonly IHtmlSanitizerService _htmlSanitizerService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ResourceManagementOptions _options;
private readonly IFileVersionProvider _fileVersionProvider;

public ImageShortcodeProvider(
IMediaFileStore mediaFileStore,
IHtmlSanitizerService htmlSanitizerService,
IHttpContextAccessor httpContextAccessor,
IOptions<ResourceManagementOptions> options)
IOptions<ResourceManagementOptions> options,
IFileVersionProvider fileVersionProvider)
{
_mediaFileStore = mediaFileStore;
_htmlSanitizerService = htmlSanitizerService;
_httpContextAccessor = httpContextAccessor;
_options = options.Value;
_fileVersionProvider = fileVersionProvider;
}

public ValueTask<string> EvaluateAsync(string identifier, Arguments arguments, string content, Context context)
Expand Down Expand Up @@ -82,6 +86,7 @@ public ValueTask<string> 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("append_version");
className = arguments.Named("class");
altText = arguments.Named("alt");

Expand Down Expand Up @@ -110,6 +115,11 @@ public ValueTask<string> EvaluateAsync(string identifier, Arguments arguments, s
queryStringParams.Add("format", format);
}

if (appendVersion?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false)
{
content = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, content);
}

if (className != null)
{
className = "class=\"" + className + "\" ";
Expand Down
1 change: 1 addition & 0 deletions src/docs/reference/modules/Shortcodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- **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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public async Task ShouldProcess(string cdnBaseUrl, string text, string expected)
Enumerable.Empty<IMediaCreatingEventHandler>(),
Mock.Of<ILogger<DefaultMediaFileStore>>());

var fileVersionProvider = Mock.Of<IFileVersionProvider>();

var sanitizer = new HtmlSanitizerService(Options.Create(sanitizerOptions));

var defaultHttpContext = new DefaultHttpContext();
Expand All @@ -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<IShortcodeContextProvider>());

Expand Down

0 comments on commit 23e1a4b

Please sign in to comment.