UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 4.75 kB
import { c as isZipContainerMime, d as normalizeMimeType, n as detectMime, u as mimeTypeFromFilePath } from "./mime-D2wVBzpa.mjs"; import { MIMEType } from "node:util"; //#region packages/media-core/src/attachment-classify.ts const TEXT_APPLICATION_MIME = /^application\/(?:json|javascript|xml|yaml|x-yaml)$/; const DOCUMENT_MIME = /^application\/(?:pdf|msword|x-cfb|vnd\.(?:apple\.(?:keynote|numbers|pages)|ms-.+|oasis\.opendocument\..+|openxmlformats-officedocument\..+))$/; const ARCHIVE_MIME = /^application\/(?:gzip|vnd\.rar|x-7z-compressed|x-gzip|x-rar-compressed|x-tar|x-zip-compressed|zip)$/; const WORDISH_CHAR = /[\p{L}\p{N}]/u; function attachmentClassFromMime(e) { const t = normalizeMimeType(e); if (!t) return "binary"; if (t.startsWith("text/") || TEXT_APPLICATION_MIME.test(t) || t.endsWith("+json") || t.endsWith("+xml")) return "text"; if (t.startsWith("image/")) return "image"; if (t.startsWith("audio/")) return "audio"; if (t.startsWith("video/")) return "video"; if (DOCUMENT_MIME.test(t)) return "document"; return ARCHIVE_MIME.test(t) || isZipContainerMime(t) ? "archive" : "binary"; } function resolveUtf16Charset(e) { if (e.length < 2) return; const t = e.readUInt16LE(0); if (t === 65279) return "utf-16le"; if (t === 65534) return "utf-16be"; const n = Math.min(e.length, 2048); let r = 0; let i = 0; for (let t = 0; t < n; t += 1) if (e[t] === 0) { if (t % 2 === 0) r += 1; else i += 1; } if ((r + i) / n <= .2) return; return i >= r ? "utf-16le" : "utf-16be"; } function textRatios(e, t) { let n = 0; let r = 0; let i = 0; for (const a of e) { const e = a.codePointAt(0) ?? 0; const o = e === 9 || e === 10 || e === 13 || e === 32; const s = !o && (e < 32 || e >= 127 && e <= 159); i += 1; n += Number(!s); r += Number(o || !s && t && WORDISH_CHAR.test(a)); } return i === 0 ? [0, 0] : [n / i, r / i]; } function isCleanText(e, t) { try { return textRatios(new TextDecoder(t, { fatal: true }).decode(e), false)[0] === 1; } catch { return false; } } function sniffTextCharset(e) { const t = e.subarray(0, 4096); const n = t.subarray(-4); const r = n.findLastIndex((e) => (e & 192) !== 128); const i = n[r] ?? 0; const a = i >= 240 ? 4 : i >= 224 ? 3 : i >= 194 ? 2 : 1; const o = t.length + Math.max(0, r + a - n.length); try { return textRatios(new TextDecoder("utf-8", { fatal: true }).decode(e.subarray(0, o)), false)[0] > .85 ? "utf-8" : void 0; } catch { const [e, n] = textRatios(new TextDecoder("windows-1252").decode(t), true); return e > .95 && n > .3 ? "windows-1252" : void 0; } } async function classifyAttachmentBytes(e) { const t = await detectMime({ buffer: e.buffer, headerMime: e.declaredMime, additionalMimeHints: e.additionalMimeHints, filePath: e.name ?? void 0 }); const n = attachmentClassFromMime(t); const r = [e.declaredMime, ...e.additionalMimeHints ?? []]; let i; for (const e of r) { if (!e) continue; try { const t = new MIMEType(e).params.get("charset"); if (t !== null) { i = t; break; } } catch {} } const a = e.buffer.length >= 2 && (e.buffer.readUInt16LE(0) === 65279 || e.buffer.readUInt16LE(0) === 65534); if (t === "application/octet-stream" || t?.startsWith("application/vnd.") || n !== "binary" && !a) { let a = n === "text" ? resolveUtf16Charset(e.buffer) ?? i : void 0; if (n === "text" && !a && !r.some((e) => /;\s*charset\s*=/i.test(e ?? "")) && sniffTextCharset(e.buffer) === "windows-1252") a = "windows-1252"; return a ? { mime: t, class: n, charset: a } : { mime: t, class: n }; } const o = await detectMime({ buffer: e.buffer }); if (o && o === t && attachmentClassFromMime(o) === "binary") return { mime: o, class: "binary" }; const s = e.buffer.length >= 4 ? e.buffer.readUInt32BE(0) : 0; if (s === 1347093252 || s === 1347092738 || s === 1347093766) return { mime: t, class: "archive" }; const c = a ? resolveUtf16Charset(e.buffer) : void 0; if (n === "text" && c && isCleanText(e.buffer, c)) return { mime: t, class: "text", charset: c }; const l = resolveUtf16Charset(e.buffer) ?? sniffTextCharset(e.buffer); if (!l) return { mime: t, class: "binary" }; const u = mimeTypeFromFilePath(e.name); const d = new TextDecoder(l).decode(e.buffer.subarray(0, Math.min(e.buffer.length, 8192))).split(/\r?\n/, 1)[0]; const f = (attachmentClassFromMime(u) === "text" ? u : void 0) ?? (d?.includes(",") ? "text/csv" : d?.includes(" ") ? "text/tab-separated-values" : "text/plain"); const p = l === "utf-16le" || l === "utf-16be" ? l : i ?? l; return { mime: f, class: "text", ...p !== "utf-8" ? { charset: p } : {} }; } //#endregion export { classifyAttachmentBytes as n, attachmentClassFromMime as t };