diff --git a/extensions/msteams/src/attachments/shared.ts b/extensions/msteams/src/attachments/shared.ts index 1a9e839f71b..533ecf197bf 100644 --- a/extensions/msteams/src/attachments/shared.ts +++ b/extensions/msteams/src/attachments/shared.ts @@ -1,7 +1,5 @@ import { Buffer } from "node:buffer"; import { lookup } from "node:dns/promises"; -export { estimateBase64DecodedBytes } from "openclaw/plugin-sdk/media-runtime"; -import { estimateBase64DecodedBytes } from "openclaw/plugin-sdk/media-runtime"; import { buildHostnameAllowlistPolicyFromSuffixAllowlist, isHttpsUrlAllowedByHostnameSuffixAllowlist, @@ -84,6 +82,42 @@ export const DEFAULT_MEDIA_AUTH_HOST_ALLOWLIST = [ export const GRAPH_ROOT = "https://graph.microsoft.com/v1.0"; export { isRecord }; +// Keep this local; importing the broad media-runtime SDK barrel pulls image/audio runtimes into +// hot MSTeams attachment tests for one tiny estimator. +export function estimateBase64DecodedBytes(base64: string): number { + let effectiveLen = 0; + for (let i = 0; i < base64.length; i += 1) { + const code = base64.charCodeAt(i); + if (code <= 0x20) { + continue; + } + effectiveLen += 1; + } + + if (effectiveLen === 0) { + return 0; + } + + let padding = 0; + let end = base64.length - 1; + while (end >= 0 && base64.charCodeAt(end) <= 0x20) { + end -= 1; + } + if (end >= 0 && base64[end] === "=") { + padding = 1; + end -= 1; + while (end >= 0 && base64.charCodeAt(end) <= 0x20) { + end -= 1; + } + if (end >= 0 && base64[end] === "=") { + padding = 2; + } + } + + const estimated = Math.floor((effectiveLen * 3) / 4) - padding; + return Math.max(0, estimated); +} + /** * Host suffixes for SharePoint/OneDrive shared links that must be fetched via * the Graph `/shares/{shareId}/driveItem/content` endpoint instead of directly.