mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 06:17:27 +00:00
fix(media): harden unknown mime handling from #39199 (thanks @nicolasgrasset)
Co-authored-by: Nicolas Grasset <nicolas.grasset@gmail.com>
This commit is contained in:
@@ -3,11 +3,11 @@ export const MAX_AUDIO_BYTES = 16 * 1024 * 1024; // 16MB
|
||||
export const MAX_VIDEO_BYTES = 16 * 1024 * 1024; // 16MB
|
||||
export const MAX_DOCUMENT_BYTES = 100 * 1024 * 1024; // 100MB
|
||||
|
||||
export type MediaKind = "image" | "audio" | "video" | "document" | "unknown";
|
||||
export type MediaKind = "image" | "audio" | "video" | "document";
|
||||
|
||||
export function mediaKindFromMime(mime?: string | null): MediaKind {
|
||||
export function mediaKindFromMime(mime?: string | null): MediaKind | undefined {
|
||||
if (!mime) {
|
||||
return "unknown";
|
||||
return undefined;
|
||||
}
|
||||
if (mime.startsWith("image/")) {
|
||||
return "image";
|
||||
@@ -27,7 +27,7 @@ export function mediaKindFromMime(mime?: string | null): MediaKind {
|
||||
if (mime.startsWith("application/")) {
|
||||
return "document";
|
||||
}
|
||||
return "unknown";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function maxBytesForKind(kind: MediaKind): number {
|
||||
|
||||
@@ -128,7 +128,9 @@ describe("mediaKindFromMime", () => {
|
||||
{ mime: "text/plain", expected: "document" },
|
||||
{ mime: "text/csv", expected: "document" },
|
||||
{ mime: "text/html; charset=utf-8", expected: "document" },
|
||||
{ mime: "model/gltf+json", expected: "unknown" },
|
||||
{ mime: "model/gltf+json", expected: undefined },
|
||||
{ mime: null, expected: undefined },
|
||||
{ mime: undefined, expected: undefined },
|
||||
] as const)("classifies $mime", ({ mime, expected }) => {
|
||||
expect(mediaKindFromMime(mime)).toBe(expected);
|
||||
});
|
||||
@@ -136,4 +138,9 @@ describe("mediaKindFromMime", () => {
|
||||
it("normalizes MIME strings before kind classification", () => {
|
||||
expect(kindFromMime(" Audio/Ogg; codecs=opus ")).toBe("audio");
|
||||
});
|
||||
|
||||
it("returns undefined for missing or unrecognized MIME kinds", () => {
|
||||
expect(kindFromMime(undefined)).toBeUndefined();
|
||||
expect(kindFromMime("model/gltf+json")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -187,6 +187,6 @@ export function imageMimeFromFormat(format?: string | null): string | undefined
|
||||
}
|
||||
}
|
||||
|
||||
export function kindFromMime(mime?: string | null): MediaKind {
|
||||
export function kindFromMime(mime?: string | null): MediaKind | undefined {
|
||||
return mediaKindFromMime(normalizeMimeType(mime));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user