DiscordChatExporter/DiscordChatExporter.Core/Exporting/ExportRequest.cs

222 lines
6.4 KiB
C#
Raw Normal View History

using System;
2023-07-29 13:47:43 -04:00
using System.Globalization;
using System.IO;
2020-07-18 08:45:09 -04:00
using System.Text;
2021-01-28 15:01:13 -05:00
using System.Text.RegularExpressions;
2021-02-21 20:15:09 -05:00
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exporting.Filtering;
2021-04-16 16:09:08 -04:00
using DiscordChatExporter.Core.Exporting.Partitioning;
2021-02-21 20:15:09 -05:00
using DiscordChatExporter.Core.Utils;
using DiscordChatExporter.Core.Utils.Extensions;
2020-07-18 08:45:09 -04:00
2021-12-08 16:50:21 -05:00
namespace DiscordChatExporter.Core.Exporting;
public partial class ExportRequest
2020-07-18 08:45:09 -04:00
{
public Guild Guild { get; }
2023-07-16 15:55:36 -04:00
public Channel Channel { get; }
public string OutputFilePath { get; }
public string OutputDirPath { get; }
public string AssetsDirPath { get; }
public ExportFormat Format { get; }
public Snowflake? After { get; }
public Snowflake? Before { get; }
public PartitionLimit PartitionLimit { get; }
public MessageFilter MessageFilter { get; }
public bool ShouldFormatMarkdown { get; }
public bool ShouldDownloadAssets { get; }
public bool ShouldReuseAssets { get; }
public string? Locale { get; }
public CultureInfo? CultureInfo { get; }
public bool IsUtcNormalizationEnabled { get; }
public ExportRequest(
Guild guild,
2023-07-16 15:55:36 -04:00
Channel channel,
string outputPath,
string? assetsDirPath,
ExportFormat format,
Snowflake? after,
Snowflake? before,
PartitionLimit partitionLimit,
MessageFilter messageFilter,
bool shouldFormatMarkdown,
bool shouldDownloadAssets,
bool shouldReuseAssets,
string? locale,
bool isUtcNormalizationEnabled
2023-08-22 14:17:19 -04:00
)
{
Guild = guild;
Channel = channel;
Format = format;
After = after;
Before = before;
PartitionLimit = partitionLimit;
MessageFilter = messageFilter;
ShouldFormatMarkdown = shouldFormatMarkdown;
ShouldDownloadAssets = shouldDownloadAssets;
ShouldReuseAssets = shouldReuseAssets;
Locale = locale;
IsUtcNormalizationEnabled = isUtcNormalizationEnabled;
2023-08-22 14:17:19 -04:00
OutputFilePath = GetOutputBaseFilePath(Guild, Channel, outputPath, Format, After, Before);
OutputDirPath = Path.GetDirectoryName(OutputFilePath)!;
AssetsDirPath = !string.IsNullOrWhiteSpace(assetsDirPath)
2023-08-22 14:17:19 -04:00
? FormatPath(assetsDirPath, Guild, Channel, After, Before)
: $"{OutputFilePath}_Files{Path.DirectorySeparatorChar}";
CultureInfo = Locale?.Pipe(CultureInfo.GetCultureInfo);
}
2021-12-08 16:50:21 -05:00
}
2020-07-18 08:45:09 -04:00
public partial class ExportRequest
2021-12-08 16:50:21 -05:00
{
public static string GetDefaultOutputFileName(
Guild guild,
2023-07-16 15:55:36 -04:00
Channel channel,
2021-12-08 16:50:21 -05:00
ExportFormat format,
Snowflake? after = null,
2023-08-22 14:17:19 -04:00
Snowflake? before = null
)
2021-12-08 16:50:21 -05:00
{
var buffer = new StringBuilder();
// Guild name
buffer.Append(guild.Name);
// Parent name
if (channel.Parent is not null)
buffer.Append(" - ").Append(channel.Parent.Name);
// Channel name and ID
buffer
.Append(" - ")
.Append(channel.Name)
.Append(' ')
.Append('[')
.Append(channel.Id)
.Append(']');
2021-12-08 16:50:21 -05:00
// Date range
if (after is not null || before is not null)
{
2022-04-06 12:43:10 -04:00
buffer.Append(' ').Append('(');
2020-07-18 08:45:09 -04:00
2021-12-08 16:50:21 -05:00
// Both 'after' and 'before' are set
if (after is not null && before is not null)
{
2023-08-22 14:17:19 -04:00
buffer.Append(
$"{after.Value.ToDate():yyyy-MM-dd} to {before.Value.ToDate():yyyy-MM-dd}"
);
2021-12-08 16:50:21 -05:00
}
// Only 'after' is set
else if (after is not null)
2020-07-18 08:45:09 -04:00
{
2021-12-08 16:50:21 -05:00
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}");
2020-07-18 08:45:09 -04:00
}
2022-04-06 12:43:10 -04:00
buffer.Append(')');
2021-12-08 16:50:21 -05:00
}
2020-07-18 08:45:09 -04:00
2021-12-08 16:50:21 -05:00
// File extension
2022-04-06 12:43:10 -04:00
buffer.Append('.').Append(format.GetFileExtension());
2020-07-18 08:45:09 -04:00
return PathEx.EscapeFileName(buffer.ToString());
2020-07-18 08:45:09 -04:00
}
private static string FormatPath(
string path,
Guild guild,
2023-07-16 15:55:36 -04:00
Channel channel,
Snowflake? after,
2023-08-22 14:17:19 -04:00
Snowflake? before
) =>
Regex.Replace(
path,
2023-01-03 03:44:31 -05:00
"%.",
2023-08-22 14:17:19 -04:00
m =>
PathEx.EscapeFileName(
m.Value switch
{
"%g" => guild.Id.ToString(),
"%G" => guild.Name,
2023-08-22 14:17:19 -04:00
"%t" => channel.Parent?.Id.ToString() ?? "",
"%T" => channel.Parent?.Name ?? "",
2023-08-22 14:17:19 -04:00
"%c" => channel.Id.ToString(),
"%C" => channel.Name,
2023-08-22 14:17:19 -04:00
"%p" => channel.Position?.ToString(CultureInfo.InvariantCulture) ?? "0",
"%P"
=> channel.Parent?.Position?.ToString(CultureInfo.InvariantCulture)
?? "0",
2023-08-22 14:17:19 -04:00
"%a"
=> after?.ToDate().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
?? "",
"%b"
=> before?.ToDate().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
?? "",
"%d"
2023-12-28 17:08:16 -05:00
=> DateTimeOffset.Now.ToString(
"yyyy-MM-dd",
CultureInfo.InvariantCulture
),
2023-08-22 14:17:19 -04:00
"%%" => "%",
_ => m.Value
}
)
);
private static string GetOutputBaseFilePath(
Guild guild,
2023-07-16 15:55:36 -04:00
Channel channel,
string outputPath,
ExportFormat format,
Snowflake? after = null,
2023-08-22 14:17:19 -04:00
Snowflake? before = null
)
{
var actualOutputPath = FormatPath(outputPath, guild, channel, after, before);
// Output is a directory
2023-08-22 14:17:19 -04:00
if (
Directory.Exists(actualOutputPath)
|| string.IsNullOrWhiteSpace(Path.GetExtension(actualOutputPath))
)
{
var fileName = GetDefaultOutputFileName(guild, channel, format, after, before);
return Path.Combine(actualOutputPath, fileName);
}
// Output is a file
return actualOutputPath;
}
2023-08-22 14:17:19 -04:00
}