diff --git a/app/Resources/Tracker/scripts/state.js b/app/Resources/Tracker/scripts/state.js index 74d64ef..15209c1 100644 --- a/app/Resources/Tracker/scripts/state.js +++ b/app/Resources/Tracker/scripts/state.js @@ -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; }); } diff --git a/app/Resources/Viewer/scripts/discord.js b/app/Resources/Viewer/scripts/discord.js index 9952d0d..9d576cc 100644 --- a/app/Resources/Viewer/scripts/discord.js +++ b/app/Resources/Viewer/scripts/discord.js @@ -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(""); } diff --git a/app/Server/Data/Attachment.cs b/app/Server/Data/Attachment.cs index e9be640..6ebacc7 100644 --- a/app/Server/Data/Attachment.cs +++ b/app/Server/Data/Attachment.cs @@ -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; } } } diff --git a/app/Server/Database/Export/ViewerJsonExport.cs b/app/Server/Database/Export/ViewerJsonExport.cs index 81f39be..45d35fc 100644 --- a/app/Server/Database/Export/ViewerJsonExport.cs +++ b/app/Server/Database/Export/ViewerJsonExport.cs @@ -166,9 +166,18 @@ namespace DHT.Server.Database.Export { } if (!message.Attachments.IsEmpty) { - obj["a"] = message.Attachments.Select(attachment => new Dictionary { - { "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 { + { "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(); } diff --git a/app/Server/Database/Sqlite/Schema.cs b/app/Server/Database/Sqlite/Schema.cs index 9930a91..9e417bf 100644 --- a/app/Server/Database/Sqlite/Schema.cs +++ b/app/Server/Database/Sqlite/Schema.cs @@ -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(); @@ -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(); } } diff --git a/app/Server/Database/Sqlite/SqliteDatabaseFile.cs b/app/Server/Database/Sqlite/SqliteDatabaseFile.cs index d64869f..169dde5 100644 --- a/app/Server/Database/Sqlite/SqliteDatabaseFile.cs +++ b/app/Server/Database/Sqlite/SqliteDatabaseFile.cs @@ -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(); 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) }); } diff --git a/app/Server/Endpoints/TrackMessagesEndpoint.cs b/app/Server/Endpoints/TrackMessagesEndpoint.cs index 6bc2d63..e508231 100644 --- a/app/Server/Endpoints/TrackMessagesEndpoint.cs +++ b/app/Server/Endpoints/TrackMessagesEndpoint.cs @@ -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; diff --git a/app/empty.dht b/app/empty.dht index 56a2a78..0458bf7 100644 Binary files a/app/empty.dht and b/app/empty.dht differ