Track dimensions of image attachments

This commit is contained in:
chylex 2022-07-18 20:55:35 +02:00
parent 6837b05b0d
commit bbc734ba9b
No known key found for this signature in database
GPG Key ID: 4DE42C8F19A80548
8 changed files with 47 additions and 12 deletions

View File

@ -251,6 +251,11 @@ const STATE = (function() {
mapped.type = attachment.content_type;
}
if (attachment.width && attachment.height) {
mapped.width = attachment.width;
mapped.height = attachment.height;
}
return mapped;
});
}

View File

@ -240,11 +240,14 @@ const DISCORD = (function() {
}
return value.map(attachment => {
if (DISCORD.isImageAttachment(attachment) && SETTINGS.enableImagePreviews) {
return templateEmbedImage.apply({ url: attachment.url, src: attachment.url });
if (!DISCORD.isImageAttachment(attachment) || !SETTINGS.enableImagePreviews) {
return templateAttachmentDownload.apply(attachment);
}
else if ("width" in attachment && "height" in attachment) {
return templateEmbedImageWithSize.apply({ url: attachment.url, src: attachment.url, width: attachment.width, height: attachment.height });
}
else {
return templateAttachmentDownload.apply(attachment);
return templateEmbedImage.apply({ url: attachment.url, src: attachment.url });
}
}).join("");
}

View File

@ -5,5 +5,7 @@ namespace DHT.Server.Data {
public string? Type { get; internal init; }
public string Url { get; internal init; }
public ulong Size { get; internal init; }
public int? Width { get; internal init; }
public int? Height { get; internal init; }
}
}

View File

@ -166,9 +166,18 @@ namespace DHT.Server.Database.Export {
}
if (!message.Attachments.IsEmpty) {
obj["a"] = message.Attachments.Select(attachment => new Dictionary<string, object> {
{ "url", strategy.GetAttachmentUrl(attachment) },
{ "name", Uri.TryCreate(attachment.Url, UriKind.Absolute, out var uri) ? Path.GetFileName(uri.LocalPath) : attachment.Url }
obj["a"] = message.Attachments.Select(attachment => {
var a = new Dictionary<string, object> {
{ "url", strategy.GetAttachmentUrl(attachment) },
{ "name", Uri.TryCreate(attachment.Url, UriKind.Absolute, out var uri) ? Path.GetFileName(uri.LocalPath) : attachment.Url }
};
if (attachment.Width != null && attachment.Height != null) {
a["width"] = attachment.Width;
a["height"] = attachment.Height;
}
return a;
}).ToArray();
}

View File

@ -6,7 +6,7 @@ using DHT.Utils.Logging;
namespace DHT.Server.Database.Sqlite {
sealed class Schema {
internal const int Version = 4;
internal const int Version = 5;
private static readonly Log Log = Log.ForType<Schema>();
@ -79,7 +79,9 @@ namespace DHT.Server.Database.Sqlite {
name TEXT NOT NULL,
type TEXT,
url TEXT NOT NULL,
size INTEGER NOT NULL)");
size INTEGER NOT NULL,
width INTEGER,
height INTEGER)");
Execute(@"CREATE TABLE embeds (
message_id INTEGER NOT NULL,
@ -159,6 +161,12 @@ namespace DHT.Server.Database.Sqlite {
perf.Step("Upgrade to version 4");
}
if (dbVersion <= 4) {
Execute("ALTER TABLE attachments ADD width INTEGER");
Execute("ALTER TABLE attachments ADD height INTEGER");
perf.Step("Upgrade to version 5");
}
perf.End();
}
}

View File

@ -252,7 +252,9 @@ namespace DHT.Server.Database.Sqlite {
("name", SqliteType.Text),
("type", SqliteType.Text),
("url", SqliteType.Text),
("size", SqliteType.Integer)
("size", SqliteType.Integer),
("width", SqliteType.Integer),
("height", SqliteType.Integer)
});
using var embedCmd = conn.Insert("embeds", new[] {
@ -307,6 +309,8 @@ namespace DHT.Server.Database.Sqlite {
attachmentCmd.Set(":type", attachment.Type);
attachmentCmd.Set(":url", attachment.Url);
attachmentCmd.Set(":size", attachment.Size);
attachmentCmd.Set(":width", attachment.Width);
attachmentCmd.Set(":height", attachment.Height);
attachmentCmd.ExecuteNonQuery();
}
}
@ -571,7 +575,7 @@ FROM downloads");
var dict = new MultiDictionary<ulong, Attachment>();
using var conn = pool.Take();
using var cmd = conn.Command("SELECT message_id, attachment_id, name, type, url, size FROM attachments");
using var cmd = conn.Command("SELECT message_id, attachment_id, name, type, url, size, width, height FROM attachments");
using var reader = cmd.ExecuteReader();
while (reader.Read()) {
@ -582,7 +586,9 @@ FROM downloads");
Name = reader.GetString(2),
Type = reader.IsDBNull(3) ? null : reader.GetString(3),
Url = reader.GetString(4),
Size = reader.GetUint64(5)
Size = reader.GetUint64(5),
Width = reader.IsDBNull(6) ? null : reader.GetInt32(6),
Height = reader.IsDBNull(7) ? null : reader.GetInt32(7)
});
}

View File

@ -61,7 +61,9 @@ namespace DHT.Server.Endpoints {
Name = ele.RequireString("name", path),
Type = ele.HasKey("type") ? ele.RequireString("type", path) : null,
Url = ele.RequireString("url", path),
Size = (ulong) ele.RequireLong("size", path)
Size = (ulong) ele.RequireLong("size", path),
Width = ele.HasKey("width") ? ele.RequireInt("width", path) : null,
Height = ele.HasKey("height") ? ele.RequireInt("height", path) : null
}).DistinctByKeyStable(static attachment => {
// Some Discord messages have duplicate attachments with the same id for unknown reasons.
return attachment.Id;

Binary file not shown.