Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions openclaw-channel-dmwork/src/api-fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,81 @@ describe("sendMediaMessage", () => {
expect(payload.height).toBeUndefined();
});
});

// ---------------------------------------------------------------------------
// ensureTextCharset
// ---------------------------------------------------------------------------
describe("ensureTextCharset", () => {
it("appends charset=utf-8 to text/plain", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("text/plain")).toBe("text/plain; charset=utf-8");
});

it("appends charset=utf-8 to text/markdown", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("text/markdown")).toBe("text/markdown; charset=utf-8");
});

it("appends charset=utf-8 to text/html", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("text/html")).toBe("text/html; charset=utf-8");
});

it("does not modify image/jpeg", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("image/jpeg")).toBe("image/jpeg");
});

it("does not double-add charset if already present", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("text/plain; charset=utf-8")).toBe("text/plain; charset=utf-8");
});

it("does not override existing charset=gbk", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("text/plain; charset=gbk")).toBe("text/plain; charset=gbk");
});

it("does not modify application/json", async () => {
const { ensureTextCharset } = await import("./api-fetch.js");
expect(ensureTextCharset("application/json")).toBe("application/json");
});
});

// ---------------------------------------------------------------------------
// uploadFileToCOS — putParams includes ContentType
// ---------------------------------------------------------------------------
describe("uploadFileToCOS putParams ContentType", () => {
it("passes ContentType to cos.putObject", async () => {
let capturedParams: any = null;

vi.resetModules();

// Mock cos-nodejs-sdk-v5 before importing api-fetch
vi.doMock("cos-nodejs-sdk-v5", () => {
return {
default: class FakeCOS {
putObject(params: any, cb: any) {
capturedParams = params;
cb(null, { Location: "bucket.cos.region.myqcloud.com/key" });
}
},
};
});

const { uploadFileToCOS } = await import("./api-fetch.js");
await uploadFileToCOS({
credentials: { tmpSecretId: "id", tmpSecretKey: "key", sessionToken: "tok" },
startTime: 0,
expiredTime: 9999999999,
bucket: "test-bucket",
region: "ap-test",
key: "test/file.txt",
fileBody: Buffer.from("hello"),
contentType: "text/plain; charset=utf-8",
});

expect(capturedParams).not.toBeNull();
expect(capturedParams.ContentType).toBe("text/plain; charset=utf-8");
});
});
17 changes: 16 additions & 1 deletion openclaw-channel-dmwork/src/api-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,25 @@ export function inferContentType(filename: string): string {
".pdf": "application/pdf", ".zip": "application/zip",
".doc": "application/msword", ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xls": "application/vnd.ms-excel", ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".txt": "text/plain", ".json": "application/json",
".txt": "text/plain", ".md": "text/markdown", ".markdown": "text/markdown",
".csv": "text/csv", ".html": "text/html", ".htm": "text/html",
".css": "text/css", ".xml": "text/xml", ".yaml": "text/yaml", ".yml": "text/yaml",
".json": "application/json",
};
return map[ext] ?? "application/octet-stream";
}

/**
* Ensure text/* content types include a charset parameter.
* If the content type starts with "text/" and has no charset, appends "; charset=utf-8".
*/
export function ensureTextCharset(contentType: string): string {
if (contentType.startsWith("text/") && !contentType.includes("charset")) {
return contentType + "; charset=utf-8";
}
return contentType;
}

/**
* Parse image dimensions from buffer (PNG/JPEG/GIF/WebP).
* Lightweight — reads only the header bytes, no external dependencies.
Expand Down Expand Up @@ -577,6 +591,7 @@ export async function uploadFileToCOS(params: {
Region: params.region,
Key: params.key,
Body: params.fileBody,
ContentType: params.contentType,
};
if (params.fileSize != null) {
putParams.ContentLength = params.fileSize;
Expand Down
6 changes: 3 additions & 3 deletions openclaw-channel-dmwork/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
resolveDmworkAccount,
type ResolvedDmworkAccount,
} from "./accounts.js";
import { registerBot, sendMessage, sendHeartbeat, sendMediaMessage, inferContentType, fetchBotGroups, getGroupMd, parseImageDimensions, parseImageDimensionsFromFile, getUploadCredentials, uploadFileToCOS } from "./api-fetch.js";
import { registerBot, sendMessage, sendHeartbeat, sendMediaMessage, inferContentType, ensureTextCharset, fetchBotGroups, getGroupMd, parseImageDimensions, parseImageDimensionsFromFile, getUploadCredentials, uploadFileToCOS } from "./api-fetch.js";
import { WKSocket } from "./socket.js";
import { handleInboundMessage, type DmworkStatusSink } from "./inbound.js";
import { ChannelType, MessageType, type BotMessage, type MessagePayload } from "./types.js";
Expand Down Expand Up @@ -445,7 +445,7 @@ export const dmworkPlugin: ChannelPlugin<ResolvedDmworkAccount> = {
tempPath = dl.tempPath;
localFilePath = dl.tempPath;
contentType = dl.contentType;
if (!contentType) contentType = inferContentType(filename);
if (!contentType || contentType === "application/octet-stream") contentType = inferContentType(filename);
const st = statSync(tempPath);
fileBody = createReadStream(tempPath);
fileSize = st.size;
Expand All @@ -469,7 +469,7 @@ export const dmworkPlugin: ChannelPlugin<ResolvedDmworkAccount> = {
key: creds.key,
fileBody,
fileSize,
contentType,
contentType: ensureTextCharset(contentType),
cdnBaseUrl: creds.cdnBaseUrl,
});

Expand Down
4 changes: 2 additions & 2 deletions openclaw-channel-dmwork/src/inbound.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChannelLogSink, OpenClawConfig } from "openclaw/plugin-sdk";
import { sendMessage, sendReadReceipt, sendTyping, getChannelMessages, getGroupMembers, getGroupMd, postJson, sendMediaMessage, inferContentType, parseImageDimensions, parseImageDimensionsFromFile, getUploadCredentials, uploadFileToCOS } from "./api-fetch.js";
import { sendMessage, sendReadReceipt, sendTyping, getChannelMessages, getGroupMembers, getGroupMd, postJson, sendMediaMessage, inferContentType, ensureTextCharset, parseImageDimensions, parseImageDimensionsFromFile, getUploadCredentials, uploadFileToCOS } from "./api-fetch.js";
import type { ResolvedDmworkAccount } from "./accounts.js";
import type { BotMessage } from "./types.js";
import { ChannelType, MessageType } from "./types.js";
Expand Down Expand Up @@ -160,7 +160,7 @@ export async function uploadAndSendMedia(params: {
key: creds.key,
fileBody,
fileSize,
contentType,
contentType: ensureTextCharset(contentType),
cdnBaseUrl: creds.cdnBaseUrl,
});

Expand Down
Loading