Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file path encoding edge cases in HTML export #1351

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions DiscordChatExporter.Core/Exporting/ExportContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -118,7 +119,7 @@ public async ValueTask<string> ResolveAssetUrlAsync(
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,
Expand All @@ -133,14 +134,7 @@ public async ValueTask<string> ResolveAssetUrlAsync(

// 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;
}
Expand Down
43 changes: 43 additions & 0 deletions DiscordChatExporter.Core/Utils/Url.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}