large files are now linked instead of uploaded

This commit is contained in:
Cadence Ember 2023-07-13 17:36:20 +12:00
parent 3a59d66626
commit f16900553a
6 changed files with 57 additions and 7 deletions

View file

@ -2,6 +2,7 @@
const assert = require("assert").strict
const markdown = require("discord-markdown")
const pb = require("prettier-bytes")
const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough")
@ -184,8 +185,24 @@ async function messageToEvent(message, guild, api) {
// Then attachments
const attachmentEvents = await Promise.all(message.attachments.map(async attachment => {
// TODO: handle large files differently - link them instead of uploading
if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) {
const emoji =
attachment.content_type?.startsWith("image/jp") ? "📸"
: attachment.content_type?.startsWith("image/") ? "🖼️"
: attachment.content_type?.startsWith("video/") ? "🎞️"
: attachment.content_type?.startsWith("text/") ? "📝"
: attachment.content_type?.startsWith("audio/") ? "🎶"
: "📄"
// for large files, always link them instead of uploading so I don't use up all the space in the content repo
if (attachment.size > reg.ooye.max_file_size) {
return {
$type: "m.room.message",
"m.mentions": mentions,
msgtype: "m.text",
body: `${emoji} Uploaded file: ${attachment.url} (${pb(attachment.size)})`,
format: "org.matrix.custom.html",
formatted_body: `${emoji} Uploaded file: <a href="${attachment.url}">${attachment.filename}</a> (${pb(attachment.size)})`
}
} else if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) {
return {
$type: "m.room.message",
"m.mentions": mentions,
@ -206,7 +223,7 @@ async function messageToEvent(message, guild, api) {
$type: "m.room.message",
"m.mentions": mentions,
msgtype: "m.text",
body: "Unsupported attachment:\n" + JSON.stringify(attachment, null, 2)
body: `Unsupported attachment:\n${JSON.stringify(attachment, null, 2)}\n${attachment.url}`
}
}
}))

View file

@ -258,4 +258,29 @@ test("message2event: simple written @mention for matrix user", async t => {
}])
})
test("message2event: very large attachment is linked instead of being uploaded", async t => {
const events = await messageToEvent({
content: "hey",
attachments: [{
filename: "hey.jpg",
url: "https://discord.com/404/hey.jpg",
content_type: "application/i-made-it-up",
size: 100e6
}]
})
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "hey"
}, {
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "📄 Uploaded file: https://discord.com/404/hey.jpg (100 MB)",
format: "org.matrix.custom.html",
formatted_body: '📄 Uploaded file: <a href="https://discord.com/404/hey.jpg">hey.jpg</a> (100 MB)'
}])
})
// TODO: read "edits of replies" in the spec