From dddb13fcc5ccefe96be7aaff3aded49b5c3c52c0 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Mon, 10 Dec 2018 23:16:15 +0200 Subject: [PATCH] Refactor default file name generation and add date ranges there Closes #29 --- .../Verbs/ExportChatVerb.cs | 13 +++-- .../Helpers/ExportHelper.cs | 52 +++++++++++++++++++ .../Dialogs/ExportSetupViewModel.cs | 6 +-- 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 DiscordChatExporter.Core/Helpers/ExportHelper.cs diff --git a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs index 25524d7..91b31ab 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs @@ -1,8 +1,7 @@ using System; -using System.IO; using System.Threading.Tasks; using DiscordChatExporter.Cli.Verbs.Options; -using DiscordChatExporter.Core.Models; +using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Services; using Tyrrrz.Extensions; @@ -32,12 +31,16 @@ namespace DiscordChatExporter.Cli.Verbs var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, Options.After, Options.Before); - // Generate file path if not set + // Generate file path if not set or is a directory var filePath = Options.FilePath; if (filePath == null || filePath.EndsWith("/") || filePath.EndsWith("\\")) { - filePath += $"{chatLog.Guild.Name} - {chatLog.Channel.Name}.{Options.ExportFormat.GetFileExtension()}" - .Replace(Path.GetInvalidFileNameChars(), '_'); + // Generate default file name + var defaultFileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, + chatLog.Channel, Options.After, Options.Before); + + // Append the file name to the file path + filePath += defaultFileName; } // Export diff --git a/DiscordChatExporter.Core/Helpers/ExportHelper.cs b/DiscordChatExporter.Core/Helpers/ExportHelper.cs new file mode 100644 index 0000000..fa90c78 --- /dev/null +++ b/DiscordChatExporter.Core/Helpers/ExportHelper.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Text; +using DiscordChatExporter.Core.Models; + +namespace DiscordChatExporter.Core.Helpers +{ + public static class ExportHelper + { + public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel, + DateTime? from = null, DateTime? to = null) + { + var result = new StringBuilder(); + + // Append guild and channel names + result.Append($"{guild.Name} - {channel.Name}"); + + // Append date range + if (from != null || to != null) + { + result.Append(" ("); + + // Both 'from' and 'to' are set + if (from != null && to != null) + { + result.Append($"{from:yyyy-MM-dd} to {to:yyyy-MM-dd}"); + } + // Only 'from' is set + else if (from != null) + { + result.Append($"after {from:yyyy-MM-dd}"); + } + // Only 'to' is set + else + { + result.Append($"before {to:yyyy-MM-dd}"); + } + + result.Append(")"); + } + + // Append extension + result.Append($".{format.GetFileExtension()}"); + + // Replace invalid chars + foreach (var invalidChar in Path.GetInvalidFileNameChars()) + result.Replace(invalidChar, '_'); + + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs index 3832828..707deca 100644 --- a/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; +using DiscordChatExporter.Core.Helpers; using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Services; using DiscordChatExporter.Gui.ViewModels.Framework; @@ -59,10 +59,10 @@ namespace DiscordChatExporter.Gui.ViewModels.Dialogs To = From; // Generate default file name - var ext = SelectedFormat.GetFileExtension(); - var defaultFileName = $"{Guild.Name} - {Channel.Name}.{ext}".Replace(Path.GetInvalidFileNameChars(), '_'); + var defaultFileName = ExportHelper.GetDefaultExportFileName(SelectedFormat, Guild, Channel, From, To); // Prompt for output file path + var ext = SelectedFormat.GetFileExtension(); var filter = $"{ext.ToUpperInvariant()} files|*.{ext}"; FilePath = _dialogManager.PromptSaveFilePath(filter, defaultFileName);