refactor: unify inline directives and media fetch

This commit is contained in:
Peter Steinberger
2026-01-10 03:01:04 +01:00
parent 4075895c4c
commit f28a4a34ad
15 changed files with 345 additions and 178 deletions

View File

@@ -45,7 +45,7 @@ import {
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
import { enqueueSystemEvent } from "../infra/system-events.js";
import { getChildLogger } from "../logging.js";
import { detectMime } from "../media/mime.js";
import { fetchRemoteMedia } from "../media/fetch.js";
import { saveMediaBuffer } from "../media/store.js";
import { buildPairingReply } from "../pairing/pairing-messages.js";
import {
@@ -355,27 +355,28 @@ async function resolveSlackMedia(params: {
const url = file.url_private_download ?? file.url_private;
if (!url) continue;
try {
const res = await fetch(url, {
headers: { Authorization: `Bearer ${params.token}` },
});
if (!res.ok) continue;
const buffer = Buffer.from(await res.arrayBuffer());
if (buffer.byteLength > params.maxBytes) continue;
const contentType = await detectMime({
buffer,
headerMime: res.headers.get("content-type"),
filePath: file.name,
const fetchImpl: typeof fetch = (input, init) => {
const headers = new Headers(init?.headers);
headers.set("Authorization", `Bearer ${params.token}`);
return fetch(input, { ...init, headers });
};
const fetched = await fetchRemoteMedia({
url,
fetchImpl,
filePathHint: file.name,
});
if (fetched.buffer.byteLength > params.maxBytes) continue;
const saved = await saveMediaBuffer(
buffer,
contentType ?? file.mimetype,
fetched.buffer,
fetched.contentType ?? file.mimetype,
"inbound",
params.maxBytes,
);
const label = fetched.fileName ?? file.name;
return {
path: saved.path,
contentType: saved.contentType,
placeholder: file.name ? `[Slack file: ${file.name}]` : "[Slack file]",
placeholder: label ? `[Slack file: ${label}]` : "[Slack file]",
};
} catch {
// Ignore download failures and fall through to the next file.