[HTML] Special case plain image embeds

Closes #537
pull/678/head
Tyrrrz 3 years ago
parent 28b2039a33
commit aae3790a5f

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;
using DiscordChatExporter.Core.Discord.Data.Common;
using DiscordChatExporter.Core.Utils;
using DiscordChatExporter.Core.Utils.Extensions;
using JsonExtensions.Reading;
@ -24,11 +24,11 @@ namespace DiscordChatExporter.Core.Discord.Data
public int? Height { get; }
public bool IsImage => ImageFileExtensions.Contains(FileExtension);
public bool IsImage => FileFormat.IsImage(FileExtension);
public bool IsVideo => VideoFileExtensions.Contains(FileExtension);
public bool IsVideo => FileFormat.IsVideo(FileExtension);
public bool IsAudio => AudioFileExtensions.Contains(FileExtension);
public bool IsAudio => FileFormat.IsAudio(FileExtension);
public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal);
@ -56,15 +56,6 @@ namespace DiscordChatExporter.Core.Discord.Data
public partial class Attachment
{
private static readonly HashSet<string> ImageFileExtensions = new(StringComparer.OrdinalIgnoreCase)
{ ".jpg", ".jpeg", ".png", ".gif", ".gifv", ".bmp", ".webp" };
private static readonly HashSet<string> VideoFileExtensions = new(StringComparer.OrdinalIgnoreCase)
{ ".mp4", ".webm", ".mov" };
private static readonly HashSet<string> AudioFileExtensions = new(StringComparer.OrdinalIgnoreCase)
{ ".mp3", ".wav", ".ogg", ".flac", ".m4a" };
public static Attachment Parse(JsonElement json)
{
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);

@ -56,6 +56,8 @@ namespace DiscordChatExporter.Core.Discord.Data.Embeds
Footer = footer;
}
public PlainImageEmbedProjection? TryGetPlainImage() => PlainImageEmbedProjection.TryResolve(this);
public SpotifyTrackEmbedProjection? TryGetSpotifyTrack() => SpotifyTrackEmbedProjection.TryResolve(this);
public YouTubeVideoEmbedProjection? TryGetYouTubeVideo() => YouTubeVideoEmbedProjection.TryResolve(this);

@ -0,0 +1,45 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DiscordChatExporter.Core.Utils;
namespace DiscordChatExporter.Core.Discord.Data.Embeds
{
public partial class PlainImageEmbedProjection
{
public string Url { get; }
public PlainImageEmbedProjection(string url) => Url = url;
[ExcludeFromCodeCoverage]
public override string ToString() => Url;
}
public partial class PlainImageEmbedProjection
{
public static PlainImageEmbedProjection? TryResolve(Embed embed)
{
if (string.IsNullOrWhiteSpace(embed.Url))
return null;
// Has to be an embed without any data (except URL and image)
if (!string.IsNullOrWhiteSpace(embed.Title) ||
embed.Timestamp is not null ||
embed.Author is not null ||
!string.IsNullOrWhiteSpace(embed.Description) ||
embed.Fields.Any() ||
embed.Footer is not null)
{
return null;
}
// Has to be an image file
var fileName = Regex.Match(embed.Url, @".+/([^?]*)").Groups[1].Value;
if (string.IsNullOrWhiteSpace(fileName) || !FileFormat.IsImage(Path.GetExtension(fileName)))
return null;
return new PlainImageEmbedProjection(embed.Url);
}
}
}

@ -176,8 +176,17 @@
@{/* Embeds */}
@foreach (var embed in message.Embeds)
{
// Plain image embed
if (embed.TryGetPlainImage() is { } plainImageEmbed)
{
<div class="chatlog__embed">
<a href="@await ResolveUrlAsync(plainImageEmbed.Url)">
<img class="chatlog__embed-plainimage" src="@await ResolveUrlAsync(plainImageEmbed.Url)" alt="Embedded image" loading="lazy">
</a>
</div>
}
// Spotify embed
if (embed.TryGetSpotifyTrack() is { } spotifyTrackEmbed)
else if (embed.TryGetSpotifyTrack() is { } spotifyTrackEmbed)
{
<div class="chatlog__embed">
<div class="chatlog__embed-spotify-container">

@ -563,6 +563,13 @@
font-weight: 500;
}
.chatlog__embed-plainimage {
vertical-align: top;
max-width: 45vw;
max-height: 500px;
border-radius: 3px;
}
.chatlog__embed-spotify {
border: 0;
}

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Utils
{
public static class FileFormat
{
private static readonly HashSet<string> ImageFormats = new(StringComparer.OrdinalIgnoreCase)
{
".jpg",
".jpeg",
".png",
".gif",
".gifv",
".bmp",
".webp"
};
public static bool IsImage(string format) => ImageFormats.Contains(format);
private static readonly HashSet<string> VideoFormats = new(StringComparer.OrdinalIgnoreCase)
{
".mp4",
".webm",
".mov"
};
public static bool IsVideo(string format) => VideoFormats.Contains(format);
private static readonly HashSet<string> AudioFormats = new(StringComparer.OrdinalIgnoreCase)
{
".mp3",
".wav",
".ogg",
".flac",
".m4a"
};
public static bool IsAudio(string format) => AudioFormats.Contains(format);
}
}
Loading…
Cancel
Save