diff --git a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs index 5835414..c1a3cc0 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs @@ -48,7 +48,7 @@ namespace DiscordChatExporter.Cli.Verbs } // Export - exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); + await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); } } } diff --git a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs index 71bd295..d34d198 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs @@ -57,7 +57,8 @@ namespace DiscordChatExporter.Cli.Verbs var filePath = Path.Combine(Options.OutputPath ?? "", fileName); // Export - exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); + await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat, + Options.PartitionLimit); } } catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) diff --git a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs index ceb95f3..6ef9a7f 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs @@ -58,7 +58,8 @@ namespace DiscordChatExporter.Cli.Verbs var filePath = Path.Combine(Options.OutputPath ?? "", fileName); // Export - exportService.ExportChatLog(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit); + await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat, + Options.PartitionLimit); } } catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index e92d021..e2f5338 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -24,9 +24,9 @@ - + - + diff --git a/DiscordChatExporter.Core/Services/ExportService.TemplateLoader.cs b/DiscordChatExporter.Core/Services/ExportService.TemplateLoader.cs index 0bd4c6e..1af3fbd 100644 --- a/DiscordChatExporter.Core/Services/ExportService.TemplateLoader.cs +++ b/DiscordChatExporter.Core/Services/ExportService.TemplateLoader.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Threading.Tasks; using DiscordChatExporter.Core.Models; using Scriban; using Scriban.Parsing; @@ -28,6 +29,11 @@ namespace DiscordChatExporter.Core.Services return Assembly.GetExecutingAssembly().GetManifestResourceString(templatePath); } + public ValueTask LoadAsync(TemplateContext context, SourceSpan callerSpan, string templatePath) + { + return new ValueTask(Load(context, callerSpan, templatePath)); + } + public string Load(ExportFormat format) { return Assembly.GetExecutingAssembly().GetManifestResourceString(GetPath(format)); diff --git a/DiscordChatExporter.Core/Services/ExportService.cs b/DiscordChatExporter.Core/Services/ExportService.cs index 25b57e4..6077ea0 100644 --- a/DiscordChatExporter.Core/Services/ExportService.cs +++ b/DiscordChatExporter.Core/Services/ExportService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using DiscordChatExporter.Core.Internal; using DiscordChatExporter.Core.Models; using Scriban; @@ -18,7 +19,7 @@ namespace DiscordChatExporter.Core.Services _settingsService = settingsService; } - private void ExportChatLogSingle(ChatLog chatLog, string filePath, ExportFormat format) + private async Task ExportChatLogSingleAsync(ChatLog chatLog, string filePath, ExportFormat format) { // Create template loader var loader = new TemplateLoader(); @@ -39,7 +40,6 @@ namespace DiscordChatExporter.Core.Services // Create template model var templateModel = new TemplateModel(format, chatLog, _settingsService.DateFormat); - context.PushGlobal(templateModel.GetScriptObject()); // Create directory @@ -54,11 +54,11 @@ namespace DiscordChatExporter.Core.Services context.PushOutput(new TextWriterOutput(output)); // Render output - context.Evaluate(template.Page); + await context.EvaluateAsync(template.Page); } } - private void ExportChatLogPartitioned(IReadOnlyList partitions, string filePath, ExportFormat format) + private async Task ExportChatLogPartitionedAsync(IReadOnlyList partitions, string filePath, ExportFormat format) { // Split file path into components var dirPath = Path.GetDirectoryName(filePath); @@ -77,20 +77,20 @@ namespace DiscordChatExporter.Core.Services partitionFilePath = Path.Combine(dirPath, partitionFilePath); // Export - ExportChatLogSingle(partition, partitionFilePath, format); + await ExportChatLogSingleAsync(partition, partitionFilePath, format); // Increment partition number partitionNumber++; } } - public void ExportChatLog(ChatLog chatLog, string filePath, ExportFormat format, + public async Task ExportChatLogAsync(ChatLog chatLog, string filePath, ExportFormat format, int? partitionLimit = null) { // If partitioning is disabled or there are fewer messages in chat log than the limit - process it without partitioning if (partitionLimit == null || partitionLimit <= 0 || chatLog.Messages.Count <= partitionLimit) { - ExportChatLogSingle(chatLog, filePath, format); + await ExportChatLogSingleAsync(chatLog, filePath, format); } // Otherwise split into partitions and export separately else @@ -100,7 +100,7 @@ namespace DiscordChatExporter.Core.Services .Select(g => new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, g, chatLog.Mentionables)) .ToArray(); - ExportChatLogPartitioned(partitions, filePath, format); + await ExportChatLogPartitionedAsync(partitions, filePath, format); } } } diff --git a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs index aa9e4ab..cf033ac 100644 --- a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs @@ -278,7 +278,7 @@ namespace DiscordChatExporter.Gui.ViewModels dialog.From, dialog.To, operation); // Export - _exportService.ExportChatLog(chatLog, filePath, dialog.SelectedFormat, + await _exportService.ExportChatLogAsync(chatLog, filePath, dialog.SelectedFormat, dialog.PartitionLimit); // Notify completion