Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a shortcode for cache busting (Lombiq Technologies: OCORE-127) #14621

Merged
merged 12 commits into from
Nov 2, 2023
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 @@ -73,6 +77,7 @@ public ValueTask<string> EvaluateAsync(string identifier, Arguments arguments, s
}
var className = string.Empty;
var altText = string.Empty;

DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
if (arguments.Any())
{
var queryStringParams = new Dictionary<string, string>();
Expand All @@ -82,6 +87,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("appendVersion");
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
className = arguments.Named("class");
altText = arguments.Named("alt");

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

if (appendVersion == "true")
DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
{
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.
- **appendVersion:** Adds cache busting if set to `true`.
DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
- **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
Loading