From 560a069c35e9525bd970e4c0ec549ca98e0b0591 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sat, 29 Oct 2022 17:39:53 +0300 Subject: [PATCH] Refactor, rename the concept of "download media" to "download assets", including related mentions --- .../Specs/SelfContainedSpecs.cs | 2 +- .../Commands/Base/ExportCommandBase.cs | 16 +++--- ...Downloader.cs => ExportAssetDownloader.cs} | 14 ++--- .../Exporting/ExportContext.cs | 10 ++-- .../Exporting/ExportRequest.cs | 6 +-- .../Exporting/Writers/CsvMessageWriter.cs | 2 +- .../Writers/Html/MessageGroupTemplate.cshtml | 52 ++++++++++--------- .../Writers/Html/PreambleTemplate.cshtml | 27 +++++----- .../Exporting/Writers/JsonMessageWriter.cs | 31 ++++++++--- .../MarkdownVisitors/HtmlMarkdownVisitor.cs | 50 +++++++++++------- .../PlainTextMarkdownVisitor.cs | 16 +++--- .../Writers/PlainTextMessageWriter.cs | 24 +++++++-- .../Services/SettingsService.cs | 4 +- .../Components/DashboardViewModel.cs | 4 +- .../Dialogs/ExportSetupViewModel.cs | 8 +-- .../ViewModels/Dialogs/SettingsViewModel.cs | 6 +-- .../Views/Dialogs/ExportSetupView.xaml | 8 +-- .../Views/Dialogs/SettingsView.xaml | 8 +-- 18 files changed, 168 insertions(+), 120 deletions(-) rename DiscordChatExporter.Core/Exporting/{MediaDownloader.cs => ExportAssetDownloader.cs} (92%) diff --git a/DiscordChatExporter.Cli.Tests/Specs/SelfContainedSpecs.cs b/DiscordChatExporter.Cli.Tests/Specs/SelfContainedSpecs.cs index 3b82502..fd2aaf2 100644 --- a/DiscordChatExporter.Cli.Tests/Specs/SelfContainedSpecs.cs +++ b/DiscordChatExporter.Cli.Tests/Specs/SelfContainedSpecs.cs @@ -36,7 +36,7 @@ public class SelfContainedSpecs : IClassFixture ChannelIds = new[] { ChannelIds.SelfContainedTestCases }, ExportFormat = ExportFormat.HtmlDark, OutputPath = filePath, - ShouldDownloadMedia = true + ShouldDownloadAssets = true }.ExecuteAsync(new FakeConsole()); // Assert diff --git a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs index b01892b..56b3931 100644 --- a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs +++ b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs @@ -76,15 +76,15 @@ public abstract class ExportCommandBase : TokenCommandBase [CommandOption( "media", - Description = "Download referenced media content." + Description = "Download assets referenced by the export (user avatars, attached files, embedded images, etc.)." )] - public bool ShouldDownloadMedia { get; init; } + public bool ShouldDownloadAssets { get; init; } [CommandOption( "reuse-media", - Description = "Reuse already existing media content to skip redundant downloads." + Description = "Reuse previously downloaded assets to avoid redundant requests." )] - public bool ShouldReuseMedia { get; init; } + public bool ShouldReuseAssets { get; init; } [CommandOption( "dateformat", @@ -97,9 +97,9 @@ public abstract class ExportCommandBase : TokenCommandBase protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList channels) { - // Reuse media option should only be used when the media option is set. + // Reuse assets option should only be used when the download assets option is set. // https://github.com/Tyrrrz/DiscordChatExporter/issues/425 - if (ShouldReuseMedia && !ShouldDownloadMedia) + if (ShouldReuseAssets && !ShouldDownloadAssets) { throw new CommandException( "Option --reuse-media cannot be used without --media." @@ -158,8 +158,8 @@ public abstract class ExportCommandBase : TokenCommandBase Before, PartitionLimit, MessageFilter, - ShouldDownloadMedia, - ShouldReuseMedia, + ShouldDownloadAssets, + ShouldReuseAssets, DateFormat ); diff --git a/DiscordChatExporter.Core/Exporting/MediaDownloader.cs b/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs similarity index 92% rename from DiscordChatExporter.Core/Exporting/MediaDownloader.cs rename to DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs index 1fa5fc5..097b44f 100644 --- a/DiscordChatExporter.Core/Exporting/MediaDownloader.cs +++ b/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs @@ -12,18 +12,18 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal partial class MediaDownloader +internal partial class ExportAssetDownloader { private readonly string _workingDirPath; - private readonly bool _reuseMedia; + private readonly bool _reuse; - // File paths of already downloaded media + // File paths of the previously downloaded assets private readonly Dictionary _pathCache = new(StringComparer.Ordinal); - public MediaDownloader(string workingDirPath, bool reuseMedia) + public ExportAssetDownloader(string workingDirPath, bool reuse) { _workingDirPath = workingDirPath; - _reuseMedia = reuseMedia; + _reuse = reuse; } public async ValueTask DownloadAsync(string url, CancellationToken cancellationToken = default) @@ -35,7 +35,7 @@ internal partial class MediaDownloader var filePath = Path.Combine(_workingDirPath, fileName); // Reuse existing files if we're allowed to - if (!_reuseMedia || !File.Exists(filePath)) + if (!_reuse || !File.Exists(filePath)) { Directory.CreateDirectory(_workingDirPath); @@ -76,7 +76,7 @@ internal partial class MediaDownloader } } -internal partial class MediaDownloader +internal partial class ExportAssetDownloader { private static string GetUrlHash(string url) { diff --git a/DiscordChatExporter.Core/Exporting/ExportContext.cs b/DiscordChatExporter.Core/Exporting/ExportContext.cs index 44719d3..cd79e7b 100644 --- a/DiscordChatExporter.Core/Exporting/ExportContext.cs +++ b/DiscordChatExporter.Core/Exporting/ExportContext.cs @@ -14,7 +14,7 @@ namespace DiscordChatExporter.Core.Exporting; internal class ExportContext { - private readonly MediaDownloader _mediaDownloader; + private readonly ExportAssetDownloader _assetDownloader; public ExportRequest Request { get; } @@ -35,7 +35,7 @@ internal class ExportContext Channels = channels; Roles = roles; - _mediaDownloader = new MediaDownloader(request.OutputMediaDirPath, request.ShouldReuseMedia); + _assetDownloader = new ExportAssetDownloader(request.OutputAssetsDirPath, request.ShouldReuseAssets); } public string FormatDate(DateTimeOffset date) => Request.DateFormat switch @@ -63,14 +63,14 @@ internal class ExportContext .FirstOrDefault(); } - public async ValueTask ResolveMediaUrlAsync(string url, CancellationToken cancellationToken = default) + public async ValueTask ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default) { - if (!Request.ShouldDownloadMedia) + if (!Request.ShouldDownloadAssets) return url; try { - var filePath = await _mediaDownloader.DownloadAsync(url, cancellationToken); + var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken); // We want relative path so that the output files can be copied around without breaking. // Base directory path may be null if the file is stored at the root or relative to working directory. diff --git a/DiscordChatExporter.Core/Exporting/ExportRequest.cs b/DiscordChatExporter.Core/Exporting/ExportRequest.cs index c772f28..7d1b1a9 100644 --- a/DiscordChatExporter.Core/Exporting/ExportRequest.cs +++ b/DiscordChatExporter.Core/Exporting/ExportRequest.cs @@ -19,8 +19,8 @@ public partial record ExportRequest( Snowflake? Before, PartitionLimit PartitionLimit, MessageFilter MessageFilter, - bool ShouldDownloadMedia, - bool ShouldReuseMedia, + bool ShouldDownloadAssets, + bool ShouldReuseAssets, string DateFormat) { private string? _outputBaseFilePath; @@ -35,7 +35,7 @@ public partial record ExportRequest( public string OutputBaseDirPath => Path.GetDirectoryName(OutputBaseFilePath) ?? OutputPath; - public string OutputMediaDirPath => $"{OutputBaseFilePath}_Files{Path.DirectorySeparatorChar}"; + public string OutputAssetsDirPath => $"{OutputBaseFilePath}_Files{Path.DirectorySeparatorChar}"; } public partial record ExportRequest diff --git a/DiscordChatExporter.Core/Exporting/Writers/CsvMessageWriter.cs b/DiscordChatExporter.Core/Exporting/Writers/CsvMessageWriter.cs index 94349f7..214ba1b 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/CsvMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/CsvMessageWriter.cs @@ -37,7 +37,7 @@ internal partial class CsvMessageWriter : MessageWriter buffer .AppendIfNotEmpty(',') - .Append(await Context.ResolveMediaUrlAsync(attachment.Url, cancellationToken)); + .Append(await Context.ResolveAssetUrlAsync(attachment.Url, cancellationToken)); } await _writer.WriteAsync(CsvEncode(buffer.ToString())); diff --git a/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml b/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml index f7fa6c4..738ac2c 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml @@ -10,13 +10,17 @@ @inherits MiniRazor.TemplateBase @{ - ValueTask ResolveUrlAsync(string url) => Model.ExportContext.ResolveMediaUrlAsync(url); + ValueTask ResolveAssetUrlAsync(string url) => + Model.ExportContext.ResolveAssetUrlAsync(url); - string FormatDate(DateTimeOffset date) => Model.ExportContext.FormatDate(date); + string FormatDate(DateTimeOffset date) => + Model.ExportContext.FormatDate(date); - ValueTask FormatMarkdownAsync(string markdown) => Model.FormatMarkdownAsync(markdown); + ValueTask FormatMarkdownAsync(string markdown) => + Model.FormatMarkdownAsync(markdown); - ValueTask FormatEmbedMarkdownAsync(string markdown) => Model.FormatMarkdownAsync(markdown, false); + ValueTask FormatEmbedMarkdownAsync(string markdown) => + Model.FormatMarkdownAsync(markdown, false); var firstMessage = Model.Messages.First(); @@ -102,7 +106,7 @@ } // Avatar - Avatar + Avatar } else { @@ -119,7 +123,7 @@
@if (message.ReferencedMessage is not null) { - Avatar + Avatar
@referencedUserNick
@@ -200,20 +204,20 @@ @{/* Attachment preview */} @if (attachment.IsImage) { - - @(attachment.Description ?? + + @(attachment.Description ?? } else if (attachment.IsVideo) { } else if (attachment.IsAudio) { } else @@ -223,7 +227,7 @@ @@ -270,7 +274,7 @@
@if (!string.IsNullOrWhiteSpace(embed.Author.IconUrl)) { - Author icon + Author icon } @if (!string.IsNullOrWhiteSpace(embed.Author.Name)) @@ -319,8 +323,8 @@ else if (embed.Kind == EmbedKind.Image && !string.IsNullOrWhiteSpace(embed.Url)) { } @@ -329,7 +333,7 @@ {
} @@ -356,7 +360,7 @@
@if (!string.IsNullOrWhiteSpace(embed.Author.IconUrl)) { - Author icon + Author icon } @if (!string.IsNullOrWhiteSpace(embed.Author.Name)) @@ -430,8 +434,8 @@ @if (embed.Thumbnail is not null && !string.IsNullOrWhiteSpace(embed.Thumbnail.Url)) { } @@ -446,8 +450,8 @@ if (!string.IsNullOrWhiteSpace(image.Url)) { } @@ -462,7 +466,7 @@ @{/* Footer icon */} @if (!string.IsNullOrWhiteSpace(embed.Footer?.IconUrl)) { - Footer icon + Footer icon } @@ -496,11 +500,11 @@
@if (sticker.Format is StickerFormat.Png or StickerFormat.PngAnimated) { - Sticker + Sticker } else if (sticker.Format == StickerFormat.Lottie) { -
+
}
} @@ -512,7 +516,7 @@ @foreach (var reaction in message.Reactions) {
- @reaction.Emoji.Name + @reaction.Emoji.Name @reaction.Count
} diff --git a/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml b/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml index 311201e..d5710fb 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml @@ -14,11 +14,14 @@ string GetFontUrl(int weight) => $"https://cdn.jsdelivr.net/gh/Tyrrrz/DiscordFonts@master/whitney-{weight}.woff"; - ValueTask ResolveUrlAsync(string url) => Model.ExportContext.ResolveMediaUrlAsync(url, CancellationToken); + ValueTask ResolveAssetUrlAsync(string url) => + Model.ExportContext.ResolveAssetUrlAsync(url, CancellationToken); - string FormatDate(DateTimeOffset date) => Model.ExportContext.FormatDate(date); + string FormatDate(DateTimeOffset date) => + Model.ExportContext.FormatDate(date); - ValueTask FormatMarkdownAsync(string markdown) => Model.FormatMarkdownAsync(markdown); + ValueTask FormatMarkdownAsync(string markdown) => + Model.FormatMarkdownAsync(markdown); } @@ -32,31 +35,31 @@ @{/* Styling */} @{/* Syntax highlighting */} - - + + @{/* Lottie animation support */} - +