mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 22:47:26 +00:00
refactor: split telegram delivery and unify media/frontmatter/i18n pipelines
This commit is contained in:
25
src/media/load-options.test.ts
Normal file
25
src/media/load-options.test.ts
Normal 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
25
src/media/load-options.ts
Normal 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 } : {}),
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user