diff --git a/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/AppendVersionFilter.cs b/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/AppendVersionFilter.cs new file mode 100644 index 00000000000..fd61937687a --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/AppendVersionFilter.cs @@ -0,0 +1,28 @@ +using Fluid; +using Fluid.Values; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using OrchardCore.Liquid; + +namespace OrchardCore.Resources.Liquid; + +public class AppendVersionFilter : ILiquidFilter +{ + private readonly IFileVersionProvider _fileVersionProvider; + private readonly IHttpContextAccessor _httpContextAccessor; + + public AppendVersionFilter(IFileVersionProvider fileVersionProvider, IHttpContextAccessor httpContextAccessor) + { + _fileVersionProvider = fileVersionProvider; + _httpContextAccessor = httpContextAccessor; + } + + public ValueTask ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) + { + var url = input.ToStringValue(); + + var imageUrl = _fileVersionProvider.AddFileVersionToPath(_httpContextAccessor.HttpContext.Request.PathBase, url); + + return ValueTask.FromResult(new StringValue(imageUrl ?? url)); + } +} diff --git a/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/ResourceUrlFilter.cs b/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/ResourceUrlFilter.cs new file mode 100644 index 00000000000..e6063f1a7f6 --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.Resources/Liquid/ResourceUrlFilter.cs @@ -0,0 +1,46 @@ +using Fluid; +using Fluid.Values; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Options; +using OrchardCore.Liquid; +using OrchardCore.ResourceManagement; + +namespace OrchardCore.Resources.Liquid; + +/// +/// Returns the Cdn Base Url of the specified resource path. +/// +public class ResourceUrlFilter : ILiquidFilter +{ + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly ResourceManagementOptions _options; + + public ResourceUrlFilter(IHttpContextAccessor httpContextAccessor, IOptions options) + { + _httpContextAccessor = httpContextAccessor; + _options = options.Value; + } + + public ValueTask ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) + { + var resourcePath = input.ToStringValue(); + + if (resourcePath.StartsWith("~/", StringComparison.Ordinal)) + { + resourcePath = _httpContextAccessor.HttpContext.Request.PathBase.Add(resourcePath[1..]).Value; + } + + // Don't prefix cdn if the path includes a protocol, i.e. is an external url, or is in debug mode. + if (!_options.DebugMode && !string.IsNullOrEmpty(_options.CdnBaseUrl) && + // Don't evaluate with Uri.TryCreate as it produces incorrect results on Linux. + !resourcePath.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && + !resourcePath.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && + !resourcePath.StartsWith("//", StringComparison.OrdinalIgnoreCase) && + !resourcePath.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) + { + resourcePath = _options.CdnBaseUrl + resourcePath; + } + + return ValueTask.FromResult(new StringValue(resourcePath)); + } +} diff --git a/src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs index 85dfe85272b..bdccb05bfe0 100644 --- a/src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Options; using OrchardCore.DisplayManagement.Liquid; using OrchardCore.Environment.Shell.Configuration; +using OrchardCore.Liquid; using OrchardCore.Modules; using OrchardCore.ResourceManagement; using OrchardCore.Resources.Liquid; @@ -39,3 +40,13 @@ public override void ConfigureServices(IServiceCollection serviceCollection) serviceCollection.AddScoped(); } } + +[RequireFeatures("OrchardCore.Liquid")] +public sealed class ResourcesLiquidStartup : StartupBase +{ + public override void ConfigureServices(IServiceCollection services) + { + services.AddLiquidFilter("append_version") + .AddLiquidFilter("resource_url"); + } +} diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/AppendVersionFilter.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/AppendVersionFilter.cs index 9c622f16e9f..1675e973e68 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/AppendVersionFilter.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/AppendVersionFilter.cs @@ -6,6 +6,7 @@ namespace OrchardCore.DisplayManagement.Liquid.Filters; +[Obsolete("This filter is obsolete. Use OrchardCore.Resources.Liquid.AppendVersionFilter instead.")] public class AppendVersionFilter : ILiquidFilter { private readonly IFileVersionProvider _fileVersionProvider; diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/ResourceUrlFilter.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/ResourceUrlFilter.cs index cea4fc565b4..9adc6e234d8 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/ResourceUrlFilter.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/ResourceUrlFilter.cs @@ -10,6 +10,7 @@ namespace OrchardCore.DisplayManagement.Liquid.Filters; /// /// Returns the Cdn Base Url of the specified resource path. /// +[Obsolete("This filter is obsolete. Use OrchardCore.Resources.Liquid.ResourceUrlFilter instead.")] public class ResourceUrlFilter : ILiquidFilter { private readonly IHttpContextAccessor _httpContextAccessor; diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs index 5f1b8f28b88..f5c32c2a830 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs @@ -41,11 +41,8 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder) services.AddSingleton, TemplateOptionsConfigurations>(); - #pragma warning disable CS0618 // Type or member is obsolete - services.AddLiquidFilter("append_version") - .AddLiquidFilter("resource_url") - .AddLiquidFilter("sanitize_html") + services.AddLiquidFilter("sanitize_html") // Deprecated, remove in a future version. .AddLiquidFilter("supported_cultures");