fix(msteams): download image attachments reliably
This commit is contained in:
@@ -1,13 +1,5 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import {
|
|
||||||
buildMSTeamsAttachmentPlaceholder,
|
|
||||||
buildMSTeamsGraphMessageUrls,
|
|
||||||
buildMSTeamsMediaPayload,
|
|
||||||
downloadMSTeamsGraphMedia,
|
|
||||||
downloadMSTeamsImageAttachments,
|
|
||||||
} from "./attachments.js";
|
|
||||||
|
|
||||||
const detectMimeMock = vi.fn(async () => "image/png");
|
const detectMimeMock = vi.fn(async () => "image/png");
|
||||||
const saveMediaBufferMock = vi.fn(async () => ({
|
const saveMediaBufferMock = vi.fn(async () => ({
|
||||||
path: "/tmp/saved.png",
|
path: "/tmp/saved.png",
|
||||||
@@ -23,18 +15,24 @@ vi.mock("../media/store.js", () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
describe("msteams attachments", () => {
|
describe("msteams attachments", () => {
|
||||||
|
const load = async () => {
|
||||||
|
return await import("./attachments.js");
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
detectMimeMock.mockClear();
|
detectMimeMock.mockClear();
|
||||||
saveMediaBufferMock.mockClear();
|
saveMediaBufferMock.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("buildMSTeamsAttachmentPlaceholder", () => {
|
describe("buildMSTeamsAttachmentPlaceholder", () => {
|
||||||
it("returns empty string when no attachments", () => {
|
it("returns empty string when no attachments", async () => {
|
||||||
|
const { buildMSTeamsAttachmentPlaceholder } = await load();
|
||||||
expect(buildMSTeamsAttachmentPlaceholder(undefined)).toBe("");
|
expect(buildMSTeamsAttachmentPlaceholder(undefined)).toBe("");
|
||||||
expect(buildMSTeamsAttachmentPlaceholder([])).toBe("");
|
expect(buildMSTeamsAttachmentPlaceholder([])).toBe("");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns image placeholder for image attachments", () => {
|
it("returns image placeholder for image attachments", async () => {
|
||||||
|
const { buildMSTeamsAttachmentPlaceholder } = await load();
|
||||||
expect(
|
expect(
|
||||||
buildMSTeamsAttachmentPlaceholder([
|
buildMSTeamsAttachmentPlaceholder([
|
||||||
{ contentType: "image/png", contentUrl: "https://x/img.png" },
|
{ contentType: "image/png", contentUrl: "https://x/img.png" },
|
||||||
@@ -48,7 +46,8 @@ describe("msteams attachments", () => {
|
|||||||
).toBe("<media:image> (2 images)");
|
).toBe("<media:image> (2 images)");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("treats Teams file.download.info image attachments as images", () => {
|
it("treats Teams file.download.info image attachments as images", async () => {
|
||||||
|
const { buildMSTeamsAttachmentPlaceholder } = await load();
|
||||||
expect(
|
expect(
|
||||||
buildMSTeamsAttachmentPlaceholder([
|
buildMSTeamsAttachmentPlaceholder([
|
||||||
{
|
{
|
||||||
@@ -59,7 +58,8 @@ describe("msteams attachments", () => {
|
|||||||
).toBe("<media:image>");
|
).toBe("<media:image>");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns document placeholder for non-image attachments", () => {
|
it("returns document placeholder for non-image attachments", async () => {
|
||||||
|
const { buildMSTeamsAttachmentPlaceholder } = await load();
|
||||||
expect(
|
expect(
|
||||||
buildMSTeamsAttachmentPlaceholder([
|
buildMSTeamsAttachmentPlaceholder([
|
||||||
{ contentType: "application/pdf", contentUrl: "https://x/x.pdf" },
|
{ contentType: "application/pdf", contentUrl: "https://x/x.pdf" },
|
||||||
@@ -73,7 +73,8 @@ describe("msteams attachments", () => {
|
|||||||
).toBe("<media:document> (2 files)");
|
).toBe("<media:document> (2 files)");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("counts inline images in text/html attachments", () => {
|
it("counts inline images in text/html attachments", async () => {
|
||||||
|
const { buildMSTeamsAttachmentPlaceholder } = await load();
|
||||||
expect(
|
expect(
|
||||||
buildMSTeamsAttachmentPlaceholder([
|
buildMSTeamsAttachmentPlaceholder([
|
||||||
{
|
{
|
||||||
@@ -96,6 +97,7 @@ describe("msteams attachments", () => {
|
|||||||
|
|
||||||
describe("downloadMSTeamsImageAttachments", () => {
|
describe("downloadMSTeamsImageAttachments", () => {
|
||||||
it("downloads and stores image contentUrl attachments", async () => {
|
it("downloads and stores image contentUrl attachments", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn(async () => {
|
const fetchMock = vi.fn(async () => {
|
||||||
return new Response(Buffer.from("png"), {
|
return new Response(Buffer.from("png"), {
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -112,13 +114,14 @@ describe("msteams attachments", () => {
|
|||||||
fetchFn: fetchMock as unknown as typeof fetch,
|
fetchFn: fetchMock as unknown as typeof fetch,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(media).toHaveLength(1);
|
|
||||||
expect(media[0]?.path).toBe("/tmp/saved.png");
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith("https://x/img");
|
expect(fetchMock).toHaveBeenCalledWith("https://x/img");
|
||||||
expect(saveMediaBufferMock).toHaveBeenCalled();
|
expect(saveMediaBufferMock).toHaveBeenCalled();
|
||||||
|
expect(media).toHaveLength(1);
|
||||||
|
expect(media[0]?.path).toBe("/tmp/saved.png");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("supports Teams file.download.info downloadUrl attachments", async () => {
|
it("supports Teams file.download.info downloadUrl attachments", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn(async () => {
|
const fetchMock = vi.fn(async () => {
|
||||||
return new Response(Buffer.from("png"), {
|
return new Response(Buffer.from("png"), {
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -138,11 +141,12 @@ describe("msteams attachments", () => {
|
|||||||
fetchFn: fetchMock as unknown as typeof fetch,
|
fetchFn: fetchMock as unknown as typeof fetch,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(media).toHaveLength(1);
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith("https://x/dl");
|
expect(fetchMock).toHaveBeenCalledWith("https://x/dl");
|
||||||
|
expect(media).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("downloads inline image URLs from html attachments", async () => {
|
it("downloads inline image URLs from html attachments", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn(async () => {
|
const fetchMock = vi.fn(async () => {
|
||||||
return new Response(Buffer.from("png"), {
|
return new Response(Buffer.from("png"), {
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -167,6 +171,7 @@ describe("msteams attachments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("stores inline data:image base64 payloads", async () => {
|
it("stores inline data:image base64 payloads", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const base64 = Buffer.from("png").toString("base64");
|
const base64 = Buffer.from("png").toString("base64");
|
||||||
const media = await downloadMSTeamsImageAttachments({
|
const media = await downloadMSTeamsImageAttachments({
|
||||||
attachments: [
|
attachments: [
|
||||||
@@ -184,6 +189,7 @@ describe("msteams attachments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("retries with auth when the first request is unauthorized", async () => {
|
it("retries with auth when the first request is unauthorized", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn(async (_url: string, opts?: RequestInit) => {
|
const fetchMock = vi.fn(async (_url: string, opts?: RequestInit) => {
|
||||||
const hasAuth = Boolean(
|
const hasAuth = Boolean(
|
||||||
opts &&
|
opts &&
|
||||||
@@ -210,11 +216,13 @@ describe("msteams attachments", () => {
|
|||||||
fetchFn: fetchMock as unknown as typeof fetch,
|
fetchFn: fetchMock as unknown as typeof fetch,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(fetchMock).toHaveBeenCalled();
|
||||||
expect(media).toHaveLength(1);
|
expect(media).toHaveLength(1);
|
||||||
expect(fetchMock).toHaveBeenCalledTimes(2);
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips urls outside the allowlist", async () => {
|
it("skips urls outside the allowlist", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn();
|
const fetchMock = vi.fn();
|
||||||
const media = await downloadMSTeamsImageAttachments({
|
const media = await downloadMSTeamsImageAttachments({
|
||||||
attachments: [
|
attachments: [
|
||||||
@@ -230,6 +238,7 @@ describe("msteams attachments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("ignores non-image attachments", async () => {
|
it("ignores non-image attachments", async () => {
|
||||||
|
const { downloadMSTeamsImageAttachments } = await load();
|
||||||
const fetchMock = vi.fn();
|
const fetchMock = vi.fn();
|
||||||
const media = await downloadMSTeamsImageAttachments({
|
const media = await downloadMSTeamsImageAttachments({
|
||||||
attachments: [
|
attachments: [
|
||||||
@@ -246,7 +255,8 @@ describe("msteams attachments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("buildMSTeamsGraphMessageUrls", () => {
|
describe("buildMSTeamsGraphMessageUrls", () => {
|
||||||
it("builds channel message urls", () => {
|
it("builds channel message urls", async () => {
|
||||||
|
const { buildMSTeamsGraphMessageUrls } = await load();
|
||||||
const urls = buildMSTeamsGraphMessageUrls({
|
const urls = buildMSTeamsGraphMessageUrls({
|
||||||
conversationType: "channel",
|
conversationType: "channel",
|
||||||
conversationId: "19:thread@thread.tacv2",
|
conversationId: "19:thread@thread.tacv2",
|
||||||
@@ -256,7 +266,8 @@ describe("msteams attachments", () => {
|
|||||||
expect(urls[0]).toContain("/teams/team-id/channels/chan-id/messages/123");
|
expect(urls[0]).toContain("/teams/team-id/channels/chan-id/messages/123");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds channel reply urls when replyToId is present", () => {
|
it("builds channel reply urls when replyToId is present", async () => {
|
||||||
|
const { buildMSTeamsGraphMessageUrls } = await load();
|
||||||
const urls = buildMSTeamsGraphMessageUrls({
|
const urls = buildMSTeamsGraphMessageUrls({
|
||||||
conversationType: "channel",
|
conversationType: "channel",
|
||||||
messageId: "reply-id",
|
messageId: "reply-id",
|
||||||
@@ -268,7 +279,8 @@ describe("msteams attachments", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds chat message urls", () => {
|
it("builds chat message urls", async () => {
|
||||||
|
const { buildMSTeamsGraphMessageUrls } = await load();
|
||||||
const urls = buildMSTeamsGraphMessageUrls({
|
const urls = buildMSTeamsGraphMessageUrls({
|
||||||
conversationType: "groupChat",
|
conversationType: "groupChat",
|
||||||
conversationId: "19:chat@thread.v2",
|
conversationId: "19:chat@thread.v2",
|
||||||
@@ -280,6 +292,7 @@ describe("msteams attachments", () => {
|
|||||||
|
|
||||||
describe("downloadMSTeamsGraphMedia", () => {
|
describe("downloadMSTeamsGraphMedia", () => {
|
||||||
it("downloads hostedContents images", async () => {
|
it("downloads hostedContents images", async () => {
|
||||||
|
const { downloadMSTeamsGraphMedia } = await load();
|
||||||
const base64 = Buffer.from("png").toString("base64");
|
const base64 = Buffer.from("png").toString("base64");
|
||||||
const fetchMock = vi.fn(async (url: string) => {
|
const fetchMock = vi.fn(async (url: string) => {
|
||||||
if (url.endsWith("/hostedContents")) {
|
if (url.endsWith("/hostedContents")) {
|
||||||
@@ -317,7 +330,8 @@ describe("msteams attachments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("buildMSTeamsMediaPayload", () => {
|
describe("buildMSTeamsMediaPayload", () => {
|
||||||
it("returns single and multi-file fields", () => {
|
it("returns single and multi-file fields", async () => {
|
||||||
|
const { buildMSTeamsMediaPayload } = await load();
|
||||||
const payload = buildMSTeamsMediaPayload([
|
const payload = buildMSTeamsMediaPayload([
|
||||||
{ path: "/tmp/a.png", contentType: "image/png" },
|
{ path: "/tmp/a.png", contentType: "image/png" },
|
||||||
{ path: "/tmp/b.png", contentType: "image/png" },
|
{ path: "/tmp/b.png", contentType: "image/png" },
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { fetchRemoteMedia } from "../media/fetch.js";
|
|
||||||
import { detectMime } from "../media/mime.js";
|
import { detectMime } from "../media/mime.js";
|
||||||
import { saveMediaBuffer } from "../media/store.js";
|
import { saveMediaBuffer } from "../media/store.js";
|
||||||
|
|
||||||
@@ -741,28 +740,22 @@ export async function downloadMSTeamsImageAttachments(params: {
|
|||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
if (!isUrlAllowed(candidate.url, allowHosts)) continue;
|
if (!isUrlAllowed(candidate.url, allowHosts)) continue;
|
||||||
try {
|
try {
|
||||||
const fetchImpl: typeof fetch = (input) => {
|
const res = await fetchWithAuthFallback({
|
||||||
const url =
|
url: candidate.url,
|
||||||
typeof input === "string"
|
|
||||||
? input
|
|
||||||
: input instanceof URL
|
|
||||||
? input.toString()
|
|
||||||
: input.url;
|
|
||||||
return fetchWithAuthFallback({
|
|
||||||
url,
|
|
||||||
tokenProvider: params.tokenProvider,
|
tokenProvider: params.tokenProvider,
|
||||||
fetchFn: params.fetchFn,
|
fetchFn: params.fetchFn,
|
||||||
});
|
});
|
||||||
};
|
if (!res.ok) continue;
|
||||||
const fetched = await fetchRemoteMedia({
|
const buffer = Buffer.from(await res.arrayBuffer());
|
||||||
url: candidate.url,
|
if (buffer.byteLength > params.maxBytes) continue;
|
||||||
fetchImpl,
|
const mime = await detectMime({
|
||||||
filePathHint: candidate.fileHint,
|
buffer,
|
||||||
|
headerMime: res.headers.get("content-type"),
|
||||||
|
filePath: candidate.fileHint ?? candidate.url,
|
||||||
});
|
});
|
||||||
if (fetched.buffer.byteLength > params.maxBytes) continue;
|
|
||||||
const saved = await saveMediaBuffer(
|
const saved = await saveMediaBuffer(
|
||||||
fetched.buffer,
|
buffer,
|
||||||
fetched.contentType ?? candidate.contentTypeHint,
|
mime ?? candidate.contentTypeHint,
|
||||||
"inbound",
|
"inbound",
|
||||||
params.maxBytes,
|
params.maxBytes,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user