You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DiscordChatExporter/DiscordChatExporter.Core/Exporting/ExportRequest.cs

121 lines
3.7 KiB

2 years ago
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
3 years ago
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exporting.Filtering;
3 years ago
using DiscordChatExporter.Core.Exporting.Partitioning;
3 years ago
using DiscordChatExporter.Core.Utils;
3 years ago
namespace DiscordChatExporter.Core.Exporting;
public partial record ExportRequest(
Guild Guild,
Channel Channel,
string OutputPath,
ExportFormat Format,
Snowflake? After,
Snowflake? Before,
PartitionLimit PartitionLimit,
MessageFilter MessageFilter,
bool ShouldDownloadMedia,
bool ShouldReuseMedia,
string DateFormat)
{
3 years ago
private string? _outputBaseFilePath;
public string OutputBaseFilePath => _outputBaseFilePath ??= GetOutputBaseFilePath(
Guild,
Channel,
OutputPath,
Format,
After,
Before
);
3 years ago
public string OutputBaseDirPath => Path.GetDirectoryName(OutputBaseFilePath) ?? OutputPath;
3 years ago
public string OutputMediaDirPath => $"{OutputBaseFilePath}_Files{Path.DirectorySeparatorChar}";
}
3 years ago
public partial record ExportRequest
{
private static string GetOutputBaseFilePath(
Guild guild,
Channel channel,
string outputPath,
ExportFormat format,
Snowflake? after = null,
Snowflake? before = null)
{
3 years ago
// Formats path
outputPath = Regex.Replace(outputPath, "%.", m =>
PathEx.EscapeFileName(m.Value switch
{
3 years ago
"%g" => guild.Id.ToString(),
"%G" => guild.Name,
"%t" => channel.Category.Id.ToString(),
"%T" => channel.Category.Name,
"%c" => channel.Id.ToString(),
"%C" => channel.Name,
"%p" => channel.Position?.ToString() ?? "0",
"%P" => channel.Category.Position?.ToString() ?? "0",
2 years ago
"%a" => after?.ToDate().ToString("yyyy-MM-dd") ?? "",
"%b" => before?.ToDate().ToString("yyyy-MM-dd") ?? "",
3 years ago
"%%" => "%",
_ => m.Value
})
);
3 years ago
// Output is a directory
if (Directory.Exists(outputPath) || string.IsNullOrWhiteSpace(Path.GetExtension(outputPath)))
{
var fileName = GetDefaultOutputFileName(guild, channel, format, after, before);
return Path.Combine(outputPath, fileName);
}
3 years ago
// Output is a file
return outputPath;
}
3 years ago
public static string GetDefaultOutputFileName(
Guild guild,
Channel channel,
ExportFormat format,
Snowflake? after = null,
Snowflake? before = null)
{
var buffer = new StringBuilder();
// Guild and channel names
buffer.Append($"{guild.Name} - {channel.Category.Name} - {channel.Name} [{channel.Id}]");
// Date range
if (after is not null || before is not null)
{
2 years ago
buffer.Append(' ').Append('(');
3 years ago
// Both 'after' and 'before' are set
if (after is not null && before is not null)
{
buffer.Append($"{after.Value.ToDate():yyyy-MM-dd} to {before.Value.ToDate():yyyy-MM-dd}");
}
// Only 'after' is set
else if (after is not null)
{
3 years ago
buffer.Append($"after {after.Value.ToDate():yyyy-MM-dd}");
}
// Only 'before' is set
else if (before is not null)
{
buffer.Append($"before {before.Value.ToDate():yyyy-MM-dd}");
}
2 years ago
buffer.Append(')');
3 years ago
}
3 years ago
// File extension
2 years ago
buffer.Append('.').Append(format.GetFileExtension());
return PathEx.EscapeFileName(buffer.ToString());
}
}