From 129df42de4300357322ffe2dd6230ef82b6e9f71 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:09:09 +0200 Subject: [PATCH] Keep `file:///` for absolute paths in HTML exports Closes #1155 --- .../Exporting/ExportContext.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/DiscordChatExporter.Core/Exporting/ExportContext.cs b/DiscordChatExporter.Core/Exporting/ExportContext.cs index 596d098..626986b 100644 --- a/DiscordChatExporter.Core/Exporting/ExportContext.cs +++ b/DiscordChatExporter.Core/Exporting/ExportContext.cs @@ -124,9 +124,9 @@ internal class ExportContext var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken); var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath); - // Prefer relative paths so that the output files can be copied around without breaking references. - // If the asset directory is outside of the export directory, use an absolute path instead. - var optimalFilePath = + // 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. + var shouldUseAbsoluteFilePath = relativeFilePath.StartsWith( ".." + Path.DirectorySeparatorChar, StringComparison.Ordinal @@ -134,15 +134,19 @@ internal class ExportContext || relativeFilePath.StartsWith( ".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal - ) - ? filePath - : relativeFilePath; + ); + + var optimalFilePath = shouldUseAbsoluteFilePath ? filePath : relativeFilePath; // For HTML, the path needs to be properly formatted if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight) { - // Create a 'file:///' URI and then strip the 'file:///' prefix to allow for relative paths - return new Uri(new Uri("file:///"), optimalFilePath).ToString()[8..]; + // 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 optimalFilePath;