Skip to content

Commit

Permalink
Partial merge
Browse files Browse the repository at this point in the history
  • Loading branch information
nulldg committed Sep 28, 2023
2 parents b3eda8f + 9180d51 commit 0a9ed68
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 87 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v2.41.1 (28-Sep-2023)

- CLI changes:
- Fixed an issue where the export failed to export channels with an empty name.

## v2.41 (15-Sep-2023)

- General changes:
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>2.41</Version>
<Version>2.41.1</Version>
<Company>Tyrrrz</Company>
<Copyright>Copyright (c) Oleksii Holub</Copyright>
<LangVersion>preview</LangVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
<PackageReference Include="AngleSharp" Version="1.0.4" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.2" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
<PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="ReflectionMagic" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 22 additions & 9 deletions DiscordChatExporter.Cli.Tests/Specs/HtmlAttachmentSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Infra;
Expand Down Expand Up @@ -31,7 +32,11 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_generic_
.Select(e => e.GetAttribute("href"))
.Should()
.Contain(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
u =>
u.StartsWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt",
StringComparison.Ordinal
)
);
}

Expand All @@ -52,7 +57,11 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_a
.Select(e => e.GetAttribute("src"))
.Should()
.Contain(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
u =>
u.StartsWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png",
StringComparison.Ordinal
)
);
}

Expand All @@ -71,9 +80,11 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_video_at
message.Text().Should().Contain("Video attachment");

var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
videoUrl.Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
videoUrl
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
}

[Fact]
Expand All @@ -91,8 +102,10 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_an_audio_a
message.Text().Should().Contain("Audio attachment");

var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
audioUrl.Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
audioUrl
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
}
}
4 changes: 2 additions & 2 deletions DiscordChatExporter.Cli.Tests/Specs/HtmlEmbedSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_Spotify_

// Assert
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().Be("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
iframeUrl.Should().StartWith("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
}

[Fact]
Expand All @@ -150,7 +150,7 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_YouTube_

// Assert
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().Be("https://www.youtube.com/embed/qOWW4OlgbvE");
iframeUrl.Should().StartWith("https://www.youtube.com/embed/qOWW4OlgbvE");
}

[Fact]
Expand Down
11 changes: 8 additions & 3 deletions DiscordChatExporter.Cli.Tests/Specs/HtmlStickerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_PNG_stic

// Assert
var stickerUrl = message.QuerySelector("[title='rock'] img")?.GetAttribute("src");
stickerUrl.Should().Be("https://cdn.discordapp.com/stickers/904215665597120572.png");
stickerUrl.Should().StartWith("https://cdn.discordapp.com/stickers/904215665597120572.png");
}

[Fact]
Expand All @@ -32,7 +32,12 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_Lottie_s
);

// Assert
var stickerUrl = message.QuerySelector("[title='Yikes'] [data-source]")?.GetAttribute("data-source");
stickerUrl.Should().Be("https://cdn.discordapp.com/stickers/816087132447178774.json");
var stickerUrl = message
.QuerySelector("[title='Yikes'] [data-source]")
?.GetAttribute("data-source");

stickerUrl
.Should()
.StartWith("https://cdn.discordapp.com/stickers/816087132447178774.json");
}
}
49 changes: 34 additions & 15 deletions DiscordChatExporter.Cli.Tests/Specs/JsonAttachmentSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_generic_
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1);

attachments[0].GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
);
attachments[0]
.GetProperty("url")
.GetString()
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("Test.txt");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(11);
}
Expand All @@ -45,10 +49,13 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_an_image_a

var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1);

attachments[0].GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
);
attachments[0]
.GetProperty("url")
.GetString()
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(466335);
}
Expand All @@ -67,11 +74,20 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_video_at

var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1);
attachments[0]
.GetProperty("url")
.GetString()
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);

attachments[0]
.GetProperty("fileName")
.GetString()
.Should()
.Be("file_example_MP4_640_3MG.mp4");

attachments[0].GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("file_example_MP4_640_3MG.mp4");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(3114374);
}

Expand All @@ -89,10 +105,13 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_an_audio_a

var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1);

attachments[0].GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
attachments[0]
.GetProperty("url")
.GetString()
.Should()
.StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
attachments[0].GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3");
attachments[0].GetProperty("fileSizeBytes").GetInt64().Should().Be(1087849);
}
Expand Down
12 changes: 10 additions & 2 deletions DiscordChatExporter.Cli.Tests/Specs/JsonStickerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_PNG_stic
sticker.GetProperty("id").GetString().Should().Be("904215665597120572");
sticker.GetProperty("name").GetString().Should().Be("rock");
sticker.GetProperty("format").GetString().Should().Be("Apng");
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://cdn.discordapp.com/stickers/904215665597120572.png");
sticker
.GetProperty("sourceUrl")
.GetString()
.Should()
.StartWith("https://cdn.discordapp.com/stickers/904215665597120572.png");
}

[Fact]
Expand All @@ -48,6 +52,10 @@ public async Task I_can_export_a_channel_that_contains_a_message_with_a_Lottie_s
sticker.GetProperty("id").GetString().Should().Be("816087132447178774");
sticker.GetProperty("name").GetString().Should().Be("Yikes");
sticker.GetProperty("format").GetString().Should().Be("Lottie");
sticker.GetProperty("sourceUrl").GetString().Should().Be("https://cdn.discordapp.com/stickers/816087132447178774.json");
sticker
.GetProperty("sourceUrl")
.GetString()
.Should()
.StartWith("https://cdn.discordapp.com/stickers/816087132447178774.json");
}
}
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private async ValueTask<HttpResponseMessage> GetResponseAsync(
CancellationToken cancellationToken = default
)
{
return await Http.ResponseResiliencePolicy.ExecuteAsync(
return await Http.ResponseResiliencePipeline.ExecuteAsync(
async innerCancellationToken =>
{
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri, url));
Expand Down
4 changes: 2 additions & 2 deletions DiscordChatExporter.Core/DiscordChatExporter.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<PackageReference Include="AsyncKeyedLock" Version="6.2.1" />
<PackageReference Include="Gress" Version="2.1.1" />
<PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="RazorBlade" Version="0.4.3" />
<PackageReference Include="Polly" Version="8.0.0" />
<PackageReference Include="RazorBlade" Version="0.4.4" />
<PackageReference Include="Superpower" Version="3.0.0" />
<PackageReference Include="WebMarkupMin.Core" Version="2.14.0" />
</ItemGroup>
Expand Down
63 changes: 37 additions & 26 deletions DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,47 @@ public async ValueTask<string> DownloadAsync(string url, CancellationToken cance

Directory.CreateDirectory(_workingDirPath);

await Http.ResiliencePolicy.ExecuteAsync(async () =>
{
// Download the file
using var response = await Http.Client.GetAsync(url, cancellationToken);
await using (var output = File.Create(filePath))
await response.Content.CopyToAsync(output, cancellationToken);
// Try to set the file date according to the last-modified header
try
await Http.ResiliencePipeline.ExecuteAsync(
async innerCancellationToken =>
{
var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s =>
DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var instant)
? instant
: (DateTimeOffset?)null
);
// Download the file
using var response = await Http.Client.GetAsync(url, innerCancellationToken);
await using (var output = File.Create(filePath))
await response.Content.CopyToAsync(output, innerCancellationToken);
if (lastModified is not null)
// Try to set the file date according to the last-modified header
try
{
File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
var lastModified = response.Content.Headers
.TryGetValue("Last-Modified")
?.Pipe(
s =>
DateTimeOffset.TryParse(
s,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var instant
)
? instant
: (DateTimeOffset?)null
);
if (lastModified is not null)
{
File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
}
}
}
catch
{
// This can apparently fail for some reason.
// Updating the file date is not a critical task, so we'll just ignore exceptions thrown here.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585
}
});
catch
{
// This can apparently fail for some reason.
// Updating the file date is not a critical task, so we'll just ignore exceptions thrown here.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585
}
},
cancellationToken
);

return _previousPathsByUrl[url] = filePath;
}
Expand Down
Loading

0 comments on commit 0a9ed68

Please sign in to comment.