Fix file path encoding edge cases in HTML export (#1351)

This commit is contained in:
Oleksii Holub
2025-03-10 19:11:17 +02:00
committed by GitHub
parent b39d015133
commit db50a2bb96
2 changed files with 46 additions and 9 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Text;
namespace DiscordChatExporter.Core.Utils;
public static class Url
{
public static string EncodeFilePath(string filePath)
{
var buffer = new StringBuilder();
var position = 0;
while (true)
{
if (position >= filePath.Length)
break;
var separatorIndex = filePath.IndexOfAny([':', '/', '\\'], position);
if (separatorIndex < 0)
{
buffer.Append(Uri.EscapeDataString(filePath[position..]));
break;
}
// Append the segment
buffer.Append(Uri.EscapeDataString(filePath[position..separatorIndex]));
// Append the separator
buffer.Append(
filePath[separatorIndex] switch
{
// Normalize slashes
'\\' => '/',
var c => c,
}
);
position = separatorIndex + 1;
}
return buffer.ToString();
}
}