refactor(media): share outbound attachment resolver

This commit is contained in:
Peter Steinberger
2026-02-15 13:53:22 +00:00
parent abb4b7c91c
commit 7d0c0bfc7c
3 changed files with 20 additions and 34 deletions

View File

@@ -2,8 +2,7 @@ import { loadConfig } from "../config/config.js";
import { resolveMarkdownTableMode } from "../config/markdown-tables.js"; import { resolveMarkdownTableMode } from "../config/markdown-tables.js";
import { convertMarkdownTables } from "../markdown/tables.js"; import { convertMarkdownTables } from "../markdown/tables.js";
import { mediaKindFromMime } from "../media/constants.js"; import { mediaKindFromMime } from "../media/constants.js";
import { saveMediaBuffer } from "../media/store.js"; import { resolveOutboundAttachmentFromUrl } from "../media/outbound-attachment.js";
import { loadWebMedia } from "../web/media.js";
import { resolveIMessageAccount, type ResolvedIMessageAccount } from "./accounts.js"; import { resolveIMessageAccount, type ResolvedIMessageAccount } from "./accounts.js";
import { createIMessageRpcClient, type IMessageRpcClient } from "./client.js"; import { createIMessageRpcClient, type IMessageRpcClient } from "./client.js";
import { formatIMessageChatTarget, type IMessageService, parseIMessageTarget } from "./targets.js"; import { formatIMessageChatTarget, type IMessageService, parseIMessageTarget } from "./targets.js";
@@ -46,20 +45,6 @@ function resolveMessageId(result: Record<string, unknown> | null | undefined): s
return raw ? String(raw).trim() : null; return raw ? String(raw).trim() : null;
} }
async function resolveAttachment(
mediaUrl: string,
maxBytes: number,
): Promise<{ path: string; contentType?: string }> {
const media = await loadWebMedia(mediaUrl, maxBytes);
const saved = await saveMediaBuffer(
media.buffer,
media.contentType ?? undefined,
"outbound",
maxBytes,
);
return { path: saved.path, contentType: saved.contentType };
}
export async function sendMessageIMessage( export async function sendMessageIMessage(
to: string, to: string,
text: string, text: string,
@@ -90,7 +75,7 @@ export async function sendMessageIMessage(
let filePath: string | undefined; let filePath: string | undefined;
if (opts.mediaUrl?.trim()) { if (opts.mediaUrl?.trim()) {
const resolveAttachmentFn = opts.resolveAttachmentImpl ?? resolveAttachment; const resolveAttachmentFn = opts.resolveAttachmentImpl ?? resolveOutboundAttachmentFromUrl;
const resolved = await resolveAttachmentFn(opts.mediaUrl.trim(), maxBytes); const resolved = await resolveAttachmentFn(opts.mediaUrl.trim(), maxBytes);
filePath = resolved.path; filePath = resolved.path;
if (!message.trim()) { if (!message.trim()) {

View File

@@ -0,0 +1,16 @@
import { loadWebMedia } from "../web/media.js";
import { saveMediaBuffer } from "./store.js";
export async function resolveOutboundAttachmentFromUrl(
mediaUrl: string,
maxBytes: number,
): Promise<{ path: string; contentType?: string }> {
const media = await loadWebMedia(mediaUrl, maxBytes);
const saved = await saveMediaBuffer(
media.buffer,
media.contentType ?? undefined,
"outbound",
maxBytes,
);
return { path: saved.path, contentType: saved.contentType };
}

View File

@@ -1,8 +1,7 @@
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { resolveMarkdownTableMode } from "../config/markdown-tables.js"; import { resolveMarkdownTableMode } from "../config/markdown-tables.js";
import { mediaKindFromMime } from "../media/constants.js"; import { mediaKindFromMime } from "../media/constants.js";
import { saveMediaBuffer } from "../media/store.js"; import { resolveOutboundAttachmentFromUrl } from "../media/outbound-attachment.js";
import { loadWebMedia } from "../web/media.js";
import { resolveSignalAccount } from "./accounts.js"; import { resolveSignalAccount } from "./accounts.js";
import { signalRpcRequest } from "./client.js"; import { signalRpcRequest } from "./client.js";
import { markdownToSignalText, type SignalTextStyleRange } from "./format.js"; import { markdownToSignalText, type SignalTextStyleRange } from "./format.js";
@@ -95,20 +94,6 @@ function buildTargetParams(
return null; return null;
} }
async function resolveAttachment(
mediaUrl: string,
maxBytes: number,
): Promise<{ path: string; contentType?: string }> {
const media = await loadWebMedia(mediaUrl, maxBytes);
const saved = await saveMediaBuffer(
media.buffer,
media.contentType ?? undefined,
"outbound",
maxBytes,
);
return { path: saved.path, contentType: saved.contentType };
}
export async function sendMessageSignal( export async function sendMessageSignal(
to: string, to: string,
text: string, text: string,
@@ -140,7 +125,7 @@ export async function sendMessageSignal(
let attachments: string[] | undefined; let attachments: string[] | undefined;
if (opts.mediaUrl?.trim()) { if (opts.mediaUrl?.trim()) {
const resolved = await resolveAttachment(opts.mediaUrl.trim(), maxBytes); const resolved = await resolveOutboundAttachmentFromUrl(opts.mediaUrl.trim(), maxBytes);
attachments = [resolved.path]; attachments = [resolved.path];
const kind = mediaKindFromMime(resolved.contentType ?? undefined); const kind = mediaKindFromMime(resolved.contentType ?? undefined);
if (!message && kind) { if (!message && kind) {