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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 9 deletions

View file

@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting;
@ -118,7 +119,7 @@ internal class ExportContext(DiscordClient discord, ExportRequest request)
var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath);
// Prefer the relative path so that the export package can be copied around without breaking references.
// However, if the assets directory lies outside of the export directory, use the absolute path instead.
// However, if the assets directory lies outside the export directory, use the absolute path instead.
var shouldUseAbsoluteFilePath =
relativeFilePath.StartsWith(
".." + Path.DirectorySeparatorChar,
@ -133,14 +134,7 @@ internal class ExportContext(DiscordClient discord, ExportRequest request)
// For HTML, the path needs to be properly formatted
if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight)
{
// Format the path into a valid file URI
var href = new Uri(new Uri("file:///"), optimalFilePath).ToString();
// File schema does not support relative paths, so strip it if that's the case
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1155
return shouldUseAbsoluteFilePath ? href : href[8..];
}
return Url.EncodeFilePath(optimalFilePath);
return optimalFilePath;
}

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();
}
}