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

@@ -1,3 +1,4 @@
import { fetchRemoteMedia } from "../media/fetch.js";
import { detectMime } from "../media/mime.js";
import { saveMediaBuffer } from "../media/store.js";
@@ -740,23 +741,28 @@ export async function downloadMSTeamsImageAttachments(params: {
for (const candidate of candidates) {
if (!isUrlAllowed(candidate.url, allowHosts)) continue;
try {
const res = await fetchWithAuthFallback({
const fetchImpl: typeof fetch = (input) => {
const url =
typeof input === "string"
? input
: input instanceof URL
? input.toString()
: input.url;
return fetchWithAuthFallback({
url,
tokenProvider: params.tokenProvider,
fetchFn: params.fetchFn,
});
};
const fetched = await fetchRemoteMedia({
url: candidate.url,
tokenProvider: params.tokenProvider,
fetchFn: params.fetchFn,
});
if (!res.ok) continue;
const buffer = Buffer.from(await res.arrayBuffer());
if (buffer.byteLength > params.maxBytes) continue;
const mime = await detectMime({
buffer,
headerMime:
candidate.contentTypeHint ?? res.headers.get("content-type"),
filePath: candidate.fileHint ?? candidate.url,
fetchImpl,
filePathHint: candidate.fileHint,
});
if (fetched.buffer.byteLength > params.maxBytes) continue;
const saved = await saveMediaBuffer(
buffer,
mime,
fetched.buffer,
fetched.contentType ?? candidate.contentTypeHint,
"inbound",
params.maxBytes,
);