refactor: split telegram delivery and unify media/frontmatter/i18n pipelines

This commit is contained in:
Peter Steinberger
2026-03-02 04:12:23 +00:00
parent 706cfcd54f
commit e1f3ded033
15 changed files with 1239 additions and 925 deletions

View File

@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import { buildOutboundMediaLoadOptions, resolveOutboundMediaLocalRoots } from "./load-options.js";
describe("media load options", () => {
it("returns undefined localRoots when mediaLocalRoots is empty", () => {
expect(resolveOutboundMediaLocalRoots(undefined)).toBeUndefined();
expect(resolveOutboundMediaLocalRoots([])).toBeUndefined();
});
it("keeps trusted mediaLocalRoots entries", () => {
expect(resolveOutboundMediaLocalRoots(["/tmp/workspace"])).toEqual(["/tmp/workspace"]);
});
it("builds loadWebMedia options from maxBytes and mediaLocalRoots", () => {
expect(
buildOutboundMediaLoadOptions({
maxBytes: 1024,
mediaLocalRoots: ["/tmp/workspace"],
}),
).toEqual({
maxBytes: 1024,
localRoots: ["/tmp/workspace"],
});
});
});

25
src/media/load-options.ts Normal file
View File

@@ -0,0 +1,25 @@
export type OutboundMediaLoadParams = {
maxBytes?: number;
mediaLocalRoots?: readonly string[];
};
export type OutboundMediaLoadOptions = {
maxBytes?: number;
localRoots?: readonly string[];
};
export function resolveOutboundMediaLocalRoots(
mediaLocalRoots?: readonly string[],
): readonly string[] | undefined {
return mediaLocalRoots && mediaLocalRoots.length > 0 ? mediaLocalRoots : undefined;
}
export function buildOutboundMediaLoadOptions(
params: OutboundMediaLoadParams = {},
): OutboundMediaLoadOptions {
const localRoots = resolveOutboundMediaLocalRoots(params.mediaLocalRoots);
return {
...(params.maxBytes !== undefined ? { maxBytes: params.maxBytes } : {}),
...(localRoots ? { localRoots } : {}),
};
}

View File

@@ -1,4 +1,5 @@
import { loadWebMedia } from "../web/media.js";
import { buildOutboundMediaLoadOptions } from "./load-options.js";
import { saveMediaBuffer } from "./store.js";
export async function resolveOutboundAttachmentFromUrl(
@@ -6,10 +7,13 @@ export async function resolveOutboundAttachmentFromUrl(
maxBytes: number,
options?: { localRoots?: readonly string[] },
): Promise<{ path: string; contentType?: string }> {
const media = await loadWebMedia(mediaUrl, {
maxBytes,
localRoots: options?.localRoots,
});
const media = await loadWebMedia(
mediaUrl,
buildOutboundMediaLoadOptions({
maxBytes,
mediaLocalRoots: options?.localRoots,
}),
);
const saved = await saveMediaBuffer(
media.buffer,
media.contentType ?? undefined,