Skip to content

Commit 8fd6633

Browse files
committed
fix(mattermost): infer extensions for unnamed uploads
1 parent a124966 commit 8fd6633

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎extensions/mattermost/src/channel.send-loopback.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// Mattermost tests cover the action-to-REST send path over loopback.
22
import { createPluginRuntimeMock } from "openclaw/plugin-sdk/plugin-test-runtime";
33
import { withServer } from "openclaw/plugin-sdk/test-env";
4-
import { describe, expect, it } from "vitest";
4+
import { describe, expect, it, vi } from "vitest";
55
import { mattermostPlugin } from "./channel.js";
66
import type { OpenClawConfig } from "./runtime-api.js";
77
import { setMattermostRuntime } from "./runtime.js";
88

99
const CHANNEL_ID = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
10+
const loadOutboundMediaFromUrl = vi.hoisted(() => vi.fn());
11+
12+
vi.mock("./mattermost/runtime-api.js", async () => ({
13+
...(await vi.importActual<typeof import("./mattermost/runtime-api.js")>(
14+
"./mattermost/runtime-api.js",
15+
)),
16+
loadOutboundMediaFromUrl,
17+
}));
1018

1119
describe("Mattermost send action loopback", () => {
1220
it("sends text with blank attachment placeholders and rejects nonblank payloads", async () => {
@@ -92,4 +100,66 @@ describe("Mattermost send action loopback", () => {
92100
},
93101
);
94102
});
103+
104+
it("infers a MIME extension for unnamed uploads", async () => {
105+
const uploads: string[] = [];
106+
107+
await withServer(
108+
(request, response) => {
109+
let body = "";
110+
request.setEncoding("utf8");
111+
request.on("data", (chunk) => {
112+
body += chunk;
113+
});
114+
request.on("end", () => {
115+
if (request.url === "/api/v4/files") {
116+
uploads.push(body);
117+
response.writeHead(201, { "content-type": "application/json" });
118+
response.end(JSON.stringify({ file_infos: [{ id: `file-${uploads.length}` }] }));
119+
return;
120+
}
121+
response.writeHead(201, { "content-type": "application/json" });
122+
response.end(JSON.stringify({ id: "post-loopback", channel_id: CHANNEL_ID }));
123+
});
124+
},
125+
async (baseUrl) => {
126+
setMattermostRuntime(createPluginRuntimeMock());
127+
loadOutboundMediaFromUrl.mockReset();
128+
loadOutboundMediaFromUrl.mockResolvedValueOnce({
129+
buffer: Buffer.from("unnamed-image"),
130+
contentType: "image/png",
131+
kind: "image",
132+
});
133+
const cfg = {
134+
channels: {
135+
mattermost: {
136+
botToken: "loopback-fixture",
137+
baseUrl,
138+
network: { dangerouslyAllowPrivateNetwork: true },
139+
},
140+
},
141+
} as OpenClawConfig;
142+
const handleAction = mattermostPlugin.actions?.handleAction;
143+
if (!handleAction) {
144+
throw new Error("Mattermost send action missing");
145+
}
146+
147+
await handleAction({
148+
channel: "mattermost",
149+
action: "send",
150+
params: {
151+
to: `channel:${CHANNEL_ID}`,
152+
message: "loopback media proof",
153+
mediaUrl: "https://media.example.test/unnamed",
154+
},
155+
cfg,
156+
accountId: "default",
157+
});
158+
},
159+
);
160+
161+
expect(uploads).toHaveLength(1);
162+
expect(uploads[0]).toContain('filename="upload.png"');
163+
expect(uploads[0]).toContain("Content-Type: image/png");
164+
});
95165
});

‎extensions/mattermost/src/mattermost/send.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "openclaw/plugin-sdk/channel-outbound";
99
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
1010
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime";
11+
import { extensionForMime } from "openclaw/plugin-sdk/media-mime";
1112
import { requireRuntimeConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
1213
import { isPrivateNetworkOptInEnabled } from "openclaw/plugin-sdk/ssrf-runtime";
1314
import {
@@ -453,7 +454,7 @@ export async function sendMessageMattermost(
453454
const fileInfo = await uploadMattermostFile(client, {
454455
channelId,
455456
buffer: media.buffer,
456-
fileName: media.fileName ?? "upload",
457+
fileName: media.fileName ?? `upload${extensionForMime(media.contentType) ?? ""}`,
457458
contentType: media.contentType ?? undefined,
458459
});
459460
fileIds = [fileInfo.id];

0 commit comments

Comments
 (0)