From 30cba7959feb1d763724c160595eeb40cda6fbb5 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Thu, 11 Apr 2019 22:56:29 +0300 Subject: [PATCH] Refactor string checks and fix exception when exporting multiple channels Fix #164 --- DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj | 2 +- DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs | 4 ++-- DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs | 2 +- DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs | 2 +- .../DiscordChatExporter.Core.Markdown.csproj | 2 +- DiscordChatExporter.Core.Markdown/MarkdownParser.cs | 2 +- DiscordChatExporter.Core.Markdown/Nodes/EmojiNode.cs | 6 ++++-- .../DiscordChatExporter.Core.Models.csproj | 2 +- DiscordChatExporter.Core.Models/Emoji.cs | 2 +- DiscordChatExporter.Core.Models/Guild.cs | 6 ++++-- DiscordChatExporter.Core.Models/User.cs | 3 ++- DiscordChatExporter.Core.Rendering/HtmlChatLogRenderer.cs | 2 +- DiscordChatExporter.Core.Services/DataService.Parsers.cs | 4 ++-- DiscordChatExporter.Core.Services/DataService.cs | 2 +- .../DiscordChatExporter.Core.Services.csproj | 2 +- DiscordChatExporter.Core.Services/ExportService.cs | 4 ++-- DiscordChatExporter.Core.Services/Helpers/ExportHelper.cs | 3 ++- DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj | 2 +- .../ViewModels/Dialogs/ExportSetupViewModel.cs | 3 ++- DiscordChatExporter.Gui/ViewModels/RootViewModel.cs | 4 ++-- 20 files changed, 33 insertions(+), 26 deletions(-) diff --git a/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj b/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj index db538f3..9dd13ac 100644 --- a/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj +++ b/DiscordChatExporter.Cli/DiscordChatExporter.Cli.csproj @@ -13,7 +13,7 @@ - + diff --git a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs index 00c7a36..1dc7c57 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChannelVerb.cs @@ -24,7 +24,7 @@ namespace DiscordChatExporter.Cli.Verbs var exportService = Container.Instance.Get(); // Configure settings - if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace()) + if (!Options.DateFormat.IsNullOrWhiteSpace()) settingsService.DateFormat = Options.DateFormat; // Track progress @@ -37,7 +37,7 @@ namespace DiscordChatExporter.Cli.Verbs // Generate file path if not set or is a directory var filePath = Options.OutputPath; - if (filePath.EmptyIfNull().IsWhiteSpace() || ExportHelper.IsDirectoryPath(filePath)) + if (filePath.IsNullOrWhiteSpace() || ExportHelper.IsDirectoryPath(filePath)) { // Generate default file name var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild, diff --git a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs index 61e8946..1dd844f 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportDirectMessagesVerb.cs @@ -27,7 +27,7 @@ namespace DiscordChatExporter.Cli.Verbs var exportService = Container.Instance.Get(); // Configure settings - if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace()) + if (!Options.DateFormat.IsNullOrWhiteSpace()) settingsService.DateFormat = Options.DateFormat; // Get channels diff --git a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs index c7eef82..f271deb 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportGuildVerb.cs @@ -28,7 +28,7 @@ namespace DiscordChatExporter.Cli.Verbs var exportService = Container.Instance.Get(); // Configure settings - if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace()) + if (!Options.DateFormat.IsNullOrWhiteSpace()) settingsService.DateFormat = Options.DateFormat; // Get channels diff --git a/DiscordChatExporter.Core.Markdown/DiscordChatExporter.Core.Markdown.csproj b/DiscordChatExporter.Core.Markdown/DiscordChatExporter.Core.Markdown.csproj index 03f624f..20446bc 100644 --- a/DiscordChatExporter.Core.Markdown/DiscordChatExporter.Core.Markdown.csproj +++ b/DiscordChatExporter.Core.Markdown/DiscordChatExporter.Core.Markdown.csproj @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/DiscordChatExporter.Core.Markdown/MarkdownParser.cs b/DiscordChatExporter.Core.Markdown/MarkdownParser.cs index e9e6a29..ce7dc13 100644 --- a/DiscordChatExporter.Core.Markdown/MarkdownParser.cs +++ b/DiscordChatExporter.Core.Markdown/MarkdownParser.cs @@ -113,7 +113,7 @@ namespace DiscordChatExporter.Core.Markdown // Capture <:lul:123456> or private static readonly IMatcher CustomEmojiNodeMatcher = new RegexMatcher( new Regex("<(a)?:(.+?):(\\d+?)>", DefaultRegexOptions), - m => new EmojiNode(m.Value, m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsEmpty())); + m => new EmojiNode(m.Value, m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsNullOrWhiteSpace())); /* Links */ diff --git a/DiscordChatExporter.Core.Markdown/Nodes/EmojiNode.cs b/DiscordChatExporter.Core.Markdown/Nodes/EmojiNode.cs index f372c4a..86cef1d 100644 --- a/DiscordChatExporter.Core.Markdown/Nodes/EmojiNode.cs +++ b/DiscordChatExporter.Core.Markdown/Nodes/EmojiNode.cs @@ -1,4 +1,6 @@ -namespace DiscordChatExporter.Core.Markdown.Nodes +using Tyrrrz.Extensions; + +namespace DiscordChatExporter.Core.Markdown.Nodes { public class EmojiNode : Node { @@ -8,7 +10,7 @@ public bool IsAnimated { get; } - public bool IsCustomEmoji => Id != null; + public bool IsCustomEmoji => !Id.IsNullOrWhiteSpace(); public EmojiNode(string source, string id, string name, bool isAnimated) : base(source) diff --git a/DiscordChatExporter.Core.Models/DiscordChatExporter.Core.Models.csproj b/DiscordChatExporter.Core.Models/DiscordChatExporter.Core.Models.csproj index 03f624f..20446bc 100644 --- a/DiscordChatExporter.Core.Models/DiscordChatExporter.Core.Models.csproj +++ b/DiscordChatExporter.Core.Models/DiscordChatExporter.Core.Models.csproj @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/DiscordChatExporter.Core.Models/Emoji.cs b/DiscordChatExporter.Core.Models/Emoji.cs index b976e1f..9de011f 100644 --- a/DiscordChatExporter.Core.Models/Emoji.cs +++ b/DiscordChatExporter.Core.Models/Emoji.cs @@ -40,7 +40,7 @@ namespace DiscordChatExporter.Core.Models public static string GetImageUrl(string id, string name, bool isAnimated) { // Custom emoji - if (id != null) + if (!id.IsNullOrWhiteSpace()) { // Animated if (isAnimated) diff --git a/DiscordChatExporter.Core.Models/Guild.cs b/DiscordChatExporter.Core.Models/Guild.cs index cb70a19..2fb93c1 100644 --- a/DiscordChatExporter.Core.Models/Guild.cs +++ b/DiscordChatExporter.Core.Models/Guild.cs @@ -1,4 +1,6 @@ -namespace DiscordChatExporter.Core.Models +using Tyrrrz.Extensions; + +namespace DiscordChatExporter.Core.Models { // https://discordapp.com/developers/docs/resources/guild#guild-object @@ -28,7 +30,7 @@ { public static string GetIconUrl(string id, string iconHash) { - return iconHash != null + return !iconHash.IsNullOrWhiteSpace() ? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png" : "https://cdn.discordapp.com/embed/avatars/0.png"; } diff --git a/DiscordChatExporter.Core.Models/User.cs b/DiscordChatExporter.Core.Models/User.cs index 2a5154c..c239ef4 100644 --- a/DiscordChatExporter.Core.Models/User.cs +++ b/DiscordChatExporter.Core.Models/User.cs @@ -1,4 +1,5 @@ using System; +using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Models { @@ -39,7 +40,7 @@ namespace DiscordChatExporter.Core.Models public static string GetAvatarUrl(string id, int discriminator, string avatarHash) { // Custom avatar - if (avatarHash != null) + if (!avatarHash.IsNullOrWhiteSpace()) { // Animated if (avatarHash.StartsWith("a_", StringComparison.Ordinal)) diff --git a/DiscordChatExporter.Core.Rendering/HtmlChatLogRenderer.cs b/DiscordChatExporter.Core.Rendering/HtmlChatLogRenderer.cs index a2daa02..331c2b2 100644 --- a/DiscordChatExporter.Core.Rendering/HtmlChatLogRenderer.cs +++ b/DiscordChatExporter.Core.Rendering/HtmlChatLogRenderer.cs @@ -92,7 +92,7 @@ namespace DiscordChatExporter.Core.Rendering if (node is MultilineCodeBlockNode multilineCodeBlockNode) { // Set language class for syntax highlighting - var languageCssClass = multilineCodeBlockNode.Language != null + var languageCssClass = !multilineCodeBlockNode.Language.IsNullOrWhiteSpace() ? "language-" + multilineCodeBlockNode.Language : null; diff --git a/DiscordChatExporter.Core.Services/DataService.Parsers.cs b/DiscordChatExporter.Core.Services/DataService.Parsers.cs index 5b4cac4..1d8cea4 100644 --- a/DiscordChatExporter.Core.Services/DataService.Parsers.cs +++ b/DiscordChatExporter.Core.Services/DataService.Parsers.cs @@ -41,14 +41,14 @@ namespace DiscordChatExporter.Core.Services var guildId = json["guild_id"]?.Value(); // If the guild ID is blank, it's direct messages - if (guildId == null) + if (guildId.IsNullOrWhiteSpace()) guildId = Guild.DirectMessages.Id; // Try to extract name var name = json["name"]?.Value(); // If the name is blank, it's direct messages - if (name == null) + if (name.IsNullOrWhiteSpace()) name = json["recipients"].Select(ParseUser).Select(u => u.Name).JoinToString(", "); return new Channel(id, parentId, guildId, name, topic, type); diff --git a/DiscordChatExporter.Core.Services/DataService.cs b/DiscordChatExporter.Core.Services/DataService.cs index cec862e..c6d0f3b 100644 --- a/DiscordChatExporter.Core.Services/DataService.cs +++ b/DiscordChatExporter.Core.Services/DataService.cs @@ -48,7 +48,7 @@ namespace DiscordChatExporter.Core.Services var value = parameter.SubstringAfter("="); // Skip empty values - if (value.IsEmpty()) + if (value.IsNullOrWhiteSpace()) continue; request.RequestUri = request.RequestUri.SetQueryParameter(key, value); diff --git a/DiscordChatExporter.Core.Services/DiscordChatExporter.Core.Services.csproj b/DiscordChatExporter.Core.Services/DiscordChatExporter.Core.Services.csproj index 42a5f6f..ffa8a48 100644 --- a/DiscordChatExporter.Core.Services/DiscordChatExporter.Core.Services.csproj +++ b/DiscordChatExporter.Core.Services/DiscordChatExporter.Core.Services.csproj @@ -8,7 +8,7 @@ - + diff --git a/DiscordChatExporter.Core.Services/ExportService.cs b/DiscordChatExporter.Core.Services/ExportService.cs index 85d544d..5c4b70c 100644 --- a/DiscordChatExporter.Core.Services/ExportService.cs +++ b/DiscordChatExporter.Core.Services/ExportService.cs @@ -38,7 +38,7 @@ namespace DiscordChatExporter.Core.Services { // Create output directory var dirPath = Path.GetDirectoryName(filePath); - if (!dirPath.EmptyIfNull().IsWhiteSpace()) + if (!dirPath.IsNullOrWhiteSpace()) Directory.CreateDirectory(dirPath); // Render chat log to output file @@ -74,7 +74,7 @@ namespace DiscordChatExporter.Core.Services var partitionFilePath = $"{fileNameWithoutExt} [{partitionNumber} of {partitions.Length}]{fileExt}"; // Compose full file path - if (!dirPath.EmptyIfNull().IsWhiteSpace()) + if (!dirPath.IsNullOrWhiteSpace()) partitionFilePath = Path.Combine(dirPath, partitionFilePath); // Export diff --git a/DiscordChatExporter.Core.Services/Helpers/ExportHelper.cs b/DiscordChatExporter.Core.Services/Helpers/ExportHelper.cs index 9ea3bbb..142b70d 100644 --- a/DiscordChatExporter.Core.Services/Helpers/ExportHelper.cs +++ b/DiscordChatExporter.Core.Services/Helpers/ExportHelper.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; using DiscordChatExporter.Core.Models; +using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Services.Helpers { @@ -11,7 +12,7 @@ namespace DiscordChatExporter.Core.Services.Helpers public static bool IsDirectoryPath(string path) => path.Last() == Path.DirectorySeparatorChar || path.Last() == Path.AltDirectorySeparatorChar || - Path.GetExtension(path) == null; + (Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path)); public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel, DateTimeOffset? after = null, DateTimeOffset? before = null) diff --git a/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj b/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj index 805d860..56d7cde 100644 --- a/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj +++ b/DiscordChatExporter.Gui/DiscordChatExporter.Gui.csproj @@ -148,7 +148,7 @@ 2.0.20525 - 1.6.0 + 1.6.1 diff --git a/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs index 4858d49..bd0df60 100644 --- a/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Dialogs/ExportSetupViewModel.cs @@ -6,6 +6,7 @@ using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services.Helpers; using DiscordChatExporter.Gui.ViewModels.Components; using DiscordChatExporter.Gui.ViewModels.Framework; +using Tyrrrz.Extensions; namespace DiscordChatExporter.Gui.ViewModels.Dialogs { @@ -84,7 +85,7 @@ namespace DiscordChatExporter.Gui.ViewModels.Dialogs } // If canceled - return - if (OutputPath == null) + if (OutputPath.IsNullOrWhiteSpace()) return; // Close dialog diff --git a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs index e5b2408..f1e2475 100644 --- a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs @@ -122,7 +122,7 @@ namespace DiscordChatExporter.Gui.ViewModels await _dialogManager.ShowDialogAsync(dialog); } - public bool CanPopulateGuildsAndChannels => !IsBusy && !TokenValue.EmptyIfNull().IsWhiteSpace(); + public bool CanPopulateGuildsAndChannels => !IsBusy && !TokenValue.IsNullOrWhiteSpace(); public async void PopulateGuildsAndChannels() { @@ -235,7 +235,7 @@ namespace DiscordChatExporter.Gui.ViewModels } } - public bool CanExportChannels => !IsBusy && SelectedChannels.EmptyIfNull().Any(); + public bool CanExportChannels => !IsBusy && !SelectedChannels.IsNullOrEmpty(); public async void ExportChannels() {