From 4590739fa5c751a7acd7cdd8e874a38d1e1cc695 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Tue, 26 Nov 2024 11:26:44 +0100 Subject: [PATCH] Add ASCII file name conversion (#17580) --- .../Models/RequestHandlerSettings.cs | 17 +++++++++++++++++ .../Strings/DefaultShortStringHelperConfig.cs | 15 ++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs b/src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs index 672577b1b723..a1d8f95a373a 100644 --- a/src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs +++ b/src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs @@ -15,6 +15,7 @@ public class RequestHandlerSettings { internal const bool StaticAddTrailingSlash = true; internal const string StaticConvertUrlsToAscii = "try"; + internal const string StaticConvertFileNamesToAscii = "false"; internal const bool StaticEnableDefaultCharReplacements = true; internal static readonly CharItem[] DefaultCharCollection = @@ -73,6 +74,22 @@ public class RequestHandlerSettings /// public bool ShouldTryConvertUrlsToAscii => ConvertUrlsToAscii.InvariantEquals("try"); + /// + /// Gets or sets a value indicating whether to convert file names to ASCII (valid values: "true", "try" or "false"). + /// + [DefaultValue(StaticConvertFileNamesToAscii)] + public string ConvertFileNamesToAscii { get; set; } = StaticConvertFileNamesToAscii; + + /// + /// Gets a value indicating whether URLs should be converted to ASCII. + /// + public bool ShouldConvertFileNamesToAscii => ConvertFileNamesToAscii.InvariantEquals("true"); + + /// + /// Gets a value indicating whether URLs should be tried to be converted to ASCII. + /// + public bool ShouldTryConvertFileNamesToAscii => ConvertFileNamesToAscii.InvariantEquals("try"); + /// /// Disable all default character replacements /// diff --git a/src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs b/src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs index ec7ed9d0023c..7a4a96835171 100644 --- a/src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs +++ b/src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs @@ -74,12 +74,21 @@ public DefaultShortStringHelperConfig WithDefault(RequestHandlerSettings request { urlSegmentConvertTo = CleanStringType.Ascii; } - - if (requestHandlerSettings.ShouldTryConvertUrlsToAscii) + else if (requestHandlerSettings.ShouldTryConvertUrlsToAscii) { urlSegmentConvertTo = CleanStringType.TryAscii; } + CleanStringType fileNameSegmentConvertTo = CleanStringType.Utf8; + if (requestHandlerSettings.ShouldConvertFileNamesToAscii) + { + fileNameSegmentConvertTo = CleanStringType.Ascii; + } + else if (requestHandlerSettings.ShouldTryConvertFileNamesToAscii) + { + fileNameSegmentConvertTo = CleanStringType.TryAscii; + } + return WithConfig(CleanStringType.UrlSegment, new Config { PreFilter = ApplyUrlReplaceCharacters, @@ -92,7 +101,7 @@ public DefaultShortStringHelperConfig WithDefault(RequestHandlerSettings request { PreFilter = ApplyUrlReplaceCharacters, IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore - StringType = CleanStringType.Utf8 | CleanStringType.LowerCase, + StringType = fileNameSegmentConvertTo | CleanStringType.LowerCase, BreakTermsOnUpper = false, Separator = '-', }).WithConfig(CleanStringType.Alias, new Config