Skip to content

Commit

Permalink
Add ASCII file name conversion (#17580)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac authored Nov 26, 2024
1 parent 7c617f2 commit 4590739
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -73,6 +74,22 @@ public class RequestHandlerSettings
/// </summary>
public bool ShouldTryConvertUrlsToAscii => ConvertUrlsToAscii.InvariantEquals("try");

/// <summary>
/// Gets or sets a value indicating whether to convert file names to ASCII (valid values: "true", "try" or "false").
/// </summary>
[DefaultValue(StaticConvertFileNamesToAscii)]
public string ConvertFileNamesToAscii { get; set; } = StaticConvertFileNamesToAscii;

/// <summary>
/// Gets a value indicating whether URLs should be converted to ASCII.
/// </summary>
public bool ShouldConvertFileNamesToAscii => ConvertFileNamesToAscii.InvariantEquals("true");

/// <summary>
/// Gets a value indicating whether URLs should be tried to be converted to ASCII.
/// </summary>
public bool ShouldTryConvertFileNamesToAscii => ConvertFileNamesToAscii.InvariantEquals("try");

/// <summary>
/// Disable all default character replacements
/// </summary>
Expand Down
15 changes: 12 additions & 3 deletions src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 4590739

Please sign in to comment.