Refactor default file name generation and add date ranges there

Closes #29
pull/123/head
Alexey Golub 6 years ago
parent cacbb58683
commit dddb13fcc5

@ -1,8 +1,7 @@
using System; using System;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Cli.Verbs.Options; using DiscordChatExporter.Cli.Verbs.Options;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Helpers;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
@ -32,12 +31,16 @@ namespace DiscordChatExporter.Cli.Verbs
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId,
Options.After, Options.Before); Options.After, Options.Before);
// Generate file path if not set // Generate file path if not set or is a directory
var filePath = Options.FilePath; var filePath = Options.FilePath;
if (filePath == null || filePath.EndsWith("/") || filePath.EndsWith("\\")) if (filePath == null || filePath.EndsWith("/") || filePath.EndsWith("\\"))
{ {
filePath += $"{chatLog.Guild.Name} - {chatLog.Channel.Name}.{Options.ExportFormat.GetFileExtension()}" // Generate default file name
.Replace(Path.GetInvalidFileNameChars(), '_'); 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 // Export

@ -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();
}
}
}

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using DiscordChatExporter.Core.Helpers;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Gui.ViewModels.Framework; using DiscordChatExporter.Gui.ViewModels.Framework;
@ -59,10 +59,10 @@ namespace DiscordChatExporter.Gui.ViewModels.Dialogs
To = From; To = From;
// Generate default file name // Generate default file name
var ext = SelectedFormat.GetFileExtension(); var defaultFileName = ExportHelper.GetDefaultExportFileName(SelectedFormat, Guild, Channel, From, To);
var defaultFileName = $"{Guild.Name} - {Channel.Name}.{ext}".Replace(Path.GetInvalidFileNameChars(), '_');
// Prompt for output file path // Prompt for output file path
var ext = SelectedFormat.GetFileExtension();
var filter = $"{ext.ToUpperInvariant()} files|*.{ext}"; var filter = $"{ext.ToUpperInvariant()} files|*.{ext}";
FilePath = _dialogManager.PromptSaveFilePath(filter, defaultFileName); FilePath = _dialogManager.PromptSaveFilePath(filter, defaultFileName);

Loading…
Cancel
Save