Skip to content

Commit b186d81

Browse files
steipetePeter Steinberger
andauthored
fix(media): preserve display metadata and attachment formats (#116928)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 0f88dd8 commit b186d81

9 files changed

Lines changed: 377 additions & 24 deletions

File tree

‎packages/media-core/src/mime.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe("mime detection", () => {
4141
}
4242

4343
it.each([
44+
{ format: "avif", expected: "image/avif" },
4445
{ format: "jpg", expected: "image/jpeg" },
4546
{ format: "jpeg", expected: "image/jpeg" },
4647
{ format: "png", expected: "image/png" },
@@ -275,6 +276,22 @@ describe("mime detection", () => {
275276
});
276277
});
277278

279+
it.each([
280+
{ brand: "avif", expected: "image/avif" },
281+
{ brand: "avis", expected: "image/avif" },
282+
{ brand: "M4B ", expected: "audio/mp4" },
283+
{ brand: "M4V ", expected: "video/x-m4v" },
284+
{ brand: "hevc", expected: "image/heic-sequence" },
285+
{ brand: "msf1", expected: "image/heif-sequence" },
286+
] as const)("preserves the file-type MIME for ISO-BMFF $brand media", async (testCase) => {
287+
const buffer = Buffer.alloc(24);
288+
buffer.writeUInt32BE(buffer.length, 0);
289+
buffer.write("ftyp", 4, "ascii");
290+
buffer.write(testCase.brand, 8, "ascii");
291+
292+
await expectDetectedMime({ input: { buffer }, expected: testCase.expected });
293+
});
294+
278295
it("does not let conflicting audio metadata override MPEG video bytes", async () => {
279296
const mpegProgramStream = Buffer.from([0x00, 0x00, 0x01, 0xba, 0x00, 0x00, 0x00, 0x00]);
280297

@@ -369,15 +386,20 @@ describe("getFileExtension", () => {
369386

370387
describe("mimeTypeFromFilePath", () => {
371388
it.each([
389+
{ filePath: "photo.avif", expected: "image/avif" },
372390
{ filePath: "image.bmp", expected: "image/bmp" },
391+
{ filePath: "photo.heic", expected: "image/heic" },
392+
{ filePath: "photo.heif", expected: "image/heif" },
373393
{ filePath: "photo.jpg", expected: "image/jpeg" },
374394
{ filePath: "photo.JPG", expected: "image/jpeg" },
375395
{ filePath: "voice.mp3", expected: "audio/mpeg" },
376396
{ filePath: "voice.m2a", expected: "audio/mpeg" },
397+
{ filePath: "audiobook.m4b", expected: "audio/mp4" },
377398
{ filePath: "voice.oga", expected: "audio/ogg" },
378399
{ filePath: "voice.amr", expected: "audio/amr" },
379400
{ filePath: "voice.wav", expected: "audio/wav" },
380401
{ filePath: "clip.avi", expected: "video/x-msvideo" },
402+
{ filePath: "clip.m4v", expected: "video/x-m4v" },
381403
{ filePath: "clip.mkv", expected: "video/x-matroska" },
382404
{ filePath: "clip.webm", expected: "video/webm" },
383405
{
@@ -415,6 +437,7 @@ describe("extensionForMime", () => {
415437
}
416438

417439
it.each([
440+
{ mime: "image/avif", expected: ".avif" },
418441
{ mime: "image/jpeg", expected: ".jpg" },
419442
{ mime: "image/jpg", expected: ".jpg" },
420443
{ mime: "image/bmp", expected: ".bmp" },
@@ -423,6 +446,9 @@ describe("extensionForMime", () => {
423446
{ mime: "image/webp", expected: ".webp" },
424447
{ mime: "image/gif", expected: ".gif" },
425448
{ mime: "image/heic", expected: ".heic" },
449+
{ mime: "image/heic-sequence", expected: ".heic" },
450+
{ mime: "image/heif", expected: ".heif" },
451+
{ mime: "image/heif-sequence", expected: ".heif" },
426452
{ mime: "audio/mpeg", expected: ".mp3" },
427453
{ mime: "audio/mp3", expected: ".mp3" },
428454
{ mime: "audio/ogg", expected: ".ogg" },
@@ -433,6 +459,7 @@ describe("extensionForMime", () => {
433459
{ mime: "audio/m4a", expected: ".m4a" },
434460
{ mime: "audio/mp4", expected: ".m4a" },
435461
{ mime: "video/x-msvideo", expected: ".avi" },
462+
{ mime: "video/x-m4v", expected: ".m4v" },
436463
{ mime: "video/mp4", expected: ".mp4" },
437464
{ mime: "video/x-matroska", expected: ".mkv" },
438465
{ mime: "video/webm", expected: ".webm" },
@@ -465,6 +492,7 @@ describe("isAudioFileName", () => {
465492
}
466493

467494
it.each([
495+
{ fileName: "audiobook.M4B", expected: true },
468496
{ fileName: "voice.mp3", expected: true },
469497
{ fileName: "voice.caf", expected: true },
470498
{ fileName: "voice.M2A", expected: true },

‎packages/media-core/src/mime.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ export const FILE_TYPE_SNIFF_MAX_BYTES = 1024 * 1024;
88

99
// Map common mimes to preferred file extensions.
1010
const EXT_BY_MIME: Record<string, string> = {
11+
"image/avif": ".avif",
1112
"image/heic": ".heic",
13+
"image/heic-sequence": ".heic",
1214
"image/heif": ".heif",
15+
"image/heif-sequence": ".heif",
1316
"image/bmp": ".bmp",
1417
"image/jpg": ".jpg",
1518
"image/jpeg": ".jpg",
@@ -33,6 +36,7 @@ const EXT_BY_MIME: Record<string, string> = {
3336
"audio/mp4": ".m4a",
3437
"audio/x-caf": ".caf",
3538
"video/x-msvideo": ".avi",
39+
"video/x-m4v": ".m4v",
3640
"video/mp4": ".mp4",
3741
"video/x-matroska": ".mkv",
3842
"video/webm": ".webm",
@@ -75,6 +79,7 @@ const MIME_BY_EXT: Record<string, string> = {
7579
// Canonical extension mappings for common MIME aliases
7680
".jpg": "image/jpeg",
7781
".m2a": "audio/mpeg",
82+
".m4b": "audio/mp4",
7883
".mp3": "audio/mpeg",
7984
".oga": "audio/ogg",
8085
".wav": "audio/wav",
@@ -283,6 +288,8 @@ export function imageMimeFromFormat(format?: string | null): string | undefined
283288
return undefined;
284289
}
285290
switch (format.toLowerCase()) {
291+
case "avif":
292+
return "image/avif";
286293
case "jpg":
287294
case "jpeg":
288295
return "image/jpeg";

‎src/auto-reply/reply/current-turn-images.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ describe("resolveCurrentTurnImages", () => {
5858
});
5959
});
6060

61+
it("hydrates AVIF attachments when transport metadata only declares generic bytes", async () => {
62+
await withTempDir({ prefix: "openclaw-current-turn-avif-" }, async (base) => {
63+
const imagePath = path.join(base, "photo.avif");
64+
const imageBytes = Buffer.from("avif-image");
65+
await fs.writeFile(imagePath, imageBytes);
66+
67+
const result = await resolveCurrentTurnImages({
68+
ctx: {
69+
Body: "caption",
70+
media: [{ path: imagePath, contentType: "application/octet-stream", workspaceDir: base }],
71+
} satisfies MsgContext,
72+
cfg: {} as OpenClawConfig,
73+
});
74+
75+
expect(result.images).toEqual([
76+
{
77+
type: "image",
78+
data: imageBytes.toString("base64"),
79+
mimeType: "image/avif",
80+
},
81+
]);
82+
expect(result.imageOrder).toEqual(["inline"]);
83+
});
84+
});
85+
6186
it("does not duplicate a prepared host-staged image during runner hydration", async () => {
6287
await withTempDir({ prefix: "openclaw-current-turn-staged-image-" }, async (base) => {
6388
const stagingRoot = path.join(base, "media", "inbound", "staged");

‎src/media/fetch.test.ts‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,143 @@ describe("readRemoteMediaBuffer", () => {
11941194
},
11951195
);
11961196

1197+
it.each([
1198+
{
1199+
name: "quoted filename containing a semicolon",
1200+
header: 'attachment; filename="quarter;final.csv"',
1201+
fileName: "quarter;final.csv",
1202+
},
1203+
{
1204+
name: "quoted filename containing an escaped quotation mark",
1205+
header: String.raw`attachment; filename="quarter\"final.csv"`,
1206+
fileName: 'quarter"final.csv',
1207+
},
1208+
{
1209+
name: "quoted filename containing an escaped semicolon",
1210+
header: String.raw`attachment; filename="quarter\;final.csv"`,
1211+
fileName: "quarter;final.csv",
1212+
},
1213+
{
1214+
name: "quoted filename containing an escaped comma",
1215+
header: String.raw`attachment; filename="quarter\,final.csv"`,
1216+
fileName: "quarter,final.csv",
1217+
},
1218+
{
1219+
name: "quoted filename containing an escaped space",
1220+
header: String.raw`attachment; filename="quarter\ final.csv"`,
1221+
fileName: "quarter final.csv",
1222+
},
1223+
{
1224+
name: "quoted filename containing an escaped hyphen",
1225+
header: String.raw`attachment; filename="quarter\-final.csv"`,
1226+
fileName: "quarter-final.csv",
1227+
},
1228+
{
1229+
name: "quoted filename containing multiple escaped punctuation characters",
1230+
header: String.raw`attachment; filename="quarter\ final\,v2.csv"`,
1231+
fileName: "quarter final,v2.csv",
1232+
},
1233+
{
1234+
name: "filename text inside an unrelated quoted parameter is ignored",
1235+
header: 'attachment; note="x; filename=spoof.csv; y"; filename=safe.csv',
1236+
fileName: "safe.csv",
1237+
},
1238+
{
1239+
name: "Windows drive paths still decode escaped punctuation",
1240+
header: String.raw`attachment; filename="C:/tmp/quarter\;final.csv"`,
1241+
fileName: "quarter;final.csv",
1242+
},
1243+
{
1244+
name: "mixed Windows path separators preserve the final basename",
1245+
header: String.raw`attachment; filename="C:/tmp/reports\Q1.csv"`,
1246+
fileName: "Q1.csv",
1247+
},
1248+
{
1249+
name: "mixed relative Windows path separators preserve the final basename",
1250+
header: String.raw`attachment; filename="reports/2026\Q1.csv"`,
1251+
fileName: "Q1.csv",
1252+
},
1253+
{
1254+
name: "legacy UNC Windows path is reduced to its basename",
1255+
header: String.raw`attachment; filename="\\server\share\photo.csv"`,
1256+
fileName: "photo.csv",
1257+
},
1258+
{
1259+
name: "legacy UNC Windows path preserves a Unicode-leading basename",
1260+
header: String.raw`attachment; filename="\\server\share\é.csv"`,
1261+
fileName: "é.csv",
1262+
},
1263+
{
1264+
name: "legacy UNC Windows path preserves a punctuation-leading basename",
1265+
header: String.raw`attachment; filename="\\server\share\;photo.csv"`,
1266+
fileName: ";photo.csv",
1267+
},
1268+
{
1269+
name: "legacy UNC Windows path preserves a space-leading basename",
1270+
header: String.raw`attachment; filename="\\server\share\ photo.csv"`,
1271+
fileName: " photo.csv",
1272+
},
1273+
{
1274+
name: "legacy relative Windows path is reduced to its basename",
1275+
header: String.raw`attachment; filename="reports\Q1.csv"`,
1276+
fileName: "Q1.csv",
1277+
},
1278+
{
1279+
name: "nested legacy relative Windows path is reduced to its basename",
1280+
header: String.raw`attachment; filename="reports\2026\Q1.csv"`,
1281+
fileName: "Q1.csv",
1282+
},
1283+
{
1284+
name: "UTF-8 filename with a language tag",
1285+
header: "attachment; filename*=UTF-8'en'%E2%82%ACrates.csv",
1286+
fileName: "€rates.csv",
1287+
},
1288+
{
1289+
name: "ISO-8859-1 extended filename",
1290+
header: "attachment; filename*=ISO-8859-1''caf%E9.csv",
1291+
fileName: "café.csv",
1292+
},
1293+
{
1294+
name: "valid extended filename preferred over plain fallback",
1295+
header: "attachment; filename=legacy.csv; filename*=UTF-8'en'%E2%82%ACrates.csv",
1296+
fileName: "€rates.csv",
1297+
},
1298+
{
1299+
name: "malformed extended filename falls back to plain filename",
1300+
header: "attachment; filename=fallback.csv; filename*=UTF-8''%ZZbad.csv",
1301+
fileName: "fallback.csv",
1302+
},
1303+
{
1304+
name: "unsupported extended charset falls back to plain filename",
1305+
header: "attachment; filename*=UTF-16''bad.csv; filename=fallback.csv",
1306+
fileName: "fallback.csv",
1307+
},
1308+
] as const)("parses $name for buffered and stored remote media", async (testCase) => {
1309+
const fetchImpl = vi.fn(
1310+
async () =>
1311+
new Response(makeStream([new Uint8Array([1, 2, 3])]), {
1312+
status: 200,
1313+
headers: {
1314+
"content-disposition": testCase.header,
1315+
"content-type": "text/csv",
1316+
},
1317+
}),
1318+
);
1319+
const request = {
1320+
url: "https://example.com/download",
1321+
fetchImpl,
1322+
lookupFn: makeLookupFn(),
1323+
maxBytes: 8,
1324+
};
1325+
1326+
const buffered = await readRemoteMediaBuffer(request);
1327+
const stored = await saveRemoteMedia(request);
1328+
1329+
expect(buffered.fileName).toBe(testCase.fileName);
1330+
expect(stored.fileName).toBe(testCase.fileName);
1331+
expect(fetchImpl).toHaveBeenCalledTimes(2);
1332+
});
1333+
11971334
it.each([
11981335
[`attachment; filename*=UTF-8''reports%2FQ1.pdf`, "reports_Q1.pdf"],
11991336
[`attachment; filename*=UTF-8''reports%5CQ1.pdf`, "reports_Q1.pdf"],

0 commit comments

Comments
 (0)