mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-10 00:43:03 +00:00
Fix file path encoding edge cases in HTML export (#1351)
This commit is contained in:
43
DiscordChatExporter.Core/Utils/Url.cs
Normal file
43
DiscordChatExporter.Core/Utils/Url.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user