From abbe04b18493c362865f90201be844ead56f2647 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 22:43:30 +0000 Subject: [PATCH] refactor(discord): share attachment media resolution loop --- src/discord/monitor/message-utils.ts | 94 ++++++++++++++-------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/src/discord/monitor/message-utils.ts b/src/discord/monitor/message-utils.ts index 6b6e3555379..f0abf545def 100644 --- a/src/discord/monitor/message-utils.ts +++ b/src/discord/monitor/message-utils.ts @@ -131,28 +131,12 @@ export async function resolveMediaList( return []; } const out: DiscordMediaInfo[] = []; - for (const attachment of attachments) { - try { - const fetched = await fetchRemoteMedia({ - url: attachment.url, - filePathHint: attachment.filename ?? attachment.url, - }); - const saved = await saveMediaBuffer( - fetched.buffer, - fetched.contentType ?? attachment.content_type, - "inbound", - maxBytes, - ); - out.push({ - path: saved.path, - contentType: saved.contentType, - placeholder: inferPlaceholder(attachment), - }); - } catch (err) { - const id = attachment.id ?? attachment.url; - logVerbose(`discord: failed to download attachment ${id}: ${String(err)}`); - } - } + await appendResolvedMediaFromAttachments({ + attachments, + maxBytes, + out, + errorPrefix: "discord: failed to download attachment", + }); return out; } @@ -166,36 +150,50 @@ export async function resolveForwardedMediaList( } const out: DiscordMediaInfo[] = []; for (const snapshot of snapshots) { - const attachments = snapshot.message?.attachments; - if (!attachments || attachments.length === 0) { - continue; - } - for (const attachment of attachments) { - try { - const fetched = await fetchRemoteMedia({ - url: attachment.url, - filePathHint: attachment.filename ?? attachment.url, - }); - const saved = await saveMediaBuffer( - fetched.buffer, - fetched.contentType ?? attachment.content_type, - "inbound", - maxBytes, - ); - out.push({ - path: saved.path, - contentType: saved.contentType, - placeholder: inferPlaceholder(attachment), - }); - } catch (err) { - const id = attachment.id ?? attachment.url; - logVerbose(`discord: failed to download forwarded attachment ${id}: ${String(err)}`); - } - } + await appendResolvedMediaFromAttachments({ + attachments: snapshot.message?.attachments, + maxBytes, + out, + errorPrefix: "discord: failed to download forwarded attachment", + }); } return out; } +async function appendResolvedMediaFromAttachments(params: { + attachments?: APIAttachment[] | null; + maxBytes: number; + out: DiscordMediaInfo[]; + errorPrefix: string; +}) { + const attachments = params.attachments; + if (!attachments || attachments.length === 0) { + return; + } + for (const attachment of attachments) { + try { + const fetched = await fetchRemoteMedia({ + url: attachment.url, + filePathHint: attachment.filename ?? attachment.url, + }); + const saved = await saveMediaBuffer( + fetched.buffer, + fetched.contentType ?? attachment.content_type, + "inbound", + params.maxBytes, + ); + params.out.push({ + path: saved.path, + contentType: saved.contentType, + placeholder: inferPlaceholder(attachment), + }); + } catch (err) { + const id = attachment.id ?? attachment.url; + logVerbose(`${params.errorPrefix} ${id}: ${String(err)}`); + } + } +} + function inferPlaceholder(attachment: APIAttachment): string { const mime = attachment.content_type ?? ""; if (mime.startsWith("image/")) {